2019 CCPC 秦皇岛 Gym - 102361F(HDU - 6736 ) ------ Forest Program

题目

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.

输入

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 a single line containing a non-negative integer, denoting the answer modulo 998244353.

样例

输入:

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

输出:

7
49

题意:

有一个无向图,其中有一些边,现在问,有多少种删除边的方法,使得剩余的图变为一个森林。
注意:无重边、自环,每条边被最多一个简单环包含。

题解:

判环,假如环上的边数是x,则环上有2 ^ x - 1中方法(自己画个环即可看出来),对于每个环都是这个规律,设环上所有方法为num1种;然后剩下的边有y条,有2种情况,删除与不删除,所以有2 ^ y种方法,设剩下边的所有方法为num2种;所以总共有num1 * num2种。期间因为数据较大,使用dfs判环和快速幂。

AC代码

#include<stdio.h>
#include<iostream>
#include<map>
#include<queue>
#include<string.h>
#include<string>
#include<algorithm>
#include<math.h>
#include<vector>
using namespace std;
#define ll long long
const int N = 3e5 + 15;
const ll mod = 998244353;
vector<int>v[N];
int vis[N];
int deep[N];
ll res = 1;
ll quick_pow(ll x, ll y) {
	if (y == 0)
		return 1;
	ll ans = 1;
	while (y) {
		if (y % 2 == 1)
			ans = (ans * x) % mod;
		x = (x * x) % mod;
		y /= 2;
	}
	return ans;
}
int num = 0;
void dfs(int x, int f) {
	vis[x] = 1;
	deep[x] = deep[f] + 1;
	//判环
	for (int i = 0; i < v[x].size(); i++) {
		if (v[x][i] == f) //当前点
			continue;
		if (vis[v[x][i]] == 0) //未走到的点
			dfs(v[x][i], x);
		
		//当前点已经走过且不是头结点,则成环
		else if (deep[x] > deep[v[x][i]]) {
			//当前环的边数
			int d = deep[x] - deep[v[x][i]] + 1;
			//所有环的边数
			num += d;
			//环上的情况
			res = (res * (quick_pow(2, d) - 1) + mod) % mod;
		}
	}
}
int main()
{
	int n, m;
	scanf("%d%d", &n, &m);
	for (int i = 0; i < m; i++) {
		int x, y;
		scanf("%d%d", &x, &y);
		v[x].push_back(y);
		v[y].push_back(x);
	}
	for (int i = 1; i <= n; i++) {
		if (vis[i] == 0)
			dfs(i, 0);
	}
	res = (res * quick_pow(2, m - num) + mod) % mod;
	printf("%lld\n", res);
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值