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

      

    世上无难事,只要肯登攀。
  • 相关阅读:
    Linux:password 与passphrase
    QT设置openCV头文件和链接动态库路径
    matlab对图像加入噪声的方法
    RGB到HSV的彩色空间变化 Matlab
    QImage与IplImage之间的转换
    iframe自适应高度(简单经典)兼容ie6ie9 ,firefox,opera,chrome
    转载:关于生成并发唯一性流水号的解决方案
    EXCEL表格纵横转换
    rdlc打印时多出空白页面(reportviewer)
    一行代码解决各种IE兼容问题,IE6,IE7,IE8,IE9,IE10
  • 原文地址:https://www.cnblogs.com/littlehoom/p/3426416.html
Copyright © 2011-2022 走看看