2019CCPC秦皇岛 F-Forest Program(DFS)(快速幂)

题干:

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

思路:

题目给了我们n个点,m条边,树的定义为无向无环连通图,要求去掉一部分边,使图中出现森林(一颗及以上的树),输入没有自环,求有多少种可以构成森林的方式。
首先,如果已经是环的图,去掉任意条边都可以构成森林(除了全保留),也就是说如果一个环有三条边则可以有 2 3 − 1 2^3-1 231种构造方法。
然后考虑图中不构成环的散边,因为不构成环,即使保留这条边也依然可以看作一棵树,去掉的话作为孤立点也是一颗树,所以对于所有散边都有保留和去掉两种选择,就有 2 y 2^y 2y种构造方法(y为散边数量)。
则答案为 ( 2 z i x 2^{zi^x} 2zix -1) * 2 y 2^y 2y(x为环的数量,zi为第i个环中边的数量,y为散边数量)
然后问题就转换成了求在环中边的数量,因为已知总边数,则散边可以用总边数-环的边数求得。
可以使用较为简单的DFS去判环,对每个点打上一个时间戳vis,并且记录该点的上一个点pre;如果某个点的下一个点已经被搜过了且时间戳早于当前点,则说明已经产生了环,则我们根据pre就可以找到这个环(因为已经成环了,所以边数和节点数是相同的)。
然后因为牵扯到了指数和取模,所有记得用快速幂。

#include <bits/stdc++.h>
using namespace std;
int vis[300010],tim,pre[300010];
struct stu{
	int v;
};
vector<stu> e[300010];
vector<int> temp;
void add(int a,int b){
	stu t;
	t.v=b;
	e[a].push_back(t);
}
void dfs(int u){
	vis[u]=++tim;
	for(int i=0;i<e[u].size();i++){
		int v=e[u][i].v;
		if(v==pre[u])	continue;
		if(!vis[v]){
			pre[v]=u;
			dfs(v);
		}
		else if(vis[v]>vis[u]){
			int t=v;
			int now=0;
			while(t!=u){
				now++;
				t=pre[t];
			}
			now++;
			temp.push_back(now);
		}
	}
}
long long pw(long long a,long long b){
	long long ans=1;
	while(b){
		if(b&1)
			ans=(ans*a)%998244353;
		a=(a*a)%998244353;
		b>>=1;
	}
	return ans%998244353;
}
int main()
{
	long long ans=1;
	int n,m,u,v;
	scanf("%d%d",&n,&m);
	ans=1;
	for(int i=0;i<m;i++){
		scanf("%d%d",&u,&v);
		add(u,v);
		add(v,u);
	}
	for(int i=1;i<=n;i++){
		if(!vis[i])
			dfs(i);
	}
	for(int i=0;i<temp.size();i++){
		int v=temp[i];
		ans=ans*(pw(2,v)-1)%998244353;
		m-=v;
	}
	ans=ans*(long long)(pw(2,m)%998244353);
	printf("%lld\n",ans%998244353);
    return 0;
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值