結果

問題 No.1420 国勢調査 (Easy)
ユーザー logxlogx
提出日時 2021-01-29 20:45:44
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 41 ms / 2,000 ms
コード長 7,162 bytes
コンパイル時間 2,535 ms
コンパイル使用メモリ 205,524 KB
実行使用メモリ 4,504 KB
最終ジャッジ日時 2023-09-09 14:06:13
合計ジャッジ時間 8,330 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 29 ms
4,376 KB
testcase_03 AC 29 ms
4,380 KB
testcase_04 AC 29 ms
4,376 KB
testcase_05 AC 29 ms
4,376 KB
testcase_06 AC 29 ms
4,380 KB
testcase_07 AC 29 ms
4,380 KB
testcase_08 AC 28 ms
4,384 KB
testcase_09 AC 29 ms
4,376 KB
testcase_10 AC 28 ms
4,376 KB
testcase_11 AC 28 ms
4,376 KB
testcase_12 AC 5 ms
4,376 KB
testcase_13 AC 11 ms
4,412 KB
testcase_14 AC 5 ms
4,380 KB
testcase_15 AC 11 ms
4,408 KB
testcase_16 AC 11 ms
4,376 KB
testcase_17 AC 11 ms
4,380 KB
testcase_18 AC 11 ms
4,380 KB
testcase_19 AC 11 ms
4,376 KB
testcase_20 AC 11 ms
4,380 KB
testcase_21 AC 11 ms
4,380 KB
testcase_22 AC 40 ms
4,376 KB
testcase_23 AC 41 ms
4,504 KB
testcase_24 AC 40 ms
4,376 KB
testcase_25 AC 41 ms
4,380 KB
testcase_26 AC 41 ms
4,380 KB
testcase_27 AC 32 ms
4,380 KB
testcase_28 AC 32 ms
4,376 KB
testcase_29 AC 33 ms
4,376 KB
testcase_30 AC 32 ms
4,380 KB
testcase_31 AC 32 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
//#include <atcoder/all>
//using namespace atcoder;

/*---------macro---------*/
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define rep2(i, s, n) for (int i = s; i < (int)(n); ++i)
#define Clear(a) a = decltype(a)()
#define all(a) begin(a),end(a)
#define rall(a) rbegin(a),rend(a)
#define bit(i,j) ((i>>j)&1)

/*---------type/const---------*/
typedef long long ll;
typedef unsigned long long ull;
typedef string::const_iterator state; //構文解析
const ll big=1000000007;
//const ll big=998244353;
const double PI=acos(-1);
const double EPS=1e-8;//適宜変える
const int dx[4]={1,0,-1,0};
const int dy[4]={0,1,0,-1};
const char newl='\n';
struct{
    constexpr operator int(){return int(1e9)+1;}
    constexpr operator ll(){return ll(1e18)+1;}
    constexpr auto operator-(){
        struct{
            constexpr operator int(){return -int(1e9)-1;}
            constexpr operator ll(){return -ll(1e18)-1;}
        }_ret;
        return _ret;
    }
}inf;

/*---------debug用関数---------*/
#ifdef LOGX
#define _GLIBCXX_DEBUG
#define dbg(x) cout << #x << ':',prt(x);
template<typename T> inline void prt(T a_);
template<typename T> inline void _prt(T a_);
template<typename A,typename B> inline void _prt(map<A,B> a_);
template<typename A,typename B> inline void _prt(unordered_map<A,B> a_);
template<typename T> inline void _prt(set<T> a_);
template<typename T> inline void _prt(unordered_set<T> a_);
template<typename A,typename B> inline void _prt(pair<A,B> a_);
template<typename T> inline void _prt(vector<T> &a_);
template<typename T> inline void _prt(vector<vector<T>> &a_);
//_prtでは改行しない.
inline void _prt(bool a_){if(a_)cout<<1;else cout<<0;}
inline void _prt(int a_){if(a_>=(int)inf)cout << "inf";else if(a_<=-(int)inf)cout << "-inf";else cout << a_;}
inline void _prt(long long a_){if(a_>=(ll)inf)cout << "INF";else if(a_<=-(ll)inf)cout << "-INF";else cout << a_;}
#ifdef ATCODER_MODINT_HPP
template<int MOD>inline void _prt(static_modint<MOD> x){cout << x.val();}
template<int MOD>inline void _prt(dynamic_modint<MOD> x){cout << x.val();}
#endif
template<typename T> inline void _prt(T a_){cout << a_;}
template<typename A,typename B>
inline void _prt(map<A,B> a_){cout<<'{';auto it=a_.begin();while(it!=a_.end()){_prt((*it).first);cout<<':';_prt((*(it++)).second);cout<<(it==a_.end() ? "":",");}cout<<'}';}
template<typename A,typename B>
inline void _prt(unordered_map<A,B> a_){cout<<'{';auto it=a_.begin();while(it!=a_.end()){_prt((*it).first);cout<<':';_prt((*(it++)).second);cout<<(it==a_.end() ? "":",");}cout<<'}';}
template<typename T>
inline void _prt(set<T> a_){cout<<'{';auto it=a_.begin();while(it!=a_.end()){_prt(*it++);cout<<(it==a_.end() ? "":",");}cout<<'}';}
template<typename T>
inline void _prt(unordered_set<T> a_){cout<<'{';auto it=a_.begin();while(it!=a_.end()){_prt(*it++);cout<<(it==a_.end() ? "":",");}cout<<'}';}
template<typename A,typename B>
inline void _prt(pair<A,B> a_){cout << '(';_prt(a_.first);cout << ',';_prt(a_.second);cout << ')';}
template<typename T>
inline void _prt(vector<T> &a_){cout<<'{';for(int i=0;i<(int)a_.size();i++){_prt(a_[i]);if(i+1<(int)a_.size())cout<<',';}cout<<'}';}
template<typename T>
inline void _prt(vector<vector<T>> &a_){cout<<"{\n";for(int i=0;i<(int)a_.size();i++){_prt(a_[i]);if(i+1<(int)a_.size())cout<<",\n";else cout<<"\n}";}}
template<typename T> inline void prt(T a_){_prt(a_);cout<<'\n';}
#else
#define dbg(x) ;
#define prt(x) ;
#define _prt(x) ;
#endif

