結果
| 問題 |
No.1420 国勢調査 (Easy)
|
| コンテスト | |
| ユーザー |
snow39
|
| 提出日時 | 2021-03-05 22:15:47 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 265 ms / 2,000 ms |
| コード長 | 2,240 bytes |
| コンパイル時間 | 1,252 ms |
| コンパイル使用メモリ | 113,884 KB |
| 最終ジャッジ日時 | 2025-01-19 11:17:08 |
|
ジャッジサーバーID (参考情報) |
judge3 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 2 |
| other | AC * 30 |
ソースコード
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <cmath>
#include <map>
#include <queue>
#include <iomanip>
#include <set>
#include <tuple>
#define mkp make_pair
#define mkt make_tuple
#define rep(i,n) for(int i = 0; i < (n); ++i)
#define all(v) v.begin(),v.end()
using namespace std;
typedef long long ll;
const ll MOD=1e9+7;
template<class T> void chmin(T &a,const T &b){if(a>b) a=b;}
template<class T> void chmax(T &a,const T &b){if(a<b) a=b;}
class DisjointSet{
public:
int N;
vector<int> rank,p;
vector<int> sz;
DisjointSet(){}
DisjointSet(int n):N(n),rank(n),p(n),sz(n){
for(int i=0;i<N;i++) makeSet(i);
}
void makeSet(int x){
p[x]=x;
rank[x]=0;
sz[x]=1;
}
bool same(int x,int y){
return leader(x)==leader(y);
}
void unite(int x,int y){
if(same(x,y)) return;
link(leader(x),leader(y));
}
void link(int x,int y){
if(rank[x]>rank[y]) swap(x,y);
p[x]=y;
sz[y]+=sz[x];
if(rank[x]==rank[y]){
++rank[y];
}
}
int leader(int x){
if(x!=p[x]) p[x]=leader(p[x]);
return p[x];
}
int size(int x){
return sz[leader(x)];
}
};
const int L=30;
int main(){
cin.tie(0);
ios::sync_with_stdio(false);
int N,M;
cin>>N>>M;
DisjointSet us(2*N*L+2);
rep(q,M){
int a,b;
cin>>a>>b;
a--;b--;
int value;
cin>>value;
rep(k,L){
int lef=2*(a*L+k);
int rig=2*(b*L+k);
if(value>>k&1) rig^=1;
us.unite(lef,rig);
us.unite(lef^1,rig^1);
}
}
rep(i,N) rep(k,L) if(us.same(2*(i*L+k),2*(i*L+k)+1)){
cout<<-1<<endl;
return 0;
}
int zero=2*N*L;
int one=zero+1;
rep(i,N){
rep(k,L){
int lef=2*(i*L+k);
int rig=lef^1;
if(us.same(zero,lef)){
us.unite(one,rig);
}else if(us.same(one,lef)){
us.unite(zero,rig);
}else if(us.same(zero,rig)){
us.unite(one,lef);
}else if(us.same(one,rig)){
us.unite(zero,lef);
}else{
us.unite(lef,one);
us.unite(rig,zero);
}
}
}
vector<int> ans(N,0);
rep(i,N) rep(k,L){
int now=2*(i*L+k);
if(us.same(now^1,one)) ans[i]+=(1<<k);
}
for(auto n:ans) cout<<n<<"\n";
return 0;
}
snow39