QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#737826#6613. Bitwise Exclusive-OR SequencezhoudashuaiML 1ms7768kbC++141.8kb2024-11-12 16:56:312024-11-12 16:56:34

Judging History

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

  • [2024-11-12 16:56:34]
  • 评测
  • 测评结果:ML
  • 用时:1ms
  • 内存:7768kb
  • [2024-11-12 16:56:31]
  • 提交

answer

#include<iostream>
#include<string.h>
#include<algorithm>
#include<math.h>
#include<vector>
#include<map>
#include<queue>
#include<set> 
#include<stack>
#include<bitset>
#include<unordered_map>
#include<iomanip>
#include<cassert>
#include<array>
#define fi first
#define se second
#define endl '\n'
#define pb push_back
#define ep emplace_back 
#define debug(x) cout<<x<<'\n'
#define RES cout<<res<<'\n'
#define ANS cout<<ans<<'\n'
#define YES	cout<<"YES"<<'\n'
#define NO cout<<"NO"<<'\n'
#define Yes	cout<<"Yes"<<'\n'
#define No cout<<"No"<<'\n'
#define me(a,x) memset(a,x,sizeof(a))
#define L(i,j,k) for(int i=(j);i<=(k);++i)
#define R(i,j,k) for(int i=(j);i>=(k);--i)
#define r23 cout<<233<<endl;return;
using namespace std;
typedef long long ll;
typedef pair<int,int> PII;
typedef pair<PII,int> PIII;
typedef unsigned long long ull;
const int N=1e5+5,P=131,mod=998244353;
int n,m;
bool flag=1;
int e[N<<2],ne[N<<2],idx,h[N],w[N<<2];
int cnt[33][2],val[N],vis[N];
void add(int a,int b,int c){
	e[idx]=b,ne[idx]=h[a],w[idx]=c,h[a]=idx++;
} 
void dfs(int u,int f){
	if(!flag)return;
	vis[u]=1;
	R(i,30,0){
		cnt[i][(val[u]>>i)&1]++;
	}
	for(int i=h[u];~i;i=ne[i]){
		int j=e[i];
		if(j==f)continue;
		if(vis[j]&&(val[j]^val[u])!=w[i]){
			flag=0;return;
		}else{
			val[j]=val[u]^w[i];
			dfs(j,u);
		}
	}
}
void solve(){
	me(h,-1);
	cin>>n>>m;
	L(i,1,m){
		int a,b,c;cin>>a>>b>>c;
		add(a,b,c);
		add(b,a,c);
	}
	ll res=0;
	L(i,1,n){
		if(flag&&!vis[i]){
			me(cnt,0);
			dfs(i,0);
			R(j,30,0){
				res+=1ll*min(cnt[j][0],cnt[j][1])*(1<<j);
			}
		}
	}
//	L(i,1,n)cout<<i<<' '<<val[i]<<endl;
	cout<<(flag==1?res:-1);
}
int main(){
	ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
	int T=1;
	//cin>>T;
	while(T--){
		solve();
	}
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 7768kb

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: 5652kb

input:

3 3
1 2 1
2 3 1
1 3 1

output:

-1

result:

ok 1 number(s): "-1"

Test #3:

score: -100
Memory Limit Exceeded

input:

5 5
3 4 12
3 1 20
2 5 16
1 4 24
4 5 19

output:


result: