跳到正文

题解

最水 2377

ImaginaryTree约 5 分钟
原文
文章信息与版本

文章信息

发布
2024年11月24日
收录
2025年12月12日
更新
2026年11月14日
篇幅
约 5 分钟
获赞
1

版本

  1. v1当前

第一次表现分 2400+\color{#FF8000}\text{2400+},写篇题解纪念一下。

ARC188C Honest or Liar or Confused

Difficulty: 2377\color{#C0C000}\text{2377}。

题目大意

有 NN 个人,其中有若干个老实人和若干个骗子。老实人总说真话,骗子总说假话。给出了 NN 条证词,第 ii 条为:AiA_i 说 BiB_i 是骗子(用 Ci=1C_i=1 表示)或老实人(用 Ci=0C_i=0 表示)。但每个人都可能被魅惑了,说出的话和他的本意正好相反。给出一个被魅惑的人的集合。

解题思路

我们只考虑第 ii 个人说出来的是真话还是假话。定义 ttitt_i 表示第 ii 个人说的是真话还是假话,hlihl_i 表示第 ii 个人是老实人还是骗子。

对每一个人,说 xx 是老实人的和说他是骗子的,呈现出来的 tttt 值必然相同,考虑在并查集上把这些人合并。

合并完之后,记 S0xS0_x 表示说 xx 是骗子的人的集合,S1xS1_x 表示说 xx 是老实人的人的集合。若 ∃a∈S0x, ∃b∈S1x\exist a\in S0_x,~\exist b\in S1_x,aa 和 bb 属于同一个节点下的并查集,则无解。否则将 aa 和 bb 所在的并查集对应根节点之间连一条边(任选即可,因为 ∀a,b∈S0x\forall a,b \in S0_x 有 aa 和 bb 在同一个并查集里,S1S1 同理)。

而显然连完之后的图应当是一个二分图,否则无法给它们分配 tttt 值。跑完之后,对于每一条线索再求出对应的 hlhl 值,异或一下就是每个人是否被魅惑的答案。

代码实现

写起来很舒服。应该没人把 B 交到 C 吃罚时吧。

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;
}
原文
存档评论1 条抓取于