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;
    }
    

      

    世上无难事,只要肯登攀。
  • 相关阅读:
    ibatis的优缺点及可行性分析
    NHibernate优点和缺点:
    IbatisNet的介绍和使用
    bat(续七)-for语句(循环结构)
    bat(续五)-获取批处理文件所在路径
    Shell函数参数
    Shell函数:Shell函数返回值、删除函数、在终端调用函数
    Shell break和continue命令
    Shell until循环
    Shell while循环
  • 原文地址:https://www.cnblogs.com/littlehoom/p/3426416.html
Copyright © 2011-2022 走看看