結果

問題 No.1420 国勢調査 (Easy)
ユーザー snow39snow39
提出日時 2021-03-05 22:15:47
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 286 ms / 2,000 ms
コード長 2,240 bytes
コンパイル時間 1,329 ms
コンパイル使用メモリ 116,980 KB
実行使用メモリ 73,988 KB
最終ジャッジ日時 2023-07-29 02:11:18
合計ジャッジ時間 8,667 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 87 ms
10,120 KB
testcase_03 AC 85 ms
10,164 KB
testcase_04 AC 86 ms
10,120 KB
testcase_05 AC 87 ms
10,120 KB
testcase_06 AC 86 ms
10,168 KB
testcase_07 AC 78 ms
10,064 KB
testcase_08 AC 80 ms
10,168 KB
testcase_09 AC 78 ms
10,100 KB
testcase_10 AC 79 ms
10,060 KB
testcase_11 AC 80 ms
10,108 KB
testcase_12 AC 50 ms
73,224 KB
testcase_13 AC 134 ms
73,984 KB
testcase_14 AC 49 ms
73,288 KB
testcase_15 AC 134 ms
73,856 KB
testcase_16 AC 133 ms
73,724 KB
testcase_17 AC 157 ms
73,988 KB
testcase_18 AC 159 ms
73,732 KB
testcase_19 AC 159 ms
73,748 KB
testcase_20 AC 134 ms
73,872 KB
testcase_21 AC 132 ms
73,816 KB
testcase_22 AC 280 ms
73,860 KB
testcase_23 AC 282 ms
73,748 KB
testcase_24 AC 280 ms
73,796 KB
testcase_25 AC 286 ms
73,744 KB
testcase_26 AC 283 ms
73,800 KB
testcase_27 AC 169 ms
73,332 KB
testcase_28 AC 168 ms
73,332 KB
testcase_29 AC 167 ms
73,276 KB
testcase_30 AC 169 ms
73,260 KB
testcase_31 AC 168 ms
73,416 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#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;
}
0