QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#729793#6613. Bitwise Exclusive-OR Sequencenorth_hTL 103ms10272kbC++173.8kb2024-11-09 17:50:282024-11-09 17:51:45

Judging History

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

  • [2024-11-09 17:51:45]
  • 评测
  • 测评结果:TL
  • 用时:103ms
  • 内存:10272kb
  • [2024-11-09 17:50:28]
  • 提交

answer

//#pragma GCC optimize(3,"Ofast","inline")
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define endl "\n"

typedef struct node{
    int u,v,w;
}node;
int n,m;
vector<node> edge;
void solve(){
    cin>>n>>m;
    for(int i=0;i<m;i++){
        int u,v,w; cin>>u>>v>>w;
        edge.emplace_back(node{u,v,w});
    }
    int ans=0;
    for(int j=0;j<30;j++){
        vector<pair<int,int>> vct[n+5];
        for(int i=0;i<m;i++){
            int u=edge[i].u,v=edge[i].v,w=edge[i].w;
            if((w>>j)&1) vct[u].emplace_back(v,1),vct[v].emplace_back(u,1);
            else vct[u].emplace_back(v,0),vct[v].emplace_back(u,0);
        }
        vector<bool> vis0(n+5,false);
        vector<bool> vis1(n+5,false);
        vector<int> col0(n+5);
        vector<int> col1(n+5);
        int res=0;
        for(int i=1;i<=n;i++){
            int res0=0,res1=0;
            if(!vis0[i]){
                vis0[i]=vis1[i]=true;
                col0[i]=0,col1[i]=1;
                queue<int> que1;
                que1.emplace(i);
                res1++;
                while(!que1.empty()){
                    int from=que1.front();
                    int color=col1[from];
                    que1.pop();
                    for(auto v:vct[from]){
                        if(!vis1[v.first]){
                            if(v.second==1){  //异色
                                que1.emplace(v.first);
                                if((color^1)==1) res1++;
                                col1[v.first]=color^1;
                                vis1[v.first]=true;
                            }
                            else{  //同色
                                que1.emplace(v.first);
                                if(color==1) res1++;
                                col1[v.first]=color;
                                vis1[v.first]=true;
                            }
                        }
                        else{
                            if(v.second==1&&col1[v.first]==color){ cout<<"-1"; return; }
                            else if(v.second==0&&col1[v.first]!=color){ cout<<"-1"; return; }
                        }
                    }
                }
                queue<int> que0;
                que0.emplace(i);
                while(!que0.empty()){
                    int from=que0.front();
                    int color=col0[from];
                    que0.pop();
                    for(auto v:vct[from]){
                        if(!vis0[v.first]){
                            if(v.second==1){  //异色
                                que0.emplace(v.first);
                                if((color^1)==1) res0++;
                                col0[v.first]=color^1;
                                vis0[v.first]=true;
                            }
                            else{  //同色
                                que0.emplace(v.first);
                                if(color==1) res0++;
                                col0[v.first]=color;
                                vis0[v.first]=true;
                            }
                        }
                        else{
                            if(v.second==1&&col0[v.first]==color){ cout<<"-1"; return; }
                            else if(v.second==0&&col0[v.first]!=color){ cout<<"-1"; return; }
                        }
                    }
                }
            }
            res+=min(res0,res1);
        }
//        cout<<j<<":"<<endl;
//        cout<<cnt0<<" "<<cnt1<<endl;
        ans+=res*(1ll<<j);
    }
    cout<<ans;
}

int32_t main() {
    ios::sync_with_stdio(false),cin.tie(nullptr),cout.tie(nullptr);
    int t=1;
//    cin>>t;
    while(t--){
        solve();
    }
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

input:

3 2
1 2 1
2 3 1

output:

1

result:

ok 1 number(s): "1"

Test #2:

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

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

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: 0
Accepted
time: 103ms
memory: 10204kb

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:

8041745

result:

ok 1 number(s): "8041745"

Test #5:

score: 0
Accepted
time: 94ms
memory: 10272kb

input:

500 124750
1 2 3902
1 3 9006
1 4 2914
1 5 8753
1 6 2395
1 7 21406
1 8 14552
1 9 25834
1 10 28282
1 11 9684
1 12 11347
1 13 20545
1 14 16324
1 15 16951
1 16 11594
1 17 5035
1 18 17726
1 19 831
1 20 23194
1 21 7693
1 22 6147
1 23 315
1 24 32755
1 25 17078
1 26 11348
1 27 9587
1 28 21015
1 29 881
1 30 ...

output:

7803950

result:

ok 1 number(s): "7803950"

Test #6:

score: -100
Time Limit Exceeded

input:

100000 200000
82262 26109 1005476194
43106 86715 475153289
59086 60577 507254441
71498 80384 186530379
99676 3003 289537598
30772 72897 345346447
12686 87447 896623879
12520 27709 26442442
82734 20830 967590473
13380 76164 927495776
25723 55377 89078582
7173 86993 669894679
37790 94846 331905713
365...

output:


result: