QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#282542#6858. Play on TreezlxFTHAC ✓129ms20392kbC++171.3kb2023-12-12 12:46:332023-12-12 12:46:33

Judging History

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

  • [2023-12-12 12:46:33]
  • 评测
  • 测评结果:AC
  • 用时:129ms
  • 内存:20392kb
  • [2023-12-12 12:46:33]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;

using i64 = long long;

constexpr int P = 1e9 + 7;
inline int qp(int a, int b = P - 2) {
  int res = 1;
  while (b) {
    if (b & 1) {
      res = i64(res) * a % P;
    }
    a = i64(a) * a % P;
    b /= 2;
  }
  return res;
}

void solve() {
  int n;
  cin >> n;
  vector<vector<int>> G(n);
  for (int i = 0; i < n - 1; ++i) {
    int u, v;
    cin >> u >> v;
    u--;
    v--;
    G[u].push_back(v);
    G[v].push_back(u);
  }
  vector<int> f(n);
  auto dfs1 = [&](auto self, int u, int fa) -> void {
    f[u] = 0;
    for (auto v : G[u]) {
      if (v == fa) {
        continue;
      }
      self(self, v, u);
      f[u] ^= (f[v] + 1);
    }
  };
  int cnt = 0;
  auto dfs2 = [&](auto self, int u, int fa) -> void {
    if (f[u]) {
      cnt++;
    }
    for (auto v : G[u]) {
      if (v == fa) {
        continue;
      }
      int fu = f[u];
      int fv = f[v];
      f[u] ^= (f[v] + 1);
      f[v] ^= (f[u] + 1);
      self(self, v, u);
      f[u] = fu;
      f[v] = fv;
    }
  };
  dfs1(dfs1, 0, -1);
  dfs2(dfs2, 0, -1);
  cout << i64(cnt) * qp(n) % P << "\n";
}

int main() {
  cin.tie(0)->sync_with_stdio(0);
  int t;
  cin >> t;
  while (t--) {
    solve();
  }
  return 0;
}

詳細信息

Test #1:

score: 100
Accepted
time: 129ms
memory: 20392kb

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