hdu6736 Forest Program (推导+求环的边数+快速幂)

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

思路:

找出所有环,每个环至少选择一条边删掉,
假设当前环上的边为x
那么方案数就是2x-1(减1是因为要去掉一个都不删的方案)
不在环上的边为y条,可删可不删,方案数就是2y

如果有k个环,y条边不在环上,则:
答案就是(2x1-1)(2x2-1)…(2xk-1)再乘上2y
用快速幂算就行了

ps:
代码用的dfs求环

题目保证是简单环,所以求环还有其他方法:

1.并查集+lca
存图的时候并查集合并,如果一条边的两端在同一集合中,
则说明出现环了,记录下这条边,但是不加入图中(相对于删边)
最后的图一定是一棵树,对于存下的每条边
环的大小就是数上两端点路径上的边数+1(用lca求就行了)

2.点双连通分量
每个分量大小就是环的边数

code:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<stack>
#include<vector>
typedef long long ll;
using namespace std;
const int mod=998244353;
const int maxm=3e5+5;
vector<int>g[maxm];//存图
int pre[maxm];//前驱
int dfn[maxm],cnt;//时间戳
vector<int>temp;//存环的边数
void dfs(int x){
    dfn[x]=++cnt;
    int len=g[x].size();
    for(int i=0;i<len;i++){
        int v=g[x][i];
        if(v==pre[x])continue;
        if(!dfn[v]){
            pre[v]=x;
            dfs(v);
        }else if(dfn[v]>dfn[x]){
            int t=v;
            int cnt=0;
            while(t!=x){
                cnt++;
                t=pre[t];
            }
            cnt++;
            temp.push_back(cnt);
        }
    }
}
ll ppow(ll a,ll b){//快速幂
    ll ans=1;
    while(b){
        if(b&1){
            ans=ans*a%mod;
        }
        b>>=1;
        a=a*a%mod;
    }
    return ans;
}
signed main(){
    int n,m;
    scanf("%d%d",&n,&m);
    for(int i=1;i<=m;i++){
        int a,b;
        scanf("%d%d",&a,&b);
        g[a].push_back(b);
        g[b].push_back(a);
    }
    for(int i=1;i<=n;i++){
        if(!dfn[i]){
            dfs(i);
        }
    }
    ll ans=1;
    int len=temp.size();
    for(int i=0;i<len;i++){
        int v=temp[i];
        ans=ans*(ppow(2,v)-1)%mod;
        m-=v;
    }
    ans=ans*ppow(2,m)%mod;
    cout<<ans<<endl;
    return 0;
}
  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值