最水 2377
ImaginaryTree · · 题解
第一次表现分
ARC188C Honest or Liar or Confused
Difficulty:
题目大意
有
解题思路
我们只考虑第
对每一个人,说
合并完之后,记
而显然连完之后的图应当是一个二分图,否则无法给它们分配
代码实现
写起来很舒服。应该没人把 B 交到 C 吃罚时吧。
#include<bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define rep(i,l,r) for(register ll i=(l);i<=(r);++i)
#define Rep(i,l,r) for(register ll i=(r);i>=(l);--i)
#define all(x) x.begin(),x.end()
#define Set(x,y) memset(x,y,sizeof(x))
#define Cpy(x,y) memcpy(x,y,sizeof(x))
#define cll const long long
using namespace std;
template<class T>
void death(T s){cout<<s<<endl;exit(0);}
cll N=200009,V=600009;
ll n,m;
vector<ll> p[N][2],tl[N][2],grp[V],ldr,to[V];
ll id[V];
ll root(ll u){return id[u]==u?u:id[u]=root(id[u]);}
void merge(ll x,ll y){
x=root(x),y=root(y);
if(x==y)return;
id[x]=y;
}
ll vst[V],tt[N],hl[N];
bool BiG=1;
void dfs(ll u,ll clr){
if(!BiG)return;
vst[u]=clr;
for(auto v:to[u]){
if(!vst[v])dfs(v,3-clr);
else if(vst[v]==clr)BiG=0;
}
}
int main(){
cin>>n>>m;
rep(i,1,m){
ll a,b,x;cin>>a>>b>>x;
p[b][x].push_back(a);
tl[a][x].push_back(b);
}
rep(i,1,n*3)id[i]=i;
rep(i,1,n){
ll t=n+i,f=n+n+i;
for(auto x:p[i][0])merge(x,t);
for(auto x:p[i][1])merge(x,f);
}
rep(i,1,n){
set<ll> s[2];
for(auto x:p[i][0])s[0].insert(root(x));
for(auto x:p[i][1])s[1].insert(root(x));
for(auto a:s[0])if(s[1].count(a))death(-1);
for(auto a:s[0])for(auto b:s[1])to[a].push_back(b),to[b].push_back(a);
}
rep(i,1,n*3)if(i==root(i)&&!vst[i])dfs(i,1);
if(!BiG)death(-1);
rep(i,1,n){
ll u=root(i);
if(vst[u]==1)tt[i]=1;
else tt[i]=0;
}
rep(i,1,n){
for(auto u:tl[i][0]) hl[u]=tt[i];
for(auto u:tl[i][1]) hl[u]=1-tt[i];
}
rep(i,1,n)cout<<(hl[i]^tt[i]);
return 0;
}