hdu 6736 Forest Program(2019CCPC秦皇岛 F题)dfs做法

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6736Forest Program

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

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


分析:

对于有n条边的环结果为2^n -1,
对于此题,先统计在环中的边数qn,用总边数m-qn就是不在环中的边数,最后将每个环的结果*2^{m-qn}
定义一个数组存每个结点对应的序号,这个数组使用来计算出现环中的边数的
对于每个环的边数可以用dfs,然后vis标记,如果遇到已经标记的说明存在环,两个序号相减就得到环中的边数

为避免超时,最后快速幂结果用mp存下

代码:
#include <bits/stdc++.h>
using namespace std;
#define ll long long 
const int M=5e5+10;
const int N=3e5+10;
const int mod=998244353;


struct edge{
	int to;
	int nxt;
}edge[M<<1];
int head[N];
int cnt=0;
int duiyin[N];
int vis[N];
map<ll,ll>mp;

int qn;//表示在圈中的总边数
ll ans;

ll mul(ll a,ll n)
{
	if(mp[n]) return mp[n];
	ll bs=a;
	ll ans=1;
	ll t=n;
	while(n)
	{
		if(n&1)
			ans=ans*bs%mod;
		n>>=1;
		bs=bs*bs%mod;
	}
	
	return mp[t]=ans;
	
}

ll cal(int n)
{
	return ((mul(2,n)-1)%mod+mod)%mod;
}

void addEdge(int u,int v)
{
	edge[cnt].to=v;
	edge[cnt].nxt=head[u];
	head[u]=cnt++;
}

void dfs(int u,int val)
{
	vis[u]=1;
	duiyin[u]=val;
	int to;
	for(int i=head[u];~i;i=edge[i].nxt)
	{
		to=edge[i].to;
		if(vis[to])
		{
			int s=duiyin[u]-duiyin[to]+1;
			if(s>=3){
				qn=qn+s;
				ans=ans*cal(s)%mod;
			}
		}
		else
		{
			dfs(to,val+1);
		}
			
	}
}

int main()
{
	int n,m,t,u,v;
	// scanf("%d",&t);
	// while(t--)
	while(~scanf("%d%d",&n,&m))
	{
		// memset(head,-1,sizeof(head));
		
		// scanf("%d%d",&n,&m);
		for(int i=0;i<=n;i++)
		{
			head[i]=-1;
			vis[i]=0;
		}
		for(int i=0;i<m;i++)
		{
			scanf("%d%d",&u,&v);
			addEdge(u,v);
			addEdge(v,u);
		}
		if(m==0)
		{
			printf("0\n");
			continue;
		}
		qn=0;
		ans=1;
		for(int i=1;i<=n;i++)
		{
			if(!vis[i])
				dfs(i,1);
		}
		ans=ans*mul(2,m-qn)%mod;
		printf("%lld\n",ans);
		
	}
	
	return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值