QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#431835 | #6858. Play on Tree | heaksicn | AC ✓ | 87ms | 27244kb | C++17 | 1.2kb | 2024-06-06 10:00:44 | 2024-06-06 10:00:45 |
Judging History
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;
}
详细
Test #1:
score: 100
Accepted
time: 87ms
memory: 27244kb
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