Submission #63552686
Source Code Expand
Copy
#include<iostream>#include<vector>using namespace std;#define ll long longint fa[200010];ll r[200010];int find_anc(int u){if(fa[u]==u){r[u]=0;return u;}int tmp=fa[u];fa[u]=find_anc(fa[u]);r[u]^=r[tmp];return fa[u];}vector<int>g[200010];ll a[200010];
#include<iostream> #include<vector> using namespace std; #define ll long long int fa[200010]; ll r[200010]; int find_anc(int u){ if(fa[u]==u){ r[u]=0; return u; } int tmp=fa[u]; fa[u]=find_anc(fa[u]); r[u]^=r[tmp]; return fa[u]; } vector<int>g[200010]; ll a[200010]; int main(){ iostream::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n,m; cin>>n>>m; for(int i=1;i<=n;i++)fa[i]=i; for(int i=0;i<m;i++){ int u,v; ll w; cin>>u>>v>>w; int fu=find_anc(u),fv=find_anc(v); if(fu==fv){ // cout<<u<<" "<<v<<" "<<r[u]<<" "<<r[v]<<" "<<(r[u]^r[v])<<endl; if((r[u]^r[v])!=w)cout<<-1,exit(0); }else{ fa[fv]=fu; // u->fu = r[u] // v->fv = r[v] // u->v = w // fu->fv = fu->u->v->fv = r[u]^w^r[v] r[fv]=r[u]^r[v]^w; } } for(int i=1;i<=n;i++){ find_anc(i); if(fa[i]!=i)g[fa[i]].push_back(i); } ll ans=0; for(int i=1;i<=n;i++){ if(g[i].empty())continue; for(int j=31;j+1;j--){ ll sum1=0,sum2=1ll<<j; for(auto id:g[i]){ sum1+=(1ll<<j)&r[id]; } for(auto id:g[i]){ sum2+=(1ll<<j)&(r[id]^(1ll<<j)); } if(sum1<sum2){ for(auto id:g[i]){ a[id]+=(1ll<<j)&r[id]; } }else{ for(auto id:g[i]){ a[id]+=(1ll<<j)&(r[id]^(1ll<<j)); } a[i]+=(1ll<<j); } } } for(int i=1;i<=n;i++)cout<<a[i]<<" "; }
Submission Info
Submission Time | |
---|---|
Task | E - Min of Restricted Sum |
User | n_ni |
Language | C++ 20 (gcc 12.2) |
Score | 450 |
Code Size | 1379 Byte |
Status | AC |
Exec Time | 52 ms |
Memory | 15176 KB |
Compile Error
Main.cpp: In function ‘int main()’: Main.cpp:51:12: warning: unused variable ‘ans’ [-Wunused-variable] 51 | ll ans=0; | ^~~
Judge Result
Set Name | Sample | All | ||||
---|---|---|---|---|---|---|
Score / Max Score | 0 / 0 | 450 / 450 | ||||
Status |
|
|
Set Name | Test Cases |
---|---|
Sample | 00_sample_00.txt, 00_sample_01.txt, 00_sample_02.txt |
All | 00_sample_00.txt, 00_sample_01.txt, 00_sample_02.txt, 01_handmade_00.txt, 01_handmade_01.txt, 01_handmade_02.txt, 01_handmade_03.txt, 01_handmade_04.txt, 01_handmade_05.txt, 01_handmade_06.txt, 01_handmade_07.txt, 01_handmade_08.txt, 01_handmade_09.txt, 02_random_00.txt, 02_random_01.txt, 02_random_02.txt, 02_random_03.txt, 02_random_04.txt, 02_random_05.txt, 02_random_06.txt, 02_random_07.txt, 02_random_08.txt, 02_random_09.txt, 02_random_10.txt, 02_random_11.txt, 02_random_12.txt, 02_random_13.txt, 02_random_14.txt, 02_random_15.txt, 02_random_16.txt, 02_random_17.txt, 02_random_18.txt, 02_random_19.txt, 02_random_20.txt, 02_random_21.txt, 02_random_22.txt, 02_random_23.txt, 02_random_24.txt |
Case Name | Status | Exec Time | Memory |
---|---|---|---|
00_sample_00.txt | AC | 2 ms | 3456 KB |
00_sample_01.txt | AC | 2 ms | 3408 KB |
00_sample_02.txt | AC | 2 ms | 3492 KB |
01_handmade_00.txt | AC | 2 ms | 3428 KB |
01_handmade_01.txt | AC | 11 ms | 5732 KB |
01_handmade_02.txt | AC | 2 ms | 3424 KB |
01_handmade_03.txt | AC | 2 ms | 4236 KB |
01_handmade_04.txt | AC | 1 ms | 3388 KB |
01_handmade_05.txt | AC | 2 ms | 4200 KB |
01_handmade_06.txt | AC | 11 ms | 5708 KB |
01_handmade_07.txt | AC | 40 ms | 15176 KB |
01_handmade_08.txt | AC | 36 ms | 15132 KB |
01_handmade_09.txt | AC | 16 ms | 3496 KB |
02_random_00.txt | AC | 27 ms | 6436 KB |
02_random_01.txt | AC | 38 ms | 12940 KB |
02_random_02.txt | AC | 45 ms | 10460 KB |
02_random_03.txt | AC | 51 ms | 13200 KB |
02_random_04.txt | AC | 20 ms | 11332 KB |
02_random_05.txt | AC | 39 ms | 13156 KB |
02_random_06.txt | AC | 33 ms | 7968 KB |
02_random_07.txt | AC | 52 ms | 13280 KB |
02_random_08.txt | AC | 27 ms | 10660 KB |
02_random_09.txt | AC | 28 ms | 12760 KB |
02_random_10.txt | AC | 2 ms | 3544 KB |
02_random_11.txt | AC | 30 ms | 12948 KB |
02_random_12.txt | AC | 4 ms | 3864 KB |
02_random_13.txt | AC | 20 ms | 5644 KB |
02_random_14.txt | AC | 6 ms | 4068 KB |
02_random_15.txt | AC | 13 ms | 5912 KB |
02_random_16.txt | AC | 3 ms | 3832 KB |
02_random_17.txt | AC | 5 ms | 5796 KB |
02_random_18.txt | AC | 25 ms | 11296 KB |
02_random_19.txt | AC | 49 ms | 13148 KB |
02_random_20.txt | AC | 8 ms | 4032 KB |
02_random_21.txt | AC | 30 ms | 5584 KB |
02_random_22.txt | AC | 21 ms | 4776 KB |
02_random_23.txt | AC | 32 ms | 5680 KB |
02_random_24.txt | AC | 15 ms | 3444 KB |