QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#211815 | #6600. Whispers of the Old Gods | chitoge | RE | 0ms | 0kb | C++20 | 1.6kb | 2023-10-12 21:31:14 | 2023-10-12 21:31:14 |
answer
#include<bits/stdc++.h>
using namespace std;
constexpr int N = 31 , M = 1e5 + 5;
vector<vector<pair<int , int> > > G[N];
vector<bool> vis;
int color[M];
bool fail = false;
void dfs(int idx , int now , int k) {
if(vis[now]) return;
vis[now] = true;
color[now] = k;
for(auto [it , w] : G[idx][now]) {
if(vis[it] && color[it] != color[now]) continue;
if(vis[it] && w == 1 && color[it] == color[now]) {
fail = true;
return;
}
if(vis[it] && w == 0 && color[it] != color[now]) {
fail = true;
return;
}
dfs(idx , it , w == 0 ? k : (k ^ 1));
}
}
int main (){
ios::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
int n , m;
cin >> n >> m;
vis.assign(n + 1 , false);
for(int i = 0 ; i < N ; ++i) {
G[i].assign(n + 1 , vector<pair<int , int>>{});
}
while(m--) {
int u , v , w;
cin >> u >> v >> w;
for(int i = 0 ; i < N ; ++i) {
G[i][u].emplace_back(v , (w >> i) & 1);
G[i][v].emplace_back(u , (w >> i) & 1);
}
}
auto count = [&]() {
int t = 0;
for(int i = 1 ; i <= n ; ++i) {
if(color[i] == 1) ++t;
}
return t;
};
long long ans = 0;
for(int i = 0 ; i < N ; ++i) {
if(G[i].empty()) continue;
int x = -1;
for(auto &it : G[i]) {
if(!it.empty()) {
x = it[0].first;
break;
}
}
if(x == -1) continue;
dfs(i , x , 0);
int c = count();
for(int i = 1 ; i <= n ; ++i) color[i] = 0;
vis.assign(n + 1 , false);
dfs(i , x , 1);
c = min(c , count());
ans += (1ll << i) * c;
for(int i = 1 ; i <= n ; ++i) color[i] = 0;
vis.assign(n + 1 , false);
}
cout << (fail ? -1 : ans) << '\n';
}
详细
Test #1:
score: 0
Runtime Error
input:
(5|6+)[12]3+ 4334