QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#577422 | #6892. WO MEI K | JZYZ# | AC ✓ | 725ms | 165384kb | C++14 | 1.8kb | 2024-09-20 11:17:54 | 2024-09-20 11:17:59 |
Judging History
answer
#include<bits/stdc++.h>
#define MP make_pair
#define pb push_back
using namespace std;
const int N = 2e5 + 10;
typedef long long LL;
typedef pair< int, int > PII;
const LL mod = 998244353;
LL ans;
int T, col[N], n, sz[N], lst[N];
int del[N], del1[N];
vector< PII > E[N];
stack< int > cor[N];
inline LL Pow(LL x, LL y) {
LL res = 1LL, k = x;
while(y) {
if(y & 1) res = res * k % mod;
y >>= 1;
k = k * k % mod;
}
return res;
}
void dfs0(int x, int fa) {
sz[x] = 1;
if(x == 1) {
for(int i = 1; i <= n; i ++ ) cor[i].push(x);
}
for(auto edge : E[x]) {
int v = edge.first, ide = edge.second;
if(v == fa) continue;
int c = col[ide];
int tp = cor[c].top();
cor[c].push(v);
dfs0(v, x);
sz[x] += sz[v];
del[tp] += sz[v];
if(tp == 1) del1[c] += sz[v];
lst[v] = tp;
cor[c].pop();
}
if(x == 1) {
for(int i = 1; i <= n; i ++ ) cor[i].pop();
}
}
void dfs(int x, int fa) {
for(auto edge : E[x]) {
int v = edge.first, ide = edge.second;
if(v == fa) continue;
int c = col[ide];
int cnt1 = sz[v] - del[v];
int u = lst[v];
int cnt2 = sz[u] - (u != 1 ? del[u] : del1[c]);
ans = (ans + 1LL * cnt1 * cnt2 % mod) % mod;
dfs(v, x);
}
}
int main() {
scanf("%d", &T);
while(T -- ) {
scanf("%d", &n);
for(int i = 1; i <= n; i ++ ) {
vector< PII > tmp; swap(tmp, E[i]);
del[i] = 0; del1[i] = 0;
}
for(int i = 1; i < n; i ++ ) {
int u, v, c; scanf("%d%d%d", &u, &v, &c);
E[u].pb(MP(v, i));
E[v].pb(MP(u, i));
col[i] = c;
}
ans = 0;
dfs0(1, 0);
dfs(1, 0);
LL res = 0;
for(int i = 1; i <= n; i ++ ) {
LL p = 1LL * (i - 1) * i % mod;
LL inv = 1LL * (n - 1) * n % mod;
inv = Pow(inv, mod - 2LL);
p = p * inv % mod;
res ^= (ans * p % mod);
}
printf("%lld\n", res);
}
return 0;
}
/*
2
2
1 2 1
3
1 2 1
1 3 2
*/
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 725ms
memory: 165384kb
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