QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#189448 | #6858. Play on Tree | spoonjunxi# | AC ✓ | 150ms | 19868kb | C++14 | 2.0kb | 2023-09-27 15:10:34 | 2023-09-27 15:10:34 |
Judging History
answer
#include <bits/stdc++.h>
#define rep(i, j, k) for (int i = (j); i <= (k); i++)
#define per(i, j, k) for (int i = (k); i >= (j); i--)
#define pb push_back
#define eb emplace_back
#define SZ(x) ((int)(x).size())
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
using VI = std::vector<int>;
using ll = long long;
using PII = std::pair<int, int>;
using db = double;
using namespace std;
const int mod = 1000000007;
inline void add(int &x, int y, int md = mod) { x += y; if (x >= md) x -= md; }
inline void sub(int &x, int y, int md = mod) { x -= y; if (x < 0) x += md; }
inline void mul(int &x, int y, int md = mod) { x = (int)(1ll * x * y % mod); if (x < 0) x += mod; }
template<typename T>
inline int powmod(int a, T e, int md = mod) {
int res = 1;
a %= md;
assert(e >= 0);
for (; e; e>>=1, mul(a, a, md))
if (e & 1)
mul(res, a, md);
return res;
}
inline int inv(int x, int md = mod) {
x %= md;
if (x < 0) x += md;
int y = md, u = 0, v = 1;
while (x) {
int w = y / x;
y -= w * x; swap(x, y);
u -= w * v; swap(u, v);
}
assert(y == 1);
if (u < 0) u += md;
return u;
}
const int N = 201000;
int n, sg[N], sg2[N];
VI adj[N];
void dfs1(int u, int f = 0) {
sg[u] = 0;
for (auto v : adj[u]) if (v != f) {
dfs1(v, u);
sg[u] ^= sg[v] + 1;
}
}
void changeroot(int u, int f = 0) {
for (auto v : adj[u]) if (v != f) {
sg2[v] = sg[v] ^ ((sg2[u] ^ (sg[v] + 1)) + 1);
changeroot(v, u);
}
}
void solve() {
cin >> n;
rep(i, 1, n) adj[i].clear();
rep(i, 1, n - 1) {
int u, v;
cin >> u >> v;
adj[u].pb(v);
adj[v].pb(u);
}
dfs1(1);
sg2[1] = sg[1];
changeroot(1);
int ans = 0;
rep(i, 1, n) if (sg2[i]) ans += 1;
cout << 1ll * ans * inv(n) % mod << "\n";
}
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
int t;
cin >> t;
while (t--) {
solve();
}
}
详细
Test #1:
score: 100
Accepted
time: 150ms
memory: 19868kb
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