QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#431372#6858. Play on Treeucup-team3591#AC ✓124ms27184kbC++171.2kb2024-06-05 13:58:322024-06-05 13:58:32

Judging History

你现在查看的是最新测评结果

  • [2024-06-05 13:58:32]
  • 评测
  • 测评结果:AC
  • 用时:124ms
  • 内存:27184kb
  • [2024-06-05 13:58:32]
  • 提交

answer

//Man always remember love because of romance only!
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int mod=1e9+7;
inline int read(){
    int X=0,w=0; char ch=0;
    while(!isdigit(ch)) {w|=ch=='-';ch=getchar();}
    while(isdigit(ch)) X=(X<<3)+(X<<1)+(ch^48),ch=getchar();
    return w?-X:X;
}
inline void write(int x){
	if(x<0) putchar('-'),x=-x;
    if(x>9) write(x/10);
    putchar(x%10+'0');
}
int qpow(int x,int y){
	int res=1;
	while(y){
		if(y&1) res=res*x%mod;
		x=x*x%mod;
		y>>=1;
	}
	return res;
}
int sg[200001];
vector<int> e[200001];
void dfs(int x,int fa){
	sg[x]=0;
	for(int v:e[x]){
		if(v==fa) continue;
		dfs(v,x);
		sg[x]^=(sg[v]+1);
	}
}
int dp[200001];
void dfs2(int x,int fa){
	for(int v:e[x]){
		if(v==fa) continue;
		dp[v]=sg[v]^((dp[x]^(sg[v]+1))+1);
		dfs2(v,x);
	}
}
signed main(){
	int T=read();
	while(T--){
		int n=read();
		for(int i=1;i<=n;i++) e[i].clear(),sg[i]=0,dp[i]=0;
		for(int i=1;i<n;i++){
			int u=read(),v=read();
			e[u].push_back(v);
			e[v].push_back(u);
		}
		dfs(1,0);
		dp[1]=sg[1];
		dfs2(1,0);
		int ans=0;
		for(int i=1;i<=n;i++) ans+=dp[i]!=0;
		write(ans*qpow(n,mod-2)%mod);
		putchar('\n');
	}
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 124ms
memory: 27184kb

input:

3
199999
144802 35753
35753 9646
9646 148183
9646 167003
9646 199831
9646 193968
9646 182279
9646 185547
9646 106896
9646 196577
9646 150627
9646 147736
9646 180949
9646 179482
9646 196999
9646 191636
9646 184859
9646 173080
9646 155500
9646 191682
9646 169619
9646 191285
9646 106525
9646 187324
964...

output:

256641286
960854812
384481201

result:

ok 3 lines