1 solutions
-
0
/* * __----~~~~~~~~~~~------___ * . . ~~//====...... __--~ ~~ * -. \_|// |||\\ ~~~~~~::::... /~ * ___-==_ _-~o~ \/ ||| \\ _/~~- * __---~~~.==~||\=_ -_--~/_-~|- |\\ \\ _/~ * _-~~ .=~ | \\-_ '-~7 /- / || \ / * .~ .~ | \\ -_ / /- / || \ / * / ____ / | \\ ~-_/ /|- _/ .|| \ / * |~~ ~~|--~~~~--_ \ ~==-/ | \~--===~~ .\ * ' ~-| /| |-~\~~ __--~~ * |-~~-_/ | | ~\_ _-~ /\ * / \ \__ \/~ \__ * _--~ _/ | .-~~____--~-/ ~~==. * ((->/~ '.|||' -_| ~~-/ , . _|| * -_ ~\ ~~---l__i__i__i--~~_/ * _-~-__ ~) \--______________--~~ * //.-~~~-~_--~- |-------~~~~~~~~ * //.-~~~--\ * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ * * 神兽保佑 永无BUG */ /* * @Description: I want to be the weakest vegetable in the world! * @Author: I.Z. */ #include<bits/stdc++.h> using namespace std; #define MOD 998244353 #define int long long #define pii pair<int,int> #define all(v) v.begin(),v.end() #define pb push_back #define REP(i,b,e) for(int i=(b);i<(int)(e);++i) #define over(x) {cout<<(x)<<endl;return;} #define lowbit(x) ((x)&(-(x))) #define cntbit(x) __builtin_popcount(x) #define deal(v) sort(all(v));v.erase(unique(v.begin(),v.end()),v.end()) #define lbound(v,x) lower_bound(all(v),x)-v.begin() mt19937 sd(random_device{}()); int qpow(int a,int b,int m=MOD,int res=1){ a%=m; while(b>0)res=(b&1)?(res*a%m):(res),a=a*a%m,b>>=1; return res; } int exgcd(int x,int y,int &a,int &b){ if(y==0){ a=1;b=0; return x; } int d=exgcd(y,x%y,a,b); int z=b; b=a-b*(x/y); a=z; return d; } int _x_,_y_; inline int inverse(int x,int y=MOD){ exgcd(x,y,_x_,_y_); return (_x_+y)%y; } int fac[2000005],inv[2000005]; void init(int n){ fac[0]=inv[0]=1; REP(i,1,n+1)fac[i]=fac[i-1]*i%MOD; inv[n]=qpow(fac[n],MOD-2); for(int i=n-1;i>=1;--i)inv[i]=inv[i+1]*(i+1)%MOD; } int binom(int x,int y){ return x<y||y<0? 0:fac[x]*inv[y]%MOD*inv[x-y]%MOD; } int n; vector<int>v[500005]; int sz[500005]; bool f[500005],g[500005];//子树内/外是否存在完美匹配 int dp[500005];//如果是奇数,表示空一个点的方案数;如果是偶数,表示把根留下然后空一个点的方案数 int dq[500005]; void dfs(int x,int pre){ f[x]=1;sz[x]=1; int cnt=0; for(auto i:v[x])if(i!=pre){ dfs(i,x); sz[x]+=sz[i]; if(!f[i])f[x]=0; cnt+=sz[i]&1; } if(cnt>1)f[x]=0; if(sz[x]&1){ if(f[x])dp[x]=1; int cntf=0; for(auto i:v[x])if(i!=pre)cntf+=!f[i]; if(cnt==2){ vector<int>t; for(auto i:v[x])if(i!=pre&&sz[i]%2)t.pb(i); if((!f[t[0]])+(!f[t[1]])==cntf){ if(f[t[0]])dp[x]+=dp[t[1]]; if(f[t[1]])dp[x]+=dp[t[0]]; } } for(auto i:v[x])if(i!=pre){ if(sz[i]%2==0){ if(cnt||cntf!=(!f[i]))continue; dp[x]+=dp[i]; } } }else{ //必须是一个奇数加上全偶数 if(cnt!=1)return; for(auto i:v[x])if(i!=pre){ if(sz[i]%2==0&&!f[i])return dp[x]=0,void(); else if(sz[i]&1)dp[x]=dp[i]; } } } void dfs2(int x,int pre){ int cnt1=0,cnt2=0; for(auto i:v[x])if(i!=pre){if(!f[i])++cnt1;} for(auto i:v[x])if(i!=pre){if(sz[i]&1)++cnt2;} if(x){ if(!g[x])++cnt1; if(sz[x]&1)++cnt2; } for(auto i:v[x])if(i!=pre){ if(!f[i])--cnt1; if(sz[i]&1)--cnt2; if(!cnt1&&cnt2<=1)g[i]=1; else g[i]=0; dfs2(i,x); if(!f[i])++cnt1; if(sz[i]&1)++cnt2; } } void dfs3(int x,int pre){ vector<int>odd,even; int cnt1=0,cnt2=0,sum=0; for(auto i:v[x])if(i!=pre){ if(!f[i])++cnt1,even.pb(i); else sum+=dp[i]; } for(auto i:v[x])if(i!=pre){ if(sz[i]&1)++cnt2,odd.pb(i); } if(x){ if(!g[x])++cnt1,even.pb(pre); else sum+=dq[x]; if(sz[x]&1)++cnt2; odd.pb(pre); } for(auto i:v[x])if(i!=pre){ if(!f[i])--cnt1; if(sz[i]&1)--cnt2; if(sz[i]&1){ // cout<<'!'<<i<<' '<<cnt1<<' '<<cnt2<<' '<<sum<<endl; dq[i]=0; if(g[i])dq[i]=1; if(cnt2==2){ vector<pii>t; for(auto j:odd)if(j!=i){ if(j==pre)t.pb({g[x],dq[x]}); else t.pb({f[j],dp[j]}); } if(cnt1==(!t[0].first)+(!t[1].first)){ if(t[0].first)dq[i]+=t[1].second; if(t[1].first)dq[i]+=t[0].second; } } if(!cnt2){ if(cnt1>1); else if(!cnt1)dq[i]+=sum-(f[i]? dp[i]:0); else{ for(auto j:even)if(j!=i){ if(j==pre)dq[i]+=dq[x]; else dq[i]+=dp[j]; } } } }else{ if(cnt2!=1)dq[i]=0; else{ int spe=odd[0]; if(cnt1-(!(spe==pre? g[x]:f[spe])))dq[i]=0; else dq[i]=spe==pre? dq[x]:dp[spe]; } } dfs3(i,x); if(!f[i])++cnt1; if(sz[i]&1)++cnt2; } } void Main() { cin>>n; if(n&1)over(0) REP(i,0,n)v[i].clear(),f[i]=g[i]=dp[i]=dq[i]=0; REP(i,1,n){ int x,y; cin>>x>>y; --x,--y; v[x].pb(y);v[y].pb(x); } dfs(0,-1);dfs2(0,-1);dfs3(0,-1); // REP(i,0,n)cout<<f[i]; // cout<<endl; // REP(i,1,n)cout<<g[i]; // cout<<endl; // REP(i,0,n)cout<<dp[i]<<' '; // cout<<endl; // REP(i,1,n)cout<<dq[i]<<' '; // cout<<endl; int ans=0; REP(i,1,n)if(sz[i]%2==0&&f[i]&&g[i])ans+=sz[i]*(n-sz[i]); REP(i,1,n)if(sz[i]&1)ans+=dp[i]*dq[i]; over(ans) } void TC() { int tc=1; while(tc--){ Main(); cout.flush(); } } signed main() { freopen("match.in","r",stdin); freopen("match.out","w",stdout); return cin.tie(0),cout.tie(0),ios::sync_with_stdio(0),TC(),0; } /* 1. CLEAR the arrays (ESPECIALLY multitests) 2. DELETE useless output */
- 1
Information
- ID
- 3098
- Time
- 2000ms
- Memory
- 512MiB
- Difficulty
- 10
- Tags
- (None)
- # Submissions
- 1
- Accepted
- 1
- Uploaded By