QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#211815#6600. Whispers of the Old GodschitogeRE 0ms0kbC++201.6kb2023-10-12 21:31:142023-10-12 21:31:14

Judging History

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

  • [2023-10-12 21:31:14]
  • 评测
  • 测评结果:RE
  • 用时:0ms
  • 内存:0kb
  • [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';
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Runtime Error

input:

(5|6+)[12]3+
4334

output:


result: