zoukankan      html  css  js  c++  java
  • How Many Tables

    How Many Tables

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


    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
     
    //How Many Tables
    #include<iostream>
    #define max 1001
    using namespace std;
    
    int adj[max][max],M,N,ans;
    
    bool visited[max];
    
    void DFS(int v)
    {
    	visited[v]=true;
    	for(int w=1;w<=M;w++)
    		if(adj[v][w]==1 && !visited[w])
    			DFS(w);
    }
    
    void DFSTranser()
    {
    	int v;
    	for(v=1;v<=M;v++)
    		visited[v]=false;
    	for(v=1;v<=M;v++)
    		if(!visited[v])
    		{
    			DFS(v);
    			ans++;
    		}
    }
    
    
    int main()
    {
    	int T,i,j,p1,p2;
    
    	cin>>T;
    	while(T--)
    	{
    		cin>>M>>N;
    		ans=0;
    		for(i=1;i<=M;i++)
    			for(j=1;j<=M;j++)
    				adj[i][j]=0;
    		for(i=1;i<=N;i++)
    		{
    			cin>>p1>>p2;
    			adj[p1][p2]=1;
    			adj[p2][p1]=1;
    		}
    
    		DFSTranser();
    
    		cout<<ans<<endl;
    	}
    
    	return 0;
    }
    

      

    世上无难事,只要肯登攀。
  • 相关阅读:
    ERROR 1045 (28000): Access denied for user root@localhost (using password:
    MySQL: InnoDB 还是 MyISAM?
    PHP系统函数
    为什么分离数据库软件和数据库服务?
    C#索引器的作用及使用
    asp.net 中Session的运用,及抛出错误“未将对象引用设置到对象的实例”
    C#父类对象和子类对象之间的转化
    C#中属性简写原理
    c# 中Intern的作用
    C# 中ref和out的区别
  • 原文地址:https://www.cnblogs.com/littlehoom/p/3426416.html
Copyright © 2011-2022 走看看