QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#179211 | #6892. WO MEI K | PPP# | AC ✓ | 465ms | 41672kb | C++17 | 2.1kb | 2023-09-14 19:38:45 | 2023-09-14 19:38:46 |
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;
const int N = 200500, mod = 998244353;
int n;
vector<pair<int, int> > g[N];
vector<int> ga[N];
int tin[N], tout[N], timer;
int sz[N];
int pr[N], csz[N];
int st[N], stn;
ll ans;
void dfs(int v, int p) {
tin[v] = ++timer;
sz[v] = 1;
for (auto it: g[v]) {
int to = it.first;
int w = it.second;
if (to == p)
continue;
dfs(to, v);
sz[v] += sz[to];
ga[w].push_back(to);
}
tout[v] = timer;
}
bool upper(int v, int u) {
return tin[v] <= tin[u] && tin[u] <= tout[v];
}
void f(vector<int> &a) {
sort(a.begin(), a.end(), [](int v, int u) {
return tin[v] < tin[u];
});
stn = 0;
st[stn++] = 0;
csz[0] = n;
for (auto v: a) {
csz[v] = sz[v];
while (!upper(st[stn - 1], v))
stn--;
csz[st[stn - 1]] -= sz[v];
pr[v] = st[stn - 1];
st[stn++] = v;
}
for (auto v: a) {
ans += 1ll * csz[v] * csz[pr[v]] % mod;
ans %= mod;
}
}
void solve() {
cin >> n;
for (int i = 0; i < n; i++) {
g[i].clear();
ga[i].clear();
}
for (int i = 0; i < n - 1; i++) {
int v, u, w;
cin >> v >> u >> w;
v--, u--, w--;
g[v].push_back({u, w});
g[u].push_back({v, w});
}
dfs(0, 0);
ans = 0;
for (int i = 0; i < n; i++)
f(ga[i]);
ans %= mod;
ans = ans * 2 % mod;
while(ans % n != 0)
ans += mod;
ans /= n;
while(ans % (n - 1) != 0)
ans += mod;
ans /= n - 1;
ll s = 0;
for (int i = 2; i <= n; i++)
s ^= 1ll * i * (i - 1) / 2 % mod * ans % mod;
cout << s << "\n";
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
#ifdef DEBUG
freopen("input.txt", "r", stdin);
#endif
int tst;
cin >> tst;
while (tst--)
solve();
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 465ms
memory: 41672kb
input:
106 200000 2 1 70358 3 1 94059 4 3 194779 5 3 147526 6 1 128796 7 4 141976 8 4 69430 9 3 132781 10 6 82526 11 5 93708 12 8 10824 13 8 159324 14 10 95645 15 9 83437 16 10 61897 17 13 119054 18 14 116823 19 18 149469 20 10 72020 21 2 95763 22 1 194299 23 1 84812 24 1 20933 25 7 71421 26 9 111015 27 16...
output:
479799996 559987406 173574248 414521015 435350101 700635296 260375015 89074409 377135544 408258434 551176217 27254026 306297792 254632387 447509594 817903044 963868466 827085336 1067266388 422475867 31622164 843934379 105738540 870679243 513141034 752944662 798135577 305400376 650171300 371654894 81...
result:
ok 106 lines