2019ccpc秦皇岛[Forest Program]dfs + 快速幂

[Forest Program] dfs判环 + 快速幂

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≤300000,0≤m≤500000), 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

Sample Output

7

Sample Input

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

Sample Output

49

题意:有一个无向图,让我们把图的某些边删掉,让图变成树(当然可以删到没有边,所有点单点成树)。
思路是这样,如果这个边不在一个环里,那么这个边可删可不删两种情况,就是给ans乘2,如果边在环里,我们可以维护这个环的大小,如果这个环有n条边的话,我们还是对每条边都有可删可不删的情况,但是对于一个环,我们必须要删掉至少一个边才能成树,所以,一个n条边的环有2n-1种情况,然后我们对于每个环我们给ans乘2n-1。

所以如何维护每个环的大小呢,菜鸡的我思考了好久,还是决定dfs试一试,结果一发就过了。。。。
另外网上看到,并查集加lca也可以判环大小。

ac代码

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 300005;
const int mod = 998244353;
vector<int >g[maxn];//邻接表存图
int vis[maxn];//记录是否遍历过这个点
int sum[maxn];//记录深度
int cir[maxn];//存储环的大小
ll ans = 1;
int cnt = 0;//环的个数
void dfs(int cur,int fa,int deep){
    if(vis[cur]==1){
        if(sum[fa]-sum[cur]<0)return ;//这里注意:如果我们fa的深度小的话说明
        							  //这个环已经找过了,可以画个图手推一下
        cir[cnt++] = sum[fa]-sum[cur]+1;//记录环大小是按深度算的
        return ;
    }
    sum[cur] = deep;
    vis[cur] = 1;
    for(int i=0;i<g[cur].size();i++){
        int v = g[cur][i];
        if(v==fa)continue;
        dfs(v,cur,deep+1);
    }
}
long long pow(ll a,ll b,int mod){//最后都是2的次方倍还要取模果断写快速幂
    ll ans = 1;
    while(b>0){
        if(b&1)ans=ans*a%mod;
        b>>=1;
        a = a*a%mod;
    }
    return ans;
}
int main(){
    int n,m;
    scanf("%d%d",&n,&m);
    for(int i=0;i<m;i++){
        int u,v;
        scanf("%d%d",&u,&v);
        g[u].push_back(v);
        g[v].push_back(u);
    }
    for(int i=1;i<=n;i++){
        if(!vis[i])dfs(i,0,1);
    }
    for(int i=0;i<cnt;i++){
        //cout<<cir[i]<<endl;
        m-=cir[i];//算不在环里边个数
    }
    for(int i = 0;i < cnt;i++){
        ans=ans*((pow(2,cir[i],mod))-1)%mod;
    }
    ans = ans*pow(2,m,mod)%mod;
    cout<<ans;
}
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值