QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#607031#6613. Bitwise Exclusive-OR Sequencewsxcb#WA 0ms3868kbC++171.3kb2024-10-03 13:42:092024-10-03 13:42:10

Judging History

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

  • [2024-10-03 13:42:10]
  • 评测
  • 测评结果:WA
  • 用时:0ms
  • 内存:3868kb
  • [2024-10-03 13:42:09]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define pb push_back
#define fi first
#define se second
const int N = 2e5 + 5;
pair<int, int>fa[N];

pair<int, int>find(int x) {
	auto [fax, val1] = fa[x];
	if (fax == x)
		return fa[x];
	auto [fay, val2] = find(fax);
	fa[x] = {fay, val2 ^ val1};
	return fa[x];
}

void solve() {
	int n, m;
	cin >> n >> m;
	for (int i = 1; i <= n; i++) {
		fa[i].fi = i;
		fa[i].se = 0;
	}
	while (m--) {
		int u, v, w;
		cin >> u >> v >> w;
		auto [fau, valu] = find(u);
		auto [fav, valv] = find(v);
		if (fau == fav) {
			int x = valu ^ valv;
			//cout << u << ' ' << v << ' ' << x << '\n';
			if (x != w) {
				cout << "-1\n";
				return ;
			}
		} else {
			fa[v].fi = fau;
			fa[v].se = w ^ valu;
		}

	}
	map<int, vector<int>>mp;
	for (int i = 1; i <= n; i++) {
		auto [fai, vali] = find(i);
		mp[fai].pb(vali);
	}
	ll ans = 0;
	for (auto &[x, e] : mp) {
		for (int i = 0; i < 30; i++) {
			int c1 = 0, c2 = 0;
			for (auto t : e) {
				if (t >> i & 1)
					c1++;
				else
					c2++;
			}
			c1 = min(c1, c2);
			ans += 1ll * (1ll << i) * c1;
		}
	}
	cout << ans << '\n';
}

int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	int t = 1;
	//cin>>t;
	while (t--)
		solve();
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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

input:

3 3
1 2 1
2 3 1
1 3 1

output:

-1

result:

ok 1 number(s): "-1"

Test #3:

score: -100
Wrong Answer
time: 0ms
memory: 3632kb

input:

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

output:

55

result:

wrong answer 1st numbers differ - expected: '58', found: '55'