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+postfix+extmail+dovecot打造基于web页面的邮件系统
    各种大型网站技术架构--摘抄
    会php不回缓存行吗?多重实现
    Nutch相关框架视频教程--说明
    Nginx 配置文件模板
    原创:CentOS6.4配置solr 4.7.2+IK分词器
    nutch2.2.1安装部署
    Android View绘制13问13答
    使用 MailOtto 做完美预加载
    ActiveAndroid:活性记录的风格ORM(对象关系映射)
  • 原文地址:https://www.cnblogs.com/littlehoom/p/3426416.html
Copyright © 2011-2022 走看看