首页 > 其他分享 >【ICPC】The 2021 ICPC Asia Shanghai Regional Programming Contest G

【ICPC】The 2021 ICPC Asia Shanghai Regional Programming Contest G

时间:2024-10-15 22:46:40浏览次数:3  
标签:Contest int Shanghai cin ICPC leq edges odd 998244353

Edge Groups

#树形结构 #组合数学 #树形dp

题目描述

Given an undirected connected graph of n n n vertices and n − 1 n-1 n−1 edges, where n n n is guaranteed to be odd. You want to divide all the n − 1 n-1 n−1 edges to n − 1 2 \frac{n-1}{2} 2n−1​ groups under following constraints:

  • There are exactly 2 edges in each group
  • The 2 edges in the same group share a common vertex

Determine the number of valid dividing schemes modulo 998244353 998244353 998244353. Two schemes are considered different if there are 2 edges that are in the same group in one scheme but not in the same group in the other scheme.

输入格式

The first line contains one integer n   ( 3 ≤ n ≤ 1 0 5 ) n\,(3\le n \le 10^5) n(3≤n≤105), denoting the number of vertices.

Following n − 1 n-1 n−1 lines each contains two integers u , v   ( 1 ≤ u ≤ v ≤ n ) u,v\ (1 \leq u \leq v \leq n) u,v (1≤u≤v≤n), denoting that vertex u , v u,v u,v are undirectedly connected by an edge.

It is guaranteed that n n n is odd and that the given graph is connected.

输出格式

Output one line containing one integer, denoting the number of valid dividing schemes modulo 998244353 998244353 998244353.

样例 #1

样例输入 #1

7
1 2
1 3
1 7
4 7
5 7
6 7

样例输出 #1

3

解题思路

观察发现,如果子树的边的个数为奇数的时候,需要和选一条边和父节点的连边组合,剩下的边可以两两组合。

如下图:

在这里插入图片描述

其中, n n n条边两两组合的方案数共有 ∏ i = 1 i ≤ n f a c t i   , i ∈ { o d d } \prod_{i=1}^{i\leq n}fact_i \ , i \in \{odd\} ∏i=1i≤n​facti​ ,i∈{odd} 即不大于 n n n的奇数的阶乘之积,可以通过数学归纳法证明,这里不多赘述。

因此,转移方程为:

f u = f u ∏ v ∏ i = 1 i ≤ n f v f a c t i   ( v ∈ c h i l d u , i ∈ { o d d } ) f_u = f_u\prod_v \prod_{i=1}^{i \leq n} f_v fact_i \ ( v \in child_u,i \in \{ odd\}) fu​=fu​v∏​i=1∏i≤n​fv​facti​ (v∈childu​,i∈{odd})

代码

const int N = 1e6 + 7;

vector<int> e[N];
int f[N], sz[N];

void dfs(int u, int fa){
    f[u] = 1;
    for (auto &v : e[u]) {
        if (v == fa) continue;
        dfs(v, u);
        if (sz[v] % 2 == 0) sz[u]++;
        f[u] = f[u] * f[v] % mod;
    }

    if (sz[u] >= 2) {
        for (int i = 1; i <= sz[u]; i += 2) {
            f[u] = f[u] * i % mod;
       }
    }
}

void solve()
{
    int n;
    cin >> n;
    for (int i = 1; i <= n - 1; i++) {
        int u, v;
        cin >> u >> v;
        e[u].push_back(v), e[v].push_back(u);
    }
    dfs(1, -1);
    cout << f[1]<< endl;
}


signed main() {
	ios::sync_with_stdio(0);
	std::cin.tie(0);
	std::cout.tie(0);
	int t = 1;
	//cin >> t;
	while (t--) {
		solve();
	}
};

标签:Contest,int,Shanghai,cin,ICPC,leq,edges,odd,998244353
From: https://blog.csdn.net/Antonio915/article/details/142964001

