QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#725258#6613. Bitwise Exclusive-OR Sequence777WA 17ms7336kbC++201.8kb2024-11-08 16:51:002024-11-08 16:51:00

Judging History

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

  • [2024-11-08 16:51:00]
  • 评测
  • 测评结果:WA
  • 用时:17ms
  • 内存:7336kb
  • [2024-11-08 16:51:00]
  • 提交

answer

#pragma GCC optimize(2)
#pragma GCC optimize(3)
#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
#define debug(x) cout << #x << "=" << x << endl
//using i128 = __int128_t;
#define int long long
typedef pair<int,int> PII;
typedef long long ll;
inline void read(int &x){
    char ch=getchar();int f=1;x=0;
    while(!isdigit(ch) && ch^'-') ch=getchar();
    if(ch=='-') f=-1,ch=getchar();
    while(isdigit(ch)) x=x*10+ch-'0',ch=getchar();
    x*=f;
}
void write(int x){
    if(x<0)
    putchar('-'),x=-x;
    if(x>9)
    write(x/10);
    putchar(x%10+'0');
    return;
}
int n,m;
void rCL(){
    cin>>n>>m;
    vector<vector<PII>>e(n+1);

    for(int i=1;i<=m;i++){
    	int u,v,w;
    	cin>>u>>v>>w;
    	e[u].push_back({v,w});
    	e[v].push_back({u,w});
    }
    vector<int>a(n+1);
    vector<int>c1(32),c0(32);
    vector<int>vis(n+1);

    auto dfs=[&](auto dfs,int u,int fa)->bool{
    	vis[u]=1;
    	//cout<<u<<endl;
    	for(int i=0;i<32;i++){
    		if(a[u]>>i&1ll)c1[i]++;
    		else c0[i]++;
    	}

    	for(auto [v,w]:e[u]){
    		if(v==fa)continue;
    		//cout<<"U"<<u<<' '<<v<<' '<<w<<endl;
    		if(!vis[v]){
    			a[v]=w^a[u];
    			return dfs(dfs,v,u);
    		}else{
    			if(a[v]^a[u]!=w)return false;
    		}
    	}
    	return true;
    };
    int res=0;
    for(int i=1;i<=n;i++){

    	if(!vis[i]){
    		c1.assign(32,0);
    		c0.assign(32,0);

    		if(!dfs(dfs,i,0)){

    			cout<<-1<<endl;
    			return;
    		}else{

    			for(int j=0;j<32;j++){
    				res=res+min(c0[j],c1[j])*(1ll<<j);
    			}
    		}
    	}
    }
    cout<<res<<endl;
}
signed main(){
    ios::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);
    int O_o=1;
    while(O_o--){
        rCL();
    }
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 3520kb

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

input:

3 3
1 2 1
2 3 1
1 3 1

output:

-1

result:

ok 1 number(s): "-1"

Test #3:

score: 0
Accepted
time: 0ms
memory: 3784kb

input:

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

output:

58

result:

ok 1 number(s): "58"

Test #4:

score: -100
Wrong Answer
time: 17ms
memory: 7336kb

input:

500 124750
1 2 31473
1 3 11597
1 4 6686
1 5 1214
1 6 14442
1 7 1042
1 8 19057
1 9 22648
1 10 24461
1 11 25714
1 12 3976
1 13 31954
1 14 7384
1 15 13988
1 16 28651
1 17 31599
1 18 8786
1 19 27068
1 20 9645
1 21 28281
1 22 11681
1 23 28897
1 24 31916
1 25 10462
1 26 23973
1 27 4615
1 28 5124
1 29 2026...

output:

-1

result:

wrong answer 1st numbers differ - expected: '8041745', found: '-1'