QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#588106#6613. Bitwise Exclusive-OR SequencezzyNorthPoleTL 41ms5268kbC++142.1kb2024-09-25 01:21:142024-09-25 01:21:15

Judging History

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

  • [2024-09-25 01:21:15]
  • 评测
  • 测评结果:TL
  • 用时:41ms
  • 内存:5268kb
  • [2024-09-25 01:21:14]
  • 提交

answer

#include <cstdio>
#include <vector>
#include <iostream>
using namespace std;
struct DSU {
	vector <int> pa, h, diff, com, dist, vis;

	DSU(int size) {
		pa.resize(size + 1, 0);
		h.resize(size + 1, 0);
		dist.resize(size + 1, 0);
		com.resize(size + 1, 1);
		diff.resize(size + 1, 0);
		vis.resize(size + 1, 0);
		for (int i = 1; i <= size; ++i) pa[i] = i;
	}
	~DSU() {}

	void init() {
		for (int i = 1; i < pa.size(); ++i) pa[i] = i, h[i] = 0, dist[i] = 0, com[i] = 1, diff[i] = 0, vis[i] = 0;
	}

	int qry(int x) {
		int cnt = 0, x_ = x;
		while (pa[x] != x) cnt += dist[x], x = pa[x];
		dist[x_] = cnt, pa[x_] = x;
		return x;
	}

	bool merge(int x, int y, int op) {
		int rtx = qry(x), rty = qry(y);
		if (rtx == rty) {
			if ((dist[x] + dist[y]) % 2 == op) return true;
			// cerr << x << ' ' << dist[x] << ' ' << y << ' ' << dist[y] << ' ' << op << '\n';
			return false;
		}
		if (h[rtx] > h[rty]) swap(rtx, rty);
		pa[rtx] = rty;
		h[rtx] = max(h[rtx], h[rty] + 1);
		if ((dist[x] + dist[y]) % 2 == op) dist[rtx] = 0, com[rty] += com[rtx], diff[rty] += diff[rtx];
		else dist[rtx] = 1, diff[rty] += com[rtx], com[rty] += diff[rtx];
		return true;
	}
};
const int M = 2e5 + 5;
struct Data {
	int u, v, w;
} a[M];
typedef long long LL;
LL solve(DSU &dsu, int step, int m) {
	dsu.init();
	for (int i = 1; i <= m; ++i) {
		int u = a[i].u, v = a[i].v, w = a[i].w;
		int flag = 1;
		if (w & (1 << step)) flag = 1;
		else flag = 0;
		if (!dsu.merge(u, v, flag)) {
			// cerr << step << '\n';
			return -1;
		}
	}
	LL ans = 0;
	for (int i = 1; i < dsu.pa.size(); ++i) {
		int rti = dsu.qry(i);
		if (dsu.vis[rti]) continue;
		else dsu.vis[rti] = 1, ans += min(dsu.com[rti], dsu.diff[rti]);
	}
	return (ans << step);
}
int main() {
	int n, m;
	scanf("%d%d", &n, &m);
	DSU dsu(n);
	for (int i = 1; i <= m; ++i) {
		scanf("%d%d%d", &a[i].u, &a[i].v, &a[i].w);
	}

	LL ans = 0;
	for (int i = 0; i < 30; ++i) {
		LL res = solve(dsu, i, m);
		if (res == -1) {
			printf("-1\n");
			return 0;
		}
		ans += res;
	}

	printf("%lld\n", ans);
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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: 3732kb

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: 3792kb

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: 41ms
memory: 5268kb

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: 40ms
memory: 5260kb

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: -100
Time Limit Exceeded

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:


result: