栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

Codeforces Round #747 (Div. 2) D. The Number of Imposters(扩展域并查集)

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Codeforces Round #747 (Div. 2) D. The Number of Imposters(扩展域并查集)

官方题解好像是二分图,em…个人觉得这题扩展域并查集更简单好理解一些。

AC代码:

#include 
#define int long long
using namespace std;
const int N=2e6+5,mod=1e9+7;
int n,m;
int p[N],s[N];

int find(int x){
	if(x!=p[x]) p[x]=find(p[x]);
	return p[x];
}

void merge(int a,int b){
	int aa=find(a),bb=find(b);
	if(aa==bb)return;
	p[aa]=bb,s[bb]+=s[aa],s[aa]=0;
}

void solve(){
	cin>>n>>m;
	
	//s[i]表示的是以i为根的树里面说假话的人的个数 
	for(int i=1;i<=n;i++)p[i]=i,s[i]=0;
	for(int i=n+1;i<=2*n;i++)p[i]=i,s[i]=1;
	
	while(m--){
		int a,b;
		string s;
		cin>>a>>b>>s;
		if(s=="crewmate"){
			merge(a,b);
			merge(a+n,b+n);
		}
		else{
			merge(a,b+n);
			merge(a+n,b);
		}	
	}
	
	int res=0;
	for(int i=1;i<=n;i++){
		int a=find(i),b=find(i+n);
		if(a==b){
			cout<<-1<>T;
	while(T--)solve();	
} 
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/302708.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号