QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#577412#6892. WO MEI KJZYZ#WA 465ms165488kbC++141.8kb2024-09-20 11:12:342024-09-20 11:12:34

Judging History

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

  • [2024-09-20 11:12:34]
  • 评测
  • 测评结果:WA
  • 用时:465ms
  • 内存:165488kb
  • [2024-09-20 11:12:34]
  • 提交

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;
		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;
	}
}
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
*/

詳細信息

Test #1:

score: 0
Wrong Answer
time: 465ms
memory: 165488kb

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:

864913694
864074521
1051045451
350351673
695769049
695769049
108372071
36622915
104090230
9630
9630
9630
176943456
311402552
969717898
787044115
999264205
567258039
770462517
294201352
1059740296
741518085
745626207
10891147
547471014
352044693
9630
126611187
678497150
9630
9630
406862062
408620727
...

result:

wrong answer 1st lines differ - expected: '479799996', found: '864913694'