QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#588289#6613. Bitwise Exclusive-OR SequencezzyNorthPoleTL 914ms25928kbC++141.3kb2024-09-25 09:07:382024-09-25 09:07:39

Judging History

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

  • [2024-09-25 09:07:39]
  • 评测
  • 测评结果:TL
  • 用时:914ms
  • 内存:25928kb
  • [2024-09-25 09:07:38]
  • 提交

answer

#include <cstdio>
#include <vector>
using namespace std;
typedef long long LL;
const int N = 1e5 + 5;
struct DSU {
	int pa[N], dist[N];
	DSU(int size) {
		for (int i = 1; i <= size; ++i) pa[i] = i;
	}
	~DSU() {}

	int query(int u) {
		int cnt = 0, _u = u;
		while (pa[u] != u) cnt ^= dist[u], u = pa[u];
		dist[_u] = cnt, pa[_u] = u;
		return u;
	}

	bool merge(int u, int v, int w) {
		int pu = query(u), pv = query(v);
		if (pu == pv) {
			if (dist[u] ^ dist[v] ^ w) return false;
			return true;
		}
		pa[pu] = pv, dist[pu] = dist[u] ^ dist[v] ^ w;
		return true;
	}
};
int com[30][N], diff[30][N];
bool is_rt[N];
int main() {
	int n, m;
	scanf("%d%d", &n, &m);
	DSU dsu(n);
	for (int i = 1; i <= m; ++i) {
		int u, v, w;
		scanf("%d%d%d", &u, &v, &w);
		if (!dsu.merge(u, v, w)) {
			printf("-1\n");
			return 0;
		}
	}
	for (int u = 1; u <= n; ++u) is_rt[u] = false;
	for (int u = 1; u <= n; ++u) {
		int pu = dsu.query(u);
		is_rt[pu] = true;
		for (int step = 0; step < 30; ++step) {
			if ((dsu.dist[u] >> step) & 1) diff[step][pu]++;
			else com[step][pu]++;
		}
	}
	LL ans = 0;
	for (int u = 1; u <= n; ++u) {
		if (is_rt[u])
			for (int step = 0; step < 30; ++step)
				ans += (1ll * min(diff[step][u], com[step][u])) << step;
	}
	printf("%lld", ans);
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 16732kb

input:

3 2
1 2 1
2 3 1

output:

1

result:

ok 1 number(s): "1"

Test #2:

score: 0
Accepted
time: 0ms
memory: 2244kb

input:

3 3
1 2 1
2 3 1
1 3 1

output:

-1

result:

ok 1 number(s): "-1"

Test #3:

score: 0
Accepted
time: 0ms
memory: 16716kb

input:

5 5
3 4 12
3 1 20
2 5 16
1 4 24
4 5 19

output:

58

result:

ok 1 number(s): "58"

Test #4:

score: 0
Accepted
time: 11ms
memory: 18792kb

input:

500 124750
1 2 31473
1 3 11597
1 4 6686
1 5 1214
1 6 14442
1 7 1042
1 8 19057
1 9 22648
1 10 24461
1 11 25714
1 12 3976
1 13 31954
1 14 7384
1 15 13988
1 16 28651
1 17 31599
1 18 8786
1 19 27068
1 20 9645
1 21 28281
1 22 11681
1 23 28897
1 24 31916
1 25 10462
1 26 23973
1 27 4615
1 28 5124
1 29 2026...

output:

8041745

result:

ok 1 number(s): "8041745"

Test #5:

score: 0
Accepted
time: 16ms
memory: 20772kb

input:

500 124750
1 2 3902
1 3 9006
1 4 2914
1 5 8753
1 6 2395
1 7 21406
1 8 14552
1 9 25834
1 10 28282
1 11 9684
1 12 11347
1 13 20545
1 14 16324
1 15 16951
1 16 11594
1 17 5035
1 18 17726
1 19 831
1 20 23194
1 21 7693
1 22 6147
1 23 315
1 24 32755
1 25 17078
1 26 11348
1 27 9587
1 28 21015
1 29 881
1 30 ...

output:

7803950

result:

ok 1 number(s): "7803950"

Test #6:

score: 0
Accepted
time: 125ms
memory: 25672kb

input:

100000 200000
82262 26109 1005476194
43106 86715 475153289
59086 60577 507254441
71498 80384 186530379
99676 3003 289537598
30772 72897 345346447
12686 87447 896623879
12520 27709 26442442
82734 20830 967590473
13380 76164 927495776
25723 55377 89078582
7173 86993 669894679
37790 94846 331905713
365...

output:

52538527353096

result:

ok 1 number(s): "52538527353096"

Test #7:

score: 0
Accepted
time: 122ms
memory: 25808kb

input:

100000 200000
15687 27598 177189307
20863 37114 1037884991
93538 8500 447584932
79876 73177 560212440
90266 81435 191658398
78954 69980 476724968
3024 95419 1016359891
28005 78423 806987047
22363 37592 995962252
80788 41407 484625454
32534 34520 497714826
66857 76961 49733398
2725 7116 786428563
393...

output:

52602853327156

result:

ok 1 number(s): "52602853327156"

Test #8:

score: 0
Accepted
time: 121ms
memory: 25540kb

input:

100000 200000
14517 31233 44615804
33486 41973 1052821004
49545 79689 319778876
8546 59914 211634618
54913 52423 157522220
7407 94892 619152362
68434 82081 302860551
79458 83213 51598667
36118 558 751409357
92878 62707 87357711
71932 57530 121862749
67177 75830 953279062
38483 37005 779602421
90440 ...

output:

52429947891248

result:

ok 1 number(s): "52429947891248"

Test #9:

score: 0
Accepted
time: 106ms
memory: 16780kb

input:

100000 200000
11656 60762 1
16805 66929 17
55148 55195 2
29841 9092 11
31848 48612 12
26261 82795 11
65162 78347 23
15597 88601 21
92692 7801 12
50087 67030 22
75497 23748 1
59304 62297 29
56011 68857 4
11540 9395 11
36408 13733 10
29267 965 15
45943 65984 29
6240 25729 29
64043 76902 7
78121 89050 ...

output:

1514667

result:

ok 1 number(s): "1514667"

Test #10:

score: 0
Accepted
time: 117ms
memory: 17328kb

input:

100000 200000
19078 1796 29
89160 3396 12
14155 57229 5
36362 16264 4
34378 35576 20
89020 76502 18
85772 59833 30
47358 3841 7
15219 48259 1
4778 42876 11
57340 52164 30
98346 7446 16
74577 91274 18
74128 7433 22
24777 57648 5
77806 76383 14
18741 73911 9
98620 83468 18
21676 45630 20
16077 32480 6...

output:

1515438

result:

ok 1 number(s): "1515438"

Test #11:

score: 0
Accepted
time: 8ms
memory: 25928kb

input:

100000 50000
1 2 1073741823
3 4 1073741823
5 6 1073741823
7 8 1073741823
9 10 1073741823
11 12 1073741823
13 14 1073741823
15 16 1073741823
17 18 1073741823
19 20 1073741823
21 22 1073741823
23 24 1073741823
25 26 1073741823
27 28 1073741823
29 30 1073741823
31 32 1073741823
33 34 1073741823
35 36 1...

output:

53687091150000

result:

ok 1 number(s): "53687091150000"

Test #12:

score: 0
Accepted
time: 16ms
memory: 22988kb

input:

100000 99999
2 1 1073741823
3 2 1073741823
4 3 1073741823
5 4 1073741823
6 5 1073741823
7 6 1073741823
8 7 1073741823
9 8 1073741823
10 9 1073741823
11 10 1073741823
12 11 1073741823
13 12 1073741823
14 13 1073741823
15 14 1073741823
16 15 1073741823
17 16 1073741823
18 17 1073741823
19 18 107374182...

output:

53687091150000

result:

ok 1 number(s): "53687091150000"

Test #13:

score: 0
Accepted
time: 14ms
memory: 2392kb

input:

100000 100000
2 1 1073741823
3 2 1073741823
4 3 1073741823
5 4 1073741823
6 5 1073741823
7 6 1073741823
8 7 1073741823
9 8 1073741823
10 9 1073741823
11 10 1073741823
12 11 1073741823
13 12 1073741823
14 13 1073741823
15 14 1073741823
16 15 1073741823
17 16 1073741823
18 17 1073741823
19 18 10737418...

output:

-1

result:

ok 1 number(s): "-1"

Test #14:

score: 0
Accepted
time: 7ms
memory: 16180kb

input:

100000 0

output:

0

result:

ok 1 number(s): "0"

Test #15:

score: 0
Accepted
time: 0ms
memory: 14672kb

input:

1 0

output:

0

result:

ok 1 number(s): "0"

Test #16:

score: 0
Accepted
time: 110ms
memory: 14372kb

input:

100000 200000
296 44153 0
5858 67253 0
77986 84024 0
13079 66431 0
48865 55236 0
53962 75116 0
74193 31717 0
4779 8496 0
91472 57204 0
96393 66037 0
26299 81546 0
66322 26724 0
74011 22225 0
79847 33417 0
32615 58874 0
49670 29377 0
44193 62155 0
51975 75204 0
33100 57873 0
3394 94539 0
86646 85744 ...

output:

0

result:

ok 1 number(s): "0"

Test #17:

score: 0
Accepted
time: 0ms
memory: 14616kb

input:

2000 1000
1488 921 0
772 1279 0
656 1845 0
602 1013 0
1967 1649 0
15 946 0
1983 775 0
1110 1254 0
1198 28 0
846 1721 0
1739 777 0
1675 225 0
315 1063 0
1156 46 0
430 1401 0
1105 812 0
1807 166 0
1808 1006 0
632 1396 0
1233 1928 0
1740 791 0
166 1713 0
1272 170 0
1365 790 0
792 1550 0
876 359 0
327 1...

output:

0

result:

ok 1 number(s): "0"

Test #18:

score: 0
Accepted
time: 0ms
memory: 14656kb

input:

1 1
1 1 0

output:

0

result:

ok 1 number(s): "0"

Test #19:

score: 0
Accepted
time: 0ms
memory: 2332kb

input:

2 1
1 1 1

output:

-1

result:

ok 1 number(s): "-1"

Test #20:

score: 0
Accepted
time: 0ms
memory: 2352kb

input:

6500 192169
513 3871 8192
5819 4477 16
4415 3714 4096
4914 2493 32
3298 3772 256
5107 2443 0
27 114 0
958 798 524288
1488 5881 128
4023 1900 4096
1002 55 8388608
1784 3295 1
243 81 1048576
6333 1096 512
1673 2991 8192
1345 810 256
1755 1669 2097152
4470 1610 0
2030 1942 268435456
2970 3535 0
1528 14...

output:

-1

result:

ok 1 number(s): "-1"

Test #21:

score: 0
Accepted
time: 0ms
memory: 2304kb

input:

5000 147225
3872 3673 8192
4417 1256 16
564 4916 1024
3774 3013 2097152
115 16 0
105 959 4194304
4025 917 0
204 1002 262144
3296 1747 1048576
242 95 0
1636 2992 0
803 1345 65536
1112 1756 131072
2439 4471 0
1070 2031 67108864
3022 3536 0
1529 607 0
4728 3673 262144
3343 2291 8192
1269 135 524288
124...

output:

-1

result:

ok 1 number(s): "-1"

Test #22:

score: 0
Accepted
time: 0ms
memory: 2332kb

input:

1000 27940
566 561 512
315 270 0
320 177 0
513 120 16384
536 276 8388608
1 6 33562624
314 466 0
710 317 16384
105 115 0
173 960 512
380 38 0
84 403 16777216
722 410 0
136 422 0
243 230 0
663 843 0
561 788 32768
227 109 0
404 649 2
30 20 1
928 269 134217728
584 942 0
218 184 524288
302 417 2097152
38...

output:

-1

result:

ok 1 number(s): "-1"

Test #23:

score: 0
Accepted
time: 3ms
memory: 2256kb

input:

100000 199960
41459 56770 1
85904 40011 0
61166 64908 1
64605 72364 1
88723 98329 1
29588 55303 0
42294 75252 2
1192 53 1
9597 13382 1
608 86828 1
46783 59046 2
14022 310 0
11637 48162 0
2952 2186 1
96200 78489 2
31770 93593 0
43628 22964 1
19107 5707 0
25204 21480 2
65721 31640 0
29298 29234 0
5175...

output:

-1

result:

ok 1 number(s): "-1"

Test #24:

score: 0
Accepted
time: 1ms
memory: 2332kb

input:

20000 199537
9470 11393 256
972 17223 0
13022 6958 0
14513 11300 128
15072 19708 0
11100 507 0
15092 3699 0
68 260 1
72 2708 0
17407 16478 0
11849 1074 512
2836 881 512
2757 9671 512
559 615 2
1135 19283 0
18761 16209 0
1948 8763 64
946 3854 0
4687 5075 0
13185 2418 0
5895 1383 0
10389 8149 1
3787 4...

output:

-1

result:

ok 1 number(s): "-1"

Test #25:

score: 0
Accepted
time: 0ms
memory: 22836kb

input:

31 30
1 2 1
2 3 2
3 4 4
4 5 8
5 6 16
6 7 32
7 8 64
8 9 128
9 10 256
10 11 512
11 12 1024
12 13 2048
13 14 4096
14 15 8192
15 16 16384
16 17 32768
17 18 65536
18 19 131072
19 20 262144
20 21 524288
21 22 1048576
22 23 2097152
23 24 4194304
24 25 8388608
25 26 16777216
26 27 33554432
27 28 67108864
28...

output:

2147385345

result:

ok 1 number(s): "2147385345"

Test #26:

score: 0
Accepted
time: 914ms
memory: 22848kb

input:

30001 30000
1 2 1
2 3 2
3 4 4
4 5 8
5 6 16
6 7 32
7 8 64
8 9 128
9 10 256
10 11 512
11 12 1024
12 13 2048
13 14 4096
14 15 8192
15 16 16384
16 17 32768
17 18 65536
18 19 131072
19 20 262144
20 21 524288
21 22 1048576
22 23 2097152
23 24 4194304
24 25 8388608
25 26 16777216
26 27 33554432
27 28 67108...

output:

16106127345000

result:

ok 1 number(s): "16106127345000"

Test #27:

score: -100
Time Limit Exceeded

input:

100000 99999
1 2 1
2 3 2
3 4 4
4 5 8
5 6 16
6 7 32
7 8 64
8 9 128
9 10 256
10 11 512
11 12 1024
12 13 2048
13 14 4096
14 15 8192
15 16 16384
16 17 32768
17 18 65536
18 19 131072
19 20 262144
20 21 524288
21 22 1048576
22 23 2097152
23 24 4194304
24 25 8388608
25 26 16777216
26 27 33554432
27 28 6710...

output:


result: