QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#153168 | #6858. Play on Tree | jrjyy# | AC ✓ | 114ms | 20796kb | C++20 | 3.4kb | 2023-08-29 15:17:46 | 2023-08-29 15:17:47 |
Judging History
answer
/* l.cpp */
#include <bits/stdc++.h>
using i64 = long long;
template <typename T> constexpr T power(T a, i64 b) {
T c{1}; for (; b; b /= 2, a *= a) if (b & 1) c *= a;
return c;
}
template <int P> struct MInt {
int x;
constexpr MInt() : x{} {}
constexpr MInt(i64 x_) : x{norm(x_ % getMod())} {}
static int Mod;
constexpr static int getMod() { return P > 0 ? P : Mod; }
constexpr static void setMod(int Mod_) { Mod = Mod_; }
constexpr int up(int x) const {
if (x < 0) x += getMod();
return x;
}
constexpr int down(int x) const {
if (x >= getMod()) x -= getMod();
return x;
}
constexpr int norm(int x) const {
return up(down(x));
}
constexpr int val() const { return x; }
explicit constexpr operator int() const { return x; }
constexpr MInt operator-() const {
MInt res; res.x = norm(getMod() - x); return res;
}
constexpr MInt inv() const {
assert(x != 0);
return power(*this, getMod() - 2);
}
constexpr MInt &operator+=(MInt rhs) & { return x = down(x + rhs.x), *this; }
constexpr MInt &operator-=(MInt rhs) & { return x = up(x - rhs.x), *this; }
constexpr MInt &operator*=(MInt rhs) & { return x = 1ll * x * rhs.x % getMod(), *this; }
constexpr MInt &operator/=(MInt rhs) & { return *this *= rhs.inv(); }
friend constexpr MInt operator+(MInt lhs, MInt rhs) { return lhs += rhs; }
friend constexpr MInt operator-(MInt lhs, MInt rhs) { return lhs -= rhs; }
friend constexpr MInt operator*(MInt lhs, MInt rhs) { return lhs *= rhs; }
friend constexpr MInt operator/(MInt lhs, MInt rhs) { return lhs /= rhs; }
friend constexpr bool operator==(MInt lhs, MInt rhs) { return lhs.val() == rhs.val(); }
friend constexpr bool operator!=(MInt lhs, MInt rhs) { return lhs.val() != rhs.val(); }
friend constexpr std::istream &operator>>(std::istream &is, MInt &a) {
i64 x = 0; is >> x, a = MInt(x); return is;
}
friend constexpr std::ostream &operator<<(std::ostream &os, const MInt &a) {
return os << a.val();
}
};
template <int P> int MInt<P>::Mod = P;
template <> int MInt<0>::Mod = 1;
template <int V, int P> constexpr MInt<P> CInv = MInt<P>(V).inv();
constexpr int P = 1000000007;
using Z = MInt<P>;
void solve() {
int n;
std::cin >> n;
std::vector<std::vector<int>> adj(n);
for (int i = 0; i < n - 1; ++i) {
int u, v;
std::cin >> u >> v;
--u, --v;
adj[u].push_back(v), adj[v].push_back(u);
}
std::vector<int> f(n), g(n);
auto dfs1 = [&](auto self, int u, int fa) -> void {
for (auto v : adj[u]) if (v != fa) {
self(self, v, u);
f[u] ^= f[v] + 1;
}
};
dfs1(dfs1, 0, -1);
auto dfs2 = [&](auto self, int u, int fa) -> void {
for (auto v : adj[u]) if (v != fa) {
g[v] = f[u] ^ (f[v] + 1) ^ (g[u] + 1);
self(self, v, u);
}
};
g[0] = -1, dfs2(dfs2, 0, -1);
Z ans = 0;
for (int i = 0; i < n; ++i) {
if ((f[i] ^ (g[i] + 1)) != 0) {
ans += 1;
}
}
std::cout << ans / n << '\n';
}
int main() {
std::cin.tie(nullptr)->sync_with_stdio(false);
int t;
std::cin >> t;
while (t--) {
solve();
}
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 114ms
memory: 20796kb
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