Gym-101987 2018-2019 ACM-ICPC, Asia Seoul Regional Contest K-TV Show Game 2-sat,tarjan

题意

\(k\)盏灯,每盏灯有两种颜色R,B。有\(n\)个人,每个人猜三盏灯的颜色,让你求这\(k\)盏灯的颜色,使每个人都至少猜对两盏灯的颜色。

分析

转化成\(two-sat\)问题,对于每个人若猜错其中一盏灯,那么另外两盏灯的颜色必须猜对,建边,跑\(tarjan\),输出答案。

Code

#include<algorithm>
#include<iostream>
#include<cstring>
#include<iomanip>
#include<sstream>
#include<cstdio>
#include<string>
#include<vector>
#include<bitset>
#include<queue>
#include<cmath>
#include<stack>
#include<set>
#include<map>
#define rep(i,x,n) for(int i=x;i<=n;i++)
#define per(i,n,x) for(int i=n;i>=x;i--)
#define sz(a) int(a.size())
#define rson mid+1,r,p<<1|1
#define pii pair<int,int>
#define lson l,mid,p<<1
#define ll long long
#define pb push_back
#define mp make_pair
#define se second
#define fi first
using namespace std;
const double eps=1e-8;
const int mod=1e9+7;
const int N=2e5+10;
const int inf=1e9;
int k,n;
vector<int>g[N];
int dfn[N],st[N],low[N],co[N],tot,top,scc_cnt;
void dfs(int u){
	dfn[u]=low[u]=++tot;
	st[++top]=u;
	for(int x:g[u]){
		if(!dfn[x]){
			dfs(x);
			low[u]=min(low[u],low[x]);
		}else if(!co[x]) low[u]=min(low[u],dfn[x]);
	}
	if(low[u]==dfn[u]){
		++scc_cnt;
		while(1){
			int x=st[top--];
			co[x]=scc_cnt;
			if(x==u) break;
		}
	}
}
int main(){
	ios::sync_with_stdio(false);
	//freopen("in","r",stdin);
	cin>>k>>n;
	rep(i,1,n){
		int a,b,c,x=0,y=0,z=0;
		char ch;
		cin>>a>>ch;
		if(ch=='R') x=1;
		cin>>b>>ch;
		if(ch=='R') y=1;
		cin>>c>>ch;
		if(ch=='R') z=1;
		g[a+x*k].pb(b+(y^1)*k);
		g[a+x*k].pb(c+(z^1)*k);
		g[b+y*k].pb(a+(x^1)*k);
		g[b+y*k].pb(c+(z^1)*k);
		g[c+z*k].pb(a+(x^1)*k);
		g[c+z*k].pb(b+(y^1)*k);
	}
	for(int i=1;i<=2*k;i++) if(!dfn[i]){
		dfs(i);
	}
	rep(i,1,k) if(co[i]==co[i+k]){
		cout<<"-1\n";
		return 0;
	}
	rep(i,1,k){
		if(co[i]<co[i+k]) cout<<'R';
		else cout<<'B';
	}
	cout<<'\n';
	return 0;
}
posted @ 2020-07-13 18:02  xyq0220  阅读(13)  评论(0编辑  收藏