QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#607057 | #6613. Bitwise Exclusive-OR Sequence | wsxcb# | WA | 1ms | 3620kb | C++17 | 1.3kb | 2024-10-03 13:48:07 | 2024-10-03 13:48:07 |
Judging History
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;
if (x != w) {
cout << "-1\n";
return ;
}
} else {
fa[v].fi = fau;
fa[v].se = (w ^ valu ^ valv);
}
}
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: 1ms
memory: 3616kb
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: 3620kb
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: 3556kb
input:
5 5 3 4 12 3 1 20 2 5 16 1 4 24 4 5 19
output:
39
result:
wrong answer 1st numbers differ - expected: '58', found: '39'