Https://www.zybuluo.com/ysner/note/1329304question
An undirected graph with an \ (n \) vertex \ (M \) edge that is not necessarily connected.
If an edge is selected, two endpoints cannot be selected.
The maximum number of vertex edges that can be selected at the same time.
At the same time, the number of vertices and edges is the largest.
\ (N \ leq10 ^ 5, m \ leq3 * 10 ^ 5 \)
AnalysisSet the answer to \ (ANS \) and the number of solutions to \ (TOT \).
Discuss the form of Unicom blocks:
- \ (M = n-1 \): \ (ANS = N, TOT = 1 \)
- \ (M = n \): \ (ANS = m \), center \ (TOT = 2 \), part of the tree \ (TOT \) the value can be obtained through \ (DP \).
\ (M> n \): \ (ANS = m \), strongly connected component \ (TOT = 1 \), part of the tree \ (TOT \) the value can be obtained through \ (DP \).
\ (DP \) of the part of the tree \):
Set \ (DP [I] [0/1] \) to calculate the \ (I \) number, and select no number of solutions for this point.
Next, move from the son to discuss it.
To sum up, you can simply scale down the strong Unicom component and then directly create a tree \ (DP.
#include<iostream>#include<cmath>#include<cstdio>#include<cstdlib>#include<cstring>#include<algorithm>#define ll long long#define re register#define il inline#define fp(i,a,b) for(re int i=a;i<=b;++i)#define fq(i,a,b) for(re int i=a;i>=b;--i)using namespace std;const int N=5e5+100,mod=998244353;int n,m,h[N],cnt=1,ans,dfn[N],low[N],sta[N],top,tot,sz[N],bl[N],scc,Esz[N],f[2][N],g[2][N];bool vis[N];struct dat{int u,v;}a[N<<1];struct Edge{int to,nxt;}e[N<<1];il void add(re int u,re int v){ e[++cnt]=(Edge){v,h[u]};h[u]=cnt; e[++cnt]=(Edge){u,h[v]};h[v]=cnt;}il ll gi(){ re ll x=0,t=1; re char ch=getchar(); while(ch!=‘-‘&&(ch<‘0‘||ch>‘9‘)) ch=getchar(); if(ch==‘-‘) t=-1,ch=getchar(); while(ch>=‘0‘&&ch<=‘9‘) x=x*10+ch-48,ch=getchar(); return x*t;}il void Tarjan(re int u,re int las){ dfn[u]=low[u]=++tot;sta[++top]=u;vis[u]=1; re int v; for(re int i=h[u];i+1;i=e[i].nxt) if((i^1)^las) { re int v=e[i].to; if(!dfn[v]) Tarjan(v,i),low[u]=min(low[u],low[v]); else if(vis[v]) low[u]=min(low[u],dfn[v]); } if(dfn[u]==low[u]) { ++scc; do{v=sta[top--];vis[v]=0;++sz[scc];bl[v]=scc;}while(u^v); }}il void dfs(re int u){ f[0][u]=sz[u];f[1][u]=Esz[u];g[0][u]=g[1][u]=1;vis[u]=1; for(re int i=h[u];i+1;i=e[i].nxt) { re int v=e[i].to; if(vis[v]) continue; dfs(v); if(f[0][v]>f[1][v]) f[0][u]+=f[0][v],g[0][u]=1ll*g[0][u]*g[0][v]%mod; if(f[0][v]==f[1][v]) f[0][u]+=f[0][v],g[0][u]=1ll*g[0][u]*(g[0][v]+g[1][v])%mod; if(f[0][v]<f[1][v]) f[0][u]+=f[1][v],g[0][u]=1ll*g[0][u]*g[1][v]%mod; if(f[0][v]>f[1][v]+1) f[1][u]+=f[0][v],g[1][u]=1ll*g[1][u]*g[0][v]%mod; if(f[0][v]==f[1][v]+1) f[1][u]+=f[0][v],g[1][u]=1ll*g[1][u]*(g[0][v]+g[1][v])%mod; if(f[0][v]<f[1][v]+1) f[1][u]+=f[1][v]+1,g[1][u]=1ll*g[1][u]*g[1][v]%mod; }}int main(){ memset(h,-1,sizeof(h)); n=gi();m=gi(); fp(i,1,m) a[i].u=gi(),a[i].v=gi(),add(a[i].u,a[i].v); fp(i,1,n) if(!dfn[i]) Tarjan(i,0); memset(h,-1,sizeof(h));cnt=0; fp(i,1,m) { re int u=a[i].u,v=a[i].v; if(bl[u]^bl[v]) add(bl[u],bl[v]); else ++Esz[bl[u]]; } n=scc;tot=1; fp(i,1,n) if(!vis[i]) { dfs(i); if(f[0][i]<f[1][i]) ans+=f[1][i],tot=1ll*tot*g[1][i]%mod; if(f[0][i]==f[1][i]) ans+=f[1][i],tot=1ll*tot*(g[0][i]+g[1][i])%mod; if(f[0][i]>f[1][i]) ans+=f[0][i],tot=1ll*tot*g[0][i]%mod; } printf("%d\n%d\n",ans,tot); return 0;}
[Noip simulation competition] Women's Wear in xiaou