2019CCPC秦皇岛HDU - 6736 F - Forest Program(dfs找环 组合数学)

简介: 2019CCPC秦皇岛HDU - 6736 F - Forest Program(dfs找环 组合数学)

linkkk

题意:

给出仙人掌图,问有多少种删边的方案使得该图变为一个森林。

思路:

跟上题类似。

从每个环出发考虑方案数,长度为t的环的方案数为2 t − 1,都不删的情况要减去,对于剩下的非环边,有删和不删两种选择,再乘上2 x , x =非环边的数量。

代码:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;typedef unsigned long long ull;
typedef pair<ll,ll>PLL;typedef pair<int,int>PII;typedef pair<double,double>PDD;
#define I_int ll
inline ll read(){ll x=0,f=1;char ch=getchar();while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}return x*f;}
#define read read()
#define rep(i, a, b) for(int i=(a);i<=(b);++i)
#define dep(i, a, b) for(int i=(a);i>=(b);--i)
ll ksm(ll a,ll b,ll p){ll res=1;while(b){if(b&1)res=res*a%p;a=a*a%p;b>>=1;}return res;}
const int maxn=3e5+100,inf=0x3f3f3f3f,mod=998244353;
vector<int>g[maxn];
int n,m,vis[maxn],siz[maxn];
vector<int>w;
void dfs(int u,int sum,int fa){
  vis[u]=1;
  siz[u]=sum;
  for(auto t:g[u]){
    if(t==fa) continue;
    if(!vis[t]) dfs(t,sum+1,u);
    else if(vis[t]==1){
      w.push_back(siz[u]-siz[t]+1);
    }
  }
  vis[u]=2;
}
int main(){
  while(cin>>n>>m){
    rep(i,1,n) g[i].clear(),siz[i]=vis[i]=0;
    w.clear();
    rep(i,1,m){
      int u=read,v=read;
      g[u].push_back(v);
      g[v].push_back(u);
    }
    rep(i,1,n){
      if(!siz[i]) dfs(i,1,0);
    }
    ll ans=1,tot=0;
    for(auto t:w){
    //  cout<<t<<endl;
      tot+=t;
      ans=(ans*(ksm(2,t,mod)-1+mod))%mod;
    }
    ans=ans*ksm(2,m-tot,mod)%mod;
    cout<<ans<<endl;
  }
    return 0;
}


目录
相关文章
|
9月前
|
算法
1091 zoj Knight Moves的BFS算法和DFS
1091 zoj Knight Moves的BFS算法和DFS
35 0
|
9月前
UVa302 - John's trip(并查集、欧拉回路、DFS)
UVa302 - John's trip(并查集、欧拉回路、DFS)
36 0
AtCoder Beginner Contest 226 E - Just one(dfs求连通块 组合数学)
AtCoder Beginner Contest 226 E - Just one(dfs求连通块 组合数学)
80 0
|
人工智能
Codeforces1501——C. Going Home(鸽巢原理)
Codeforces1501——C. Going Home(鸽巢原理)
52 0
AtCoder Beginner Contest 203(Sponsored by Panasonic) D.Pond(二分+二维前缀和)
AtCoder Beginner Contest 203(Sponsored by Panasonic) D.Pond(二分+二维前缀和)
65 0
AtCoder Beginner Contest 223 D - Restricted Permutation(建图 思维 构造 拓扑排序)
AtCoder Beginner Contest 223 D - Restricted Permutation(建图 思维 构造 拓扑排序)
87 0
AtCoder Beginner Contest 216 G - 01Sequence (并查集 贪心 树状数组 差分约束)
AtCoder Beginner Contest 216 G - 01Sequence (并查集 贪心 树状数组 差分约束)
103 0
AtCoder Beginner Contest 216 D - Pair of Balls (思维建图 拓扑排序判断有向图是否有环)
AtCoder Beginner Contest 216 D - Pair of Balls (思维建图 拓扑排序判断有向图是否有环)
89 0
POJ-2488,A Knight's Journey(DFS)
POJ-2488,A Knight's Journey(DFS)
HDU-1142,A Walk Through the Forest(Dijkstra+DFS)
HDU-1142,A Walk Through the Forest(Dijkstra+DFS)