QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#761129 | #6613. Bitwise Exclusive-OR Sequence | gyc123 | WA | 32ms | 191176kb | C++14 | 2.8kb | 2024-11-18 21:06:22 | 2024-11-18 21:06:23 |
Judging History
answer
#include<bits/stdc++.h>
#define int long long
using namespace std;
using PII = pair<int,int>;
const int N = 30, M = 2e5 +10;
vector<int> g[N + 10][M];
int qmi(int a, int b)
{
int res = 1;
while(b)
{
if(b & 1) res = res * a;
a = a * a;
b >>= 1;
}
return res;
}
signed main()
{
cin.tie(0), ios::sync_with_stdio(false);
int n, m;
cin >> n >> m;
vector<int> u(m + 1), v(m + 1), w(m + 1);
vector<vector<PII>> g1(35);
for(int i = 1; i <= m; i++)
{
cin >> u[i] >> v[i] >> w[i];
for(int j = 0; j < 30; j++)
{
if((w[i] >> j) & 1)
{
g[j][u[i]].push_back(v[i]);
g[j][v[i]].push_back(u[i]);
}
else
{
g1[j].push_back({v[i], u[i]});
}
}
}
vector<bool> vis(n + 1, false);
vector<int> col(n + 1, 0);
int ans = 0;
auto bfs = [&](int x, int j){
queue<int> q;
vis[x] = true;
q.push(x);
col[x] = 1;
set<int> u;
while(q.size())
{
int t = q.front();
u.insert(t);
q.pop();
for(auto v : g[j][t])
{
if(!col[v])
{
col[v] = (col[t] * 2) % 3;
vis[v] = true;
q.push(v);
}
else
{
if(col[v] == col[t]) return false;
}
}
}
for(auto [x, y] : g1[j])
{
u.insert(x);
u.insert(y);
if(col[x] && col[y])
{
if(col[x] != col[y])
{
return false;
}
}
else if(!col[x] && !col[y]) col[x] = 1, col[y] = 1;
else
{
if(!col[x]) col[x] = col[y];
else col[y] = col[x];
}
}
int cnt = 0, cnt1 = 0;
for(auto x : u)
{
// if(j == 2) cerr << x << ' ' << col[x] << endl;
if(col[x] == 1) cnt++;
else cnt1++;
}
ans += min(cnt, cnt1) * qmi(2ll, j);
return true;
};
for(int j = 0; j < 30; j++)
{
for(int i = 1; i <= n; i++) vis[i] = false, col[i] = 0;
for(int i = 1; i <= n; i++)
{
if(!vis[i] && g[j][i].size())
{
bool sum = bfs(i, j);
if(!sum)
{
cout << -1 << endl;
return 0;
}
}
}
}
cout << ans << endl;
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 31ms
memory: 191164kb
input:
3 2 1 2 1 2 3 1
output:
1
result:
ok 1 number(s): "1"
Test #2:
score: 0
Accepted
time: 27ms
memory: 191152kb
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: 32ms
memory: 191176kb
input:
5 5 3 4 12 3 1 20 2 5 16 1 4 24 4 5 19
output:
-1
result:
wrong answer 1st numbers differ - expected: '58', found: '-1'