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≤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.
Examples
- Input
3 3
1 2
2 3
3 1 - Output
7 - Input
6 6
1 2
2 3
3 1
2 4
4 5
5 2 - Output
49
思路
- 该题就是寻找环的数量,假设一个环中有n条边,最后该环拆边的方法就是 2^n-1 种 (例:2^3-1=7 )
- 然后不在环中的边,可拆分的情况就是2^m (环中要排除都不拆的情况)
- 因此最后的答案就是,各个环的情况相乘再乘以不是环的边的情况。
- 以下图为例,环中有条边,环外一条边 答案是 2^1 * (2^3 -1) = 14(下图只把环的情况画出,环外那条边可以去或者不去,因此乘二,也就是14)
代码如下
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAXN=3e6+7;
const int MAXM=3e6+7;
const ll MOD=998244353;
//快读 ,利用读取字符串速度较快来读取,然后转化为数字,
int read(){
int x=0,f=1;
char ch=getchar();
while(ch<'0' || ch>'9'){ if(ch=='-')f=-1;ch=getchar();}
while(ch<='9' && ch>='0') x=(x<<3) +(x<<1)+(ch^48),ch=getchar();
return x*f;
}
struct Edge{
int v;
int next;
}e[MAXM<<1];
int head[MAXM<<1],vis[MAXN],pre[MAXN],sum,cnt;
ll ans;
//前向星存图
void add(int v,int u){
e[++cnt].v=v;
e[cnt].next = head[u];
head[u]=cnt;
}
//快速幂
ll Pow(ll a,ll b){
ll s=1;
while(b){
if(b&1) s=(s*a)%MOD;
a=(a*a)%MOD;
b=b>>1;
}
return s;
}
// dfs找环
void dfs(int cur,int fath){
vis[cur]=1;
// 遍历之前存的图,前向星存图遍历方法
for(int i=head[cur];i!=-1;i=e[i].next){
int nex = e[i].v;
// 如果当前节点与当前根节点的父节点相同,则跳过
if(nex==fath)continue;
// 如果没有走过就标记前置节点,从该点开始走
if(!vis[nex]){
pre[nex]=cur;
dfs(nex,cur);
//此时代表形成了一个环,回到了正在走的点
}else if(vis[nex]==1){
int beg=cur;
int tot = 1;
while(beg!=nex){
tot++;
beg=pre[beg];
}
sum += tot;
ans = (ans*(Pow(2,tot)-1))%MOD ;
}
}
vis[cur]=2;
}
int n,m;
int main(){
memset(head,-1,sizeof(head));
n=read(),m=read();
for(int i=1,a,b;i<=m;i++){
a=read(),b=read();
add(a,b);
add(b,a);
}
ans = 1,sum=0;
for(int i=1;i<=n;i++){
if(!vis[i])dfs(i,-1);
}
cout << (Pow(2,m-sum)%MOD*ans)%MOD <<endl;
}