CCPC2019秦皇岛 - Forest Program

Forest Program

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 431 Accepted Submission(s): 158

Problem Description
The kingdom of Z is fighting against desertification these years since there are plenty of deserts in its wide and huge territory. The deserts are too arid to have rainfall or human habitation, and the only creatures that can live inside the deserts are the cactuses. In this problem, a cactus in desert can be represented by a cactus in graph theory.
In graph theory, a cactus is a connected undirected graph with no self-loops and no multi-edges, and each edge can only be in at most one simple cycle. While a tree in graph theory is a connected undirected acyclic graph. So here comes the idea: just remove some edges in these cactuses so that the remaining connected components all become trees. After that, the deserts will become forests, which can halt desertification fundamentally.
Now given an undirected graph with n vertices and m edges satisfying that all connected components are cactuses, you should determine the number of schemes to remove edges in the graph so that the remaining connected components are all trees. Print the answer modulo 998244353.
Two schemes are considered to be different if and only if the sets of removed edges in two schemes are different.

Input
The first line contains two non-negative integers n, m (1 ≤ n ≤ 300 000, 0 ≤ m ≤ 500 000), denoting the number of vertices and the number of edges in the given graph.
Next m lines each contains two positive integers u, v (1 ≤ u, v ≤ n, u = v), denoting that vertices u and v are connected by an undirected edge.
It is guaranteed that each connected component in input graph is a cactus.

Output
Output a single line containing a non-negative integer, denoting the answer modulo 998244353.

Sample Input
3 3
1 2
2 3
3 1
6 6
1 2
2 3
3 1
2 4
4 5
5 2

Sample Output
7
49

Source
642ccpcQHD


点双求出环即可。分别从环上的点和非环上的点考虑即可。


AC代码:

#pragma GCC optimize(2)
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int p=998244353,N=3e5+10;
int T,n,m,cnt,dfn[N],low[N],res;
int head[N],nex[N<<2],to[N<<2],tot=1,idx;
stack<int> st;
vector<int> v[N];
inline void add(int a,int b){
	to[++tot]=b; nex[tot]=head[a]; head[a]=tot;
}
int qmi(int a,int b){
	int res=1;
	while(b){
		if(b&1) res=res*a%p;	b>>=1;	a=a*a%p;
	}
	return res;
}
void tarjan(int x,int fa){
	dfn[x]=low[x]=++cnt;	st.push(x);
	if(x==fa&&(!head[x])){
		v[++idx].push_back(x);	return ;
	}
	for(int i=head[x];i;i=nex[i]){
		if(!dfn[to[i]]){
			tarjan(to[i],fa);
			low[x]=min(low[x],low[to[i]]);
			if(low[to[i]]>=dfn[x]){
				idx++;	int u;
				do{
					u=st.top();	st.pop();	v[idx].push_back(u);
				}while(u!=to[i]);
				v[idx].push_back(x);
			}
		}else low[x]=min(low[x],dfn[to[i]]);
	}
}
void init(){
	for(int i=1;i<=n;i++)	v[i].clear();	
	while(!st.empty())	st.pop();
	cnt=0;	tot=1;	idx=0;	res=1;
	memset(head,0,sizeof head);	memset(dfn,0,sizeof dfn);
	memset(low,0,sizeof low);
}
signed main(){
	while(~scanf("%lld %lld",&n,&m)){
		init();
		if(m==0){
			puts("0");	continue;
		}
		for(int i=1;i<=m;i++){
			int a,b;	scanf("%lld %lld",&a,&b);	add(a,b);	add(b,a);
		}
		for(int i=1;i<=n;i++)	if(!dfn[i])	tarjan(i,0);
		for(int i=1;i<=idx;i++){
			if(v[i].size()<=2)	continue;	m-=v[i].size();
			res=res*(qmi(2,v[i].size())-1+p)%p;
		}
		res=res*qmi(2,m)%p;
		printf("%lld\n",res);
	}
	return 0;
}
全部评论

相关推荐

不愿透露姓名的神秘牛友
06-27 20:55
点赞 评论 收藏
分享
见见123:简历没有啥问题,是这个社会有问题。因为你刚毕业,没有工作经历,现在企业都不要没有工作经历的。社会病了。
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
07-01 11:47
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务