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.

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,为了保证图中只有树,每个环中至少要去掉一条边,所以对于每个环有C_{n}^{1} + C_{n}^{2} + ... +C_{n}^{n} = 2^{n} - 1种删法,所以答案就是所有环的删法相乘,最后乘上不在任何环中的边的删法C_{n}^{0} + C_{n}^{1} + ...+C_{n}^{n} = 2^{n}

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll inf = 0x3f3f3f3f;
const ll N = 3e5 + 10;
const ll mod = 998244353;
vector<ll>mp[N];
ll dep[N], cnt, ans;

ll qpow(ll a, ll b) {
    ll ans = 1;
    while(b) {
        if(b & 1)
            ans = ans * a % mod;
        a = a * a % mod;
        b >>= 1;
    }
    return ans % mod;
}

void dfs(ll u, ll fa) {
    dep[u] = dep[fa] + 1;
    ll siz = mp[u].size();
    for(ll i = 0; i < siz; ++i) {
        ll v = mp[u][i];
        if(v == fa) continue;
        if(!dep[v]) dfs(v, u);
        else if(dep[u] > dep[v]) {
            ll tmp = dep[u] - dep[v] + 1;
            ans = ans % mod * ((qpow(2, tmp) - 1 + mod) % mod) % mod;
            cnt += dep[u] - dep[v] + 1;
        }
    }
}

int main() {
    ll n, m, u, v;
    while(~scanf("%lld%lld", &n, &m)) {
        if(m == 0) {
            printf("0\n");
            continue;
        }
        for(ll i = 1; i <= n; ++i) mp[i].clear();
        for(ll i = 1; i <= m; ++i) {
            scanf("%lld%lld", &u, &v);
            mp[u].push_back(v);
            mp[v].push_back(u);
        }
        for(ll i = 1; i <= n; ++i) dep[i] = 0;
        ans = 1, cnt = 0;
        for(ll i = 1; i <= n; ++i)
            if(!dep[i]) dfs(i, 0);
        ans = ans % mod * (qpow(2, m - cnt) % mod) % mod;
        printf("%lld\n", ans % mod);
    }
	return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值