HDU1213 How Many Tables / HDU1325 Is It A Tree?(普通并查集)

【hdu1213】How Many Tables

Problem Description
Today is Ignatius' birthday. He invites a lot of friends. Now it's dinner time. Ignatius wants to know how many tables he needs at least. You have to notice that not all the friends know each other, and all the friends do not want to stay with strangers.

One important rule for this problem is that if I tell you A knows B, and B knows C, that means A, B, C know each other, so they can stay in one table.

For example: If I tell you A knows B, B knows C, and D knows E, so A, B, C can stay in one table, and D, E have to stay in the other one. So Ignatius needs 2 tables at least.
 

Input
The input starts with an integer T(1<=T<=25) which indicate the number of test cases. Then T test cases follow. Each test case starts with two integers N and M(1<=N,M<=1000). N indicates the number of friends, the friends are marked from 1 to N. Then M lines follow. Each line consists of two integers A and B(A!=B), that means friend A and friend B know each other. There will be a blank line between two cases.
 
Output
For each test case, just output how many tables Ignatius needs at least. Do NOT print any blanks.
 

Sample Input
 
  
 2
 5 3
 1 2
 2 3 
 4 5
 5 1
 2 5
 

Sample Output
 
  
2 4


题目大意:有T行输入,两个数字代表两个人是互相认识,只有认识的才能坐一桌,问最少要多少张桌子。

所以只要找出有一共多少个不同的集合,也就是有多少个根节点(parent[x]=x)即可。

#include<iostream>
using namespace std;

int sum ,m ,n;
int pre [1005];

void init(int n){

for(int i=1;i<=n;i++)//n表示输入的结点的个数
{
    pre[i]=i;//将每一个结点的前导点设置为自己

}
}

int find(int x)
{
    int r=x;
    while(pre[r]!=r)
    r=pre[r];//找到他的前导结点
    int i=x,j;
    while(i!=r)//路径压缩算法
    {
        j=pre[i];//记录x的前导结点
        pre[i]=r;//将i的前导结点设置为r根节点
        i=j;
    }
    return r;
}
/*int find(int x)
{
	if(pre[x]!=x)
	pre[x]=find(pre[x]);
	return pre[x];
}*/

void join(int x,int y)
{
    int a=find(x);//x的根节点为a
    int b=find(y);//y的根节点为b
    if(a!=b)//如果a,b不是相同的根节点,则说明ab不是连通的
    {
        pre[a]=b;//我们将ab相连 将a的前导结点设置为b
    }
    else return;
}

int main(){
	int t;
	cin>>t;
	while(t--){
		sum=0;
		cin>>m>>n;
	//	sum=n;
		init(m);
		int first,second;
		while(n--){
			cin>>first>>second;
			join(first,second);
		} 
		for(int i=1;i<=m;i++){
			if(find(i)==i)
			sum++;
		}
		cout<<sum<<endl;
	}
	return 0;
} 

【HDU1325】 Is It A Tree?

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 12108    Accepted Submission(s): 2757


Problem Description
A tree is a well-known data structure that is either empty (null, void, nothing) or is a set of one or more nodes connected by directed edges between nodes satisfying the following properties. 
There is exactly one node, called the root, to which no directed edges point. 

Every node except the root has exactly one edge pointing to it. 

There is a unique sequence of directed edges from the root to each node. 

For example, consider the illustrations below, in which nodes are represented by circles and edges are represented by lines with arrowheads. The first two of these are trees, but the last is not.



In this problem you will be given several descriptions of collections of nodes connected by directed edges. For each of these you are to determine if the collection satisfies the definition of a tree or not. 

 

 

Input
The input will consist of a sequence of descriptions (test cases) followed by a pair of negative integers. Each test case will consist of a sequence of edge descriptions followed by a pair of zeroes Each edge description will consist of a pair of integers; the first integer identifies the node from which the edge begins, and the second integer identifies the node to which the edge is directed. Node numbers will always be greater than zero. 
 

 

Output
For each test case display the line ``Case k is a tree." or the line ``Case k is not a tree.", where k corresponds to the test case number (they are sequentially numbered starting with 1). 
 

 

Sample Input
6 8 5 3 5 2 6 4 5 6 0 0
8 1 7 3 6 2 8 9 7 5 7 4 7 8 7 6 0 0
3 8 6 8 6 4 5 3 5 6 5 2 0 0
-1 -1
 

 

Sample Output
Case 1 is a tree.
Case 2 is a tree.
Case 3 is not a tree.

题目大意:判断是不是一棵树,构成环不是,森林也不是,入度大于一也不是。所以立好flag就可以了。

还有就是join那里是pre[b]=a!!!啊啊啊啊啊啊啊啊啊啊啊啊找了一久饱的错误才发现原来是后面的数字是儿子前面的数字是父亲


树的定义:

1)一个具有n个结点的数,最多只有n-1个结点

2)一棵树有且只有一个入度为0的结点,并且所有结点的入度都不大于1.

百度百科的解释:每个节点有零个或多个子节点;没有父节点的节点称为根节点;每一个非根节点有且只有一个父节点;除了根节点外,每个子节点可以分为多个不相交的子树。


#include<iostream>
#include<stdio.h>
using namespace std;
int f[100005];
const int MAX=100000;

int rudu[MAX+10];//入度

int root[MAX+10];//保存是否为根


int pre [MAX];

void init(){

for(int i=1;i<=MAX;i++)//n表示输入的结点的个数
{
    pre[i]=i;//将每一个结点的前导点设置为自己
    rudu[i]=0;
    root[i]=0;
}

}

int find(int x)
{
    int r=x;
    while(pre[r]!=r)
    r=pre[r];//找到他的前导结点
    int i=x,j;
    while(i!=r)//路径压缩算法
    {
        j=pre[i];//记录x的前导结点
        pre[i]=r;//将i的前导结点设置为r根节点
        i=j;
    }
    return r;
}
/*int find(int x)
{
	if(pre[x]!=x)
	pre[x]=find(pre[x]);
	return pre[x];
}*/

void join(int x,int y)
{
    int a=find(x);//x的根节点为a
    int b=find(y);//y的根节点为b
    if(a!=b)//如果a,b不是相同的根节点,则说明ab不是连通的
    {
        pre[b]=a;//我们将ab相连 将a的前导结点设置为b
        //!!!!!!!!!!!!!!!!!!这里是pre[b] =a!!!!!!!!!!!!!!!!!!!!!!!!!!
    }
    //else return;
}

int main(){
	int x,y,flag,circle,num=1;
	while(1)
	{
		flag=0;circle=0;
		init();
		while(cin>>x>>y)
		{
			if(x<=0)
			break;
			root[x]=1;root[y]=1;
			
			if(find(x)==find(y))
			circle=1;
			
			else if(find(y)!=y)
			circle=1;
			
			else
			join(x,y);
		}
		if(x<0)break;
		   for (int i=1;i<=MAX;i++)  
        {  
            if (root[i])  
                rudu[find(pre[i])]++;  
        }  
        for (int i=1;i<=MAX;i++)  
        {  
            if (rudu[i]>0)  
                flag++;  
        }  
        printf ("Case %d ",num++);  
        if (flag>1)  
            printf ("is not a tree.\n");  
        else if (circle)  
            printf ("is not a tree.\n");  
        else  
            printf ("is a tree.\n");  
	}
	return 0;
} 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值