/*---------function---------*/
template<typename T,typename U> T max(const T x,const U y){if(x>y)return x;else return y;}
template<typename T,typename U> T min(const T x,const U y){if(x<y)return x;else return y;}
template<typename T> T max(const vector<T> &a){T ans=a[0];for(T elem:a){ans=max(ans,elem);}return ans;}
template<typename T> T min(const vector<T> &a){T ans=a[0];for(T elem:a){ans=min(ans,elem);}return ans;}
template<typename T,typename U> bool chmin(T &a,const U b){if(a>b){a=b;return true;}return false;}
template<typename T,typename U> bool chmax(T &a,const U b){if(a<b){a=b;return true;}return false;}
template<typename T,typename U>
T expm(T x,U y,const ll mod=big){
    T res=1;
    while(y){
        if(y&1)(res*=x)%=mod;
        (x*=x)%=mod;
        y>>=1;
    }
    return res;
}
template<typename T,typename U>
T exp(T x,U y){
    T res=1;
    while(y){
        if(y&1)res*=x;
        x*=x;
        y>>=1;
    }
    return res;
}

struct unionfind{
    int n;
    vector<int> par;
    unionfind(const int _):n(_),par(_,-1){}
    int find(const int x){
        if(par[x]<0)return x;
        return par[x]=find(par[x]);
    }
    bool same(int x,int y){
        return find(x)==find(y);
    }
    void unite(int x,int y){
        x=find(x);
        y=find(y);
        if(x==y)return;
        if(par[x]<par[y])swap(x,y);
        int t=par[y]+par[x];
        par[y]=t;
        par[x]=y;
        n--;
        return;
    }
    int getsize(int i){
        i=find(i);
        return -par[i];
    }
};

//Tには可換群が載る.
template<typename T,T (*op)(T,T),T (*op_inv)(T,T)>
struct weight_uf{
    int n;
    vector<int> par;
    vector<T> diff;
    weight_uf(const int _):n(_),par(_,-1),diff(_,T(0)){}
    //O(α(N))
    int find(const int x){
        if(par[x]<0)return x;
        int r=find(par[x]);//根 この時に先祖のdiffの値も更新がかかる
        diff[x]=op(diff[x],diff[par[x]]);
        return par[x]=r;
    }
    //O(α(N))
    T weight(const int x){
        find(x);
        return diff[x];
    }
    //!same(x,y)なる(x,y)に対しては動かない.
    //O(α(N))
    T diff_query(int x,int y){
        return op_inv(weight(y),weight(x));
    }
    bool same(int x,int y){
        return find(x)==find(y);
    }
    /*
    既にある情報と矛盾する場合はreturn falseする.
    diff[y]-diff[x]=wとなるようにunite.
    */
    //O(α(N))
    bool unite(int x,int y,T w){
        if(same(x,y)){
            //矛盾する
            if(diff_query(x,y)!=w)return false;
            //矛盾しない
            else return true;
        }
        w=op(w,op_inv(weight(x),weight(y)));
        x=find(x);
        y=find(y);
        if(par[x]>par[y])swap(x,y),w=op_inv(T(0),w);
        int t=par[y]+par[x];
        par[x]=t;
        par[y]=x;
        diff[y]=w;
        n--;
        return true;
    }
    //O(1)
    int getsize(int i){
        i=find(i);
        return -par[i];
    }
};

int op(int a,int b){
    return a^b;
}
int op_inv(int a,int b){
    return a^b;
}


int main(){
    ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    cout.precision(10);
    /*--------------------------------*/
    
    int n,m;cin >> n >> m;
    weight_uf<int,op,op_inv> uf(n);
    bool ok=true;
    while(m--){
        int a,b;cin >> a >> b;
        int y;cin >> y;
        ok&=uf.unite(a-1,b-1,y);
    }
    if(!ok){
        cout << -1 << newl;
        return 0;
    }
    vector<int> ans(n);
    rep(i,n){
        if(uf.find(i)==i)ans[i]=0;
    }
    rep(i,n){
        int r=uf.find(i);
        ans[i]=ans[r]^uf.diff_query(i,r);
    }
    for(auto &e:ans)cout << e << newl;
}
0