相关文章

  • 【ICPC】The 2021 ICPC Asia Shanghai Regional Programming Contest H
    LifeisaGame#最小生成树#重构树#图论#贪心题目描述Agoodproblemshouldhaveaconcisestatement.Youaregivenanarrayaaaoflength......
  • AtCoder Beginner Contest 375
    A-Seats#include<bits/stdc++.h>usingnamespacestd;usingi32=int32_t;usingi64=longlong;#defineintlonglongusingvi=vector<int>;usingpii=pair<int,int>;i32main(){ios::sync_with_stdio(false),cin.tie(null......
  • Panasonic Programming Contest 2024(AtCoder Beginner Contest 375)
    PanasonicProgrammingContest2024(AtCoderBeginnerContest375)\(A\)Seats\(AC\)基础字符串。点击查看代码intmain(){intn,ans=0,i;strings;cin>>n>>s;for(i=0;i<n;i++){ans+=(s[i]=='#'&&s[i......
  • AtCoder Beginner Contest 375
    省流版A.枚举所有子串判断即可B.模拟计算记录累加即可C.根据旋转的周期性对每个点旋转次数取模后暴力旋转即可D.枚举\(k\),考虑\(i,j\)的对数,写成数学表达式后维护个数和位置和即可E.背包问题,以前两个数组和为状态,考虑每个数移动到何处转移即可F.逆向,删边变加边,维护......
  • KEYENCE Programming Contest 2024(AtCoder Beginner Contest 374)E题
    六年级蒟蒻来题解了!!!题目大意:给定你一个n,表示有n个生产线,每一个生产线都有两种机器,第i个生产线第一件产品每天可以造Ai件零件但是得付出Pi元的代价,第二件产品每天可以生产Bi件物品但是得付出Qi元的代价,然后给你x元的预算问你所有流水线中的最小值的最大值是多少?思路:首先我们......
  • 2023 Benelux Algorithm Programming Contest (BAPC 23)
    A-\texttt题意\(n\)个软件包,已知大小,可以同时下载\(k\)个,已经下载好了\(m\)个,选\(k\)个下载使得下载完后进度最大,输出已完成进度所占百分比。思路选最大的\(m+k\)个。代码点击查看代码#include<bits/stdc++.h>usingnamespacestd;#defineintlonglongvoid......
  • The 2022 ICPC Asia Hangzhou Regional Programming Contest K. Master of Both
    题目链接题意:给定n个字符串,然后给定q种字典序规则,在这q种字典序规则下求出这n个字符串的逆序对数量。思路:我们发现q很大,对于每一种排序规则都遍历一遍n个字符串去求逆序对的数量复杂度太高,所以考虑预处理。我们知道要判断两个字符串字典序的大小关系,只需要知道它们第......
  • AtCoder Beginner Contest 373 (A-F)
    AtCoderBeginnerContest373(A-F)比赛链接A-September#include<bits/stdc++.h>usingnamespacestd;usingi64=longlong;voidShowball(){intans=0;for(inti=1;i<=12;i++){strings;cin>>s;ans+=((int)s.si......
  • The 2021 ICPC Asia Shenyang Regional Contest
    目录写在前面E签到F签到JBFSB带权并查集H图论I数学L树形DP,容斥M字符串,离线,单调性G贪心写在最后写在前面比赛地址:https://codeforces.com/gym/103427。以下按个人向难度排序。唉唉国庆vp三场唯一打的还像人的一场,最后手里还有两道题在写可惜都没出来呃呃。被树上背......
  • CS224 Program Analysis@Shanghaitech 24 Fall Notes
    1.IntroductionRice'sTheoremStaticAnalysisanalyzesaprogramPtoreasonaboutitsbehaviorsanddetermineswhetheritsatisfiessomepropertiesbeforerunningP.Rice'sTheorem:Anynon-trivialpropertyofthebehaviorofprogramsinare......