首页 > 其他分享 >hdu-3926-Hand in Hand-并查集

hdu-3926-Hand in Hand-并查集

时间:2023-09-12 12:33:20浏览次数:35  
标签:hdu return fu int sum 查集 Hand include root


判断 两个图 是否同构。。

用了并查集。。


#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<algorithm>
#include<queue>
#include<stack>
using namespace std;
#define ll long long

int n,m;
struct node
{
	int fu;
	int sum;
	int flag;
}p[10010],p1[10010],p2[10010];
int root(int h)
{
	if(p[h].fu==-1) return h;
	else return p[h].fu=root(p[h].fu);
}
void merge(int a,int b)
{
	a=root(a);
	b=root(b);
	p[a].fu=b;
	p[b].sum+=p[a].sum;
}
int cmp(node a,node b)
{
	if(a.sum==b.sum)
		return a.flag<b.flag;
	return a.sum<b.sum;
}

int main()
{
	int i,j,k;
	int t;
	scanf("%d",&t);
	for(k=1;k<=t;k++)
	{
		scanf("%d%d",&n,&m);
		for(i=1;i<=n;i++){
			p[i].fu=-1;
			p[i].sum=1;
			p[i].flag=0;
		}
		while(m--)
		{
			int a,b;
			scanf("%d%d",&a,&b);
			int ta=root(a);
			int tb=root(b);
			if(ta!=tb)
				merge(a,b);
			else
				p[ta].flag=1;
		}
		int ans=0;
		for(i=1;i<=n;i++)
		{
			if(p[i].fu==-1)
			{
				p1[ans].flag=p[i].flag;
				p1[ans].sum=p[i].sum;
				ans++;
			}
		}
		scanf("%d%d",&n,&m);
		for(i=1;i<=n;i++){
			p[i].fu=-1;
			p[i].sum=1;
			p[i].flag=0;
		}
		while(m--)
		{
			int a,b;
			scanf("%d%d",&a,&b);
			int ta=root(a);
			int tb=root(b);
			if(ta!=tb)
				merge(a,b);
			else
				p[ta].flag=1;
		}
		int ans1=0;
		for(i=1;i<=n;i++)
		{
			if(p[i].fu==-1)
			{
				p2[ans1].flag=p[i].flag;
				p2[ans1].sum=p[i].sum;
				ans1++;
			}
		}
		if(ans!=ans1)
			printf("Case #%d: NO\n",k);
		else
		{
			sort(p1,p1+ans,cmp);
			sort(p2,p2+ans,cmp);
			int ff=0;
			for(i=0;i<ans;i++)
			{
				if(p1[i].sum==p2[i].sum && p1[i].flag==p2[i].flag)
					continue;
				else
				{
					ff=1;
					break;
				}
			}
			if(ff==0)
				printf("Case #%d: YES\n",k);
			else
				printf("Case #%d: NO\n",k);
		}
	}
}




标签:hdu,return,fu,int,sum,查集,Hand,include,root
From: https://blog.51cto.com/u_16244339/7444337

相关文章

  • HDU 2955 Robberies
    01背包银行总钱数==容量V概率可以算安全的概率p=1-p;#include<stdio.h>#include<string.h>#include<algorithm>usingnamespacestd;doublepp,p[10001],f[10001];intv[10001];intmain(){ intt; scanf("%d",&t); while(t--){ intn,j,i,k,sum......
  • hdu 1372 Knight Moves 骑士的移动 bfs--马走日
    #include<stdio.h>#include<string.h>#include<queue>usingnamespacestd;charss[3],ee[3];intx1,y1,x2,y2;structpos{intx,y,step;}sta,end;intf[10][10];intdir[8][2]={1,2,1,-2,-1,2,-1,-2,2,1,2,-1,-2,1,-2,-1};boolfan......
  • 二叉树 遍历 hdu-1710-Binary Tree Traversals
    给出二叉树的前序遍历和中序遍历,求后序遍历。。 算法:由前序遍历的第一个元素可确定左、右子树的根节点,参照中序遍历又可进一步确定子树的左、右子树元素。如此递归地参照两个遍历序列,最终构造出二叉树。 由前序和中序结果求后序遍历结果树的遍历: 给你一棵树的先......
  • hdu1400/acwing 291 Mondriaan's Dream
    题意描述:给定一块n*m的区域,用1*2的长方形填充,长方形可以横着或竖着摆,问一共有多少种填充方案具体思路:题意没什么好说的,简单易懂,很经典的一类状态压缩问题(在棋盘中求填充方案)。观察数据,满足n,m都比较小,但是搜索的复杂度大到无法接受,考虑使用状态压缩求解此类问题......
  • 循序渐进介绍基于CommunityToolkit.Mvvm 和HandyControl的WPF应用端开发(2)
    在前面随笔《循序渐进介绍基于CommunityToolkit.Mvvm和HandyControl的WPF应用端开发(1)》中介绍了Mvvm的开发,以及一些界面效果,本篇随笔继续深入探讨基于CommunityToolkit.Mvvm和HandyControl的WPF应用端开发,介绍如何整合SqlSugar框架的基础接口,通过基类继承的方式,简化实际项目......
  • 并查集 堆排序 (9/10)
    并查集模板 注意查找到后查找的节点直接连接根节点#include<iostream>usingnamespacestd;constintN=100010;intp[N];//关键记住find函数intfind(inta){if(p[a]!=a)p[a]=find(p[a]);//不等于根节点就往头结点走,并且等于returnp[a];}intma......
  • [HDU4117] GRE
    RecentlyGeorgeispreparingfortheGraduateRecordExaminations(GREforshort).Obviouslythemostimportantthingisrecitingthewords.NowGeorgeisworkingonawordlistcontaining\(N\)words.Hehassopooramemorythatitistoohardforhim......
  • 循序渐进介绍基于CommunityToolkit.Mvvm 和HandyControl的WPF应用端开发(1)
    在我们的SqlSugar的开发框架中,整合了Winform端、Vue3+ElementPlus的前端、以及基于UniApp+Vue+ThorUI的移动前端几个前端处理,基本上覆盖了我们日常的应用模式了,本篇随笔进一步介绍前端应用的领域,研究集成WPF的应用端,循序渐进介绍基于CommunityToolkit.Mvvm和HandyControl的WPF应......
  • waiting for handler commit
    一、mysql主机状态showprocesslist;出现大量waitingforhandlercommit可以考虑看一下磁盘,是不是磁盘占满二、mysql从机状态showslavestatus;Slave_SQL_Running_State:waitingforhandlercommit如果SQL线程和IO线程都是YesSlave_IO_Running:YesSlave_SQL_Running:......
  • mybatis类型转换。TypeHandler案例
    配置mybatis-plus:configuration:map-underscore-to-camel-case:truecache-enabled:falseglobal-config:db-config:column-underline:truedb-type:mysqlrefresh:falsetype-handlers-package:com.peony.common.typehandlers......