萌新求调
查看原帖
萌新求调
379420
Xuejiama1227楼主2022/12/2 20:50
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=5e5+5;
const int M=2e6+5;
const int MOD=1e9+7;
int n,m,fn,dc=0,cnt,se=0,fe=0,sc=0;
ll dp[N][2];
stack<int>s;
struct node{
	int hd,dfn,low,col;
}a[N];
struct nodef{
	int hd,sn,sm;
}f[N];
struct edge{
	int to,nx;
	bool vs;
}e[M];
struct edgef{
	int to,nx;
}g[M];
void addeda(int x,int y){
	e[se].nx=a[x].hd;
	e[se].to=y;
	e[se].vs=0;
	a[x].hd=se++;
	e[se].nx=a[y].hd;
	e[se].to=x;
	e[se].vs=0;
	a[y].hd=se++;
}
void addedf(int x,int y){
	g[fe].nx=f[x].hd;
	g[fe].to=y;
	f[x].hd=fe++;
}
void tarjan(int x){
	a[x].dfn=a[x].low=++dc;
	s.push(x);
	for(int i=a[x].hd;i>=0;i=e[i].nx){
		int y=e[i].to;
		if(!e[i].vs){
			e[i].vs=e[i^1].vs=1;
			if(!a[y].dfn){
				tarjan(y);	
				a[x].low=min(a[x].low,a[y].low);
			}else a[x].low=min(a[x].low,a[y].dfn);
		}
	}
	if(a[x].low==a[x].dfn){
		a[x].col=++sc;
		while(s.top()!=x)a[s.top()].col=sc,s.pop();
		s.pop();
	}
}
ll ksm(ll x,ll y){
	ll r=1;
	while(y){
		if(y&1)r=(r*x)%MOD;
		x=(x*x)%MOD;
		y>>=1;
	}
	return r;
}
void treedp(int x,int fa){
	dp[x][1]=(ksm(2,f[x].sn+f[x].sm)-ksm(2,f[x].sm)+MOD)%MOD;dp[x][0]=ksm(2,f[x].sm);
	for(int i=f[x].hd;i>=0;i=g[i].nx){
		int y=g[i].to;
		if(y==fa)continue;
		treedp(y,x);
		dp[x][0]=(dp[x][0]*(((dp[y][0]+dp[y][1])%MOD*2)%MOD))%MOD;
		dp[x][1]=(dp[x][1]*(((dp[y][0]*2)%MOD+dp[y][1])%MOD))%MOD;
	}
}
int main(){
	int n,m,x,y,i,j;
	scanf("%d%d",&n,&m);
	for(i=1;i<=n;i++)a[i].dfn=a[i].low=a[i].col=0,a[i].hd=-1,f[i].sn=f[i].sm=0,f[i].hd=-1;
	for(i=1;i<=m;i++){
		scanf("%d%d",&x,&y);
		addeda(x,y);
	}
	tarjan(1);fn=sc;
	for(i=1;i<=n;i++){
		f[a[i].col].sn++;
		for(j=a[i].hd;j>=0;j=e[j].nx){
			if(a[i].col!=a[e[j].to].col)addedf(a[i].col,a[e[j].to].col);
			else f[a[i].col].sm++;
		}
	}
	for(i=1;i<=fn;i++)f[i].sm/=2;
	treedp(1,0);
	printf("%lld",((dp[1][0]+dp[1][1])%MOD-ksm(2,m)+MOD)%MOD);
	return 0;
}

能过两个小样例,大样例都过不了

2022/12/2 20:50
加载中...