QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#165637 | #6858. Play on Tree | PPP# | AC ✓ | 134ms | 27544kb | C++17 | 2.7kb | 2023-09-05 20:20:01 | 2023-09-05 20:20:02 |
Judging History
answer
#ifdef DEBUG
#define _GLIBCXX_DEBUG
#endif
//#pragma GCC optimize("O3")
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define X first
#define Y second
const ll mod = 1000000007;
ll pew(ll a, ll b)
{
ll res = 1;
while (b>0)
{
if (b&1) res = res*a%mod;
b >>= 1;
a = a*a%mod;
}
return res;
}
ll t[1200000], cnt[1200000], del[1200000], F[1200000];
pair<ll,ll> mn[1200000];
vector<int> a;
void push(int v)
{
if (F[v]==1)
{
}
}
void build(int v, int v2, int tl, int tr)
{
t[v2] = cnt[v2] = 0;
del[v] = del[v2] = 0;
F[v] = F[v2] = 0;
if (tl==tr)
{
mn[v] = {a[tl],tl};
t[v] = a[tl];
cnt[v] = 1;
return;
}
int tm = (tl+tr)/2;
build(v*2,v2*2,tl,tm);
build(v*2+1,v2*2+1,tm+1,tr);
}
pair<ll,ll> get(int v, int tl, int tr, int l, int r)
{
if (tl>=l and tr<=r) return mn[v];
}
void solve()
{
int n;
cin >> n;
vector<vector<pair<int,int>>> g(n);
vector<pair<int,int>> e;
for (ll i=0;i+1<n;i++)
{
int u, v;
cin >> u >> v;
u--, v--;
e.push_back({u,v});
g[u].push_back({v,i*2});
g[v].push_back({u,i*2+1});
e.push_back({v,u});
}
int N = n*2-2;
vector<int> dp(N,-1), d(n), S(n);
for (int i=0;i<n;i++) d[i] = g[i].size();
vector<int> q;
for (int i=0;i<N;i++)
{
int u = e[i].X, v = e[i].Y;
if (d[u]==1)
{
dp[i] = 0;
q.push_back(i);
}
}
vector<int> was(N);
for (int w=0;w<q.size();w++)
{
int p = q[w];
int u = e[p].X, v = e[p].Y;
S[v] ^= dp[p]+1;
was[p] = 1;
d[v]--;
if (d[v]==1)
{
for (auto x: g[v])
{
int p2 = x.Y;
if (was[p2^1]==0)
{
dp[p2] = S[v];
q.push_back(p2);
}
}
}
if (d[v]==0)
{
for (auto x: g[v])
{
int p2 = x.Y;
if (dp[p2]==-1)
{
dp[p2] = S[v]^(dp[p2^1]+1);
q.push_back(p2);
}
}
}
}
ll A = 0;
for (int i=0;i<n;i++)
{
if (S[i]!=0) A++;
}
A = A*pew(n,mod-2)%mod;
cout << A << "\n";
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
#ifdef DEBUG
//freopen("input.txt", "r", stdin);
#endif
int T;
cin >> T;
while (T--) solve();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 134ms
memory: 27544kb
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