QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#787793 | #6613. Bitwise Exclusive-OR Sequence | Loxilante | WA | 1ms | 7724kb | C++20 | 1.7kb | 2024-11-27 14:39:09 | 2024-11-27 14:39:09 |
Judging History
answer
#define F_C
#include <bits/stdc++.h>
#define rep(i, l, r) for(int i = l; i < r; i++)
#define hrp(i, l, r) for(int i = l; i <= r; i++)
#define rev(i, r, l) for(int i = r; i >= l; i--)
#define int ll
using namespace std;
typedef long long ll;
template<typename tn = int> tn next(void) { tn k; cin>>k; return k; }
#ifndef LOCAL
#define D(...) 0
#endif
const int U = 3e5;
int head[U], nxt[U], to[U], val[U], tot;
int asu[U];
vector<int> cpn;
bool vis[U];
bool dfs(int e)
{
if (vis[e]) return 1;
vis[e] = 1;
for(int i = head[e]; i; i = nxt[i])
{
int y = to[i], z = val[i];
if (vis[y]) { if ((asu[y]^z) != asu[e]) return 0; }
else
{
asu[y] = asu[e]^z;
cpn.push_back(asu[y]);
if (!dfs(y)) return 0;
}
}
return 1;
}
void add(int x, int y, int z)
{
to[++tot] = y; val[tot] = z; nxt[tot] = head[x]; head[x] = tot;
}
signed main(void)
{
#ifdef LOCAL
// freopen("C:\\Users\\Loxil\\Desktop\\IN.txt", "r", stdin);
// freopen("C:\\Users\\Loxil\\Desktop\\OUT.txt", "w", stdout);
#endif
ios::sync_with_stdio(false);
cin.tie(0);
int n, m;
cin>>n>>m;
rep(i, 0, m) { int u, v, w; cin>>u>>v>>w; add(u, v, w); add(v, u, w); }
int ans = 0;
hrp(i, 1, n)
{
if (vis[i]) continue;
cpn.clear();
if (!dfs(i)) return cout<<-1<<endl, 0;
rep(b, 0, 30)
{
int cnt1 = 0;
for(auto v: cpn) cnt1 += (v>>b)&1;
ans += (1<<b)*min(cnt1, (int)cpn.size()-cnt1);
}
}
cout<<ans<<endl;
return 0;
}
/*
3 2
1 2 1
2 3 1
*/
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 7724kb
input:
3 2 1 2 1 2 3 1
output:
1
result:
ok 1 number(s): "1"
Test #2:
score: 0
Accepted
time: 1ms
memory: 7652kb
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: 1ms
memory: 7652kb
input:
5 5 3 4 12 3 1 20 2 5 16 1 4 24 4 5 19
output:
34
result:
wrong answer 1st numbers differ - expected: '58', found: '34'