栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > 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

 思维+实现 可以尝试

 思路:如果 A 在评论中说 B 是船员,则 A 和 B 属于同一团队(冒名顶替者或船员)。 如果 A 在评论中说 B 是冒名顶替者,则 A 和 B 属于不同的团队。如果 A 和 B 在同一个团队中,那么我们添加权重为 0 的边,否则添加权重为 1。

给出ac代码

#include
using namespace std;
const int N=2e5+5;
int f[2*N],size[2*N];             
bool st[N];
int find(int x){
	if(x==f[x]) return x;
	else return f[x]=find(f[x]);
}
void merge(int x,int y){
	int fx=find(x),fy=find(y);
	if(fx!=fy){
		f[fy]=fx;
		size[fx]+=size[fy];
		size[fy]=0;
	}
}
int main(){
	int tt,x,y;
	cin>>tt;
	string t;
	while(tt--){
		int n,m,ans=0;
		cin>>n>>m;
		memset(st,0,sizeof(st));
		for(int i=1;i<=2*n;i++) {f[i]=i;}
		for(int i=1;i<=n;i++) size[i]=1;
		for(int i=n+1;i<=2*n;i++) size[i]=0;
		for(int i=1;i<=m;i++){
			cin>>x>>y>>t;
			if(t=="imposter") {merge(x,y+n);merge(x+n,y);}
			else {merge(x,y);merge(x+n,y+n);}
		}
		int ff=0;
		for(int i=1; i<=n; i++){
			if(find(i)==find(i+n)){
			    if(!ff)cout<<-1< 

ccpc网络赛搞得心累,孩子想放个假

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/317817.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

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

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