zoukankan      html  css  js  c++  java
  • hdu 1213 How Many Tables(dfs)

    hdu 1213 How Many Tables

    Problem DescriptionToday 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.
    InputThe 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.
    OutputFor each test case, just output how many tables Ignatius needs at least. Do NOT print any blanks.
    Sample Input25 31 22 34 55 12 5

    //这个题我采用广搜来处理
    #include<iostream>
    using namespace std;
    bool mark[1001][1001];  //用来标记认识与否
    int num,n;
    void bfs(int i,int j)
    {
     mark[i][j]=0;
     int k;
     for(k=0;k<n;k++)  //横向纵向的搜索 
     {
       if(mark[i][k])
        bfs(i,k);
       if(mark[k][i])
        bfs(k,i);
       if(mark[k][j])
        bfs(k,j);
       if(mark[j][k])
        bfs(j,k);
     }
    }
    int main()
    {
     int t,m,i,a,b,j;
     int flag[1001];
     cin>>t;
     while(t--)
     {
      num=0;
      cin>>n>>m;
      memset(mark,0,n*n*sizeof(bool));
      memset(flag,0,sizeof(flag));
      for(i=0;i<m;i++)
      {
       cin>>a>>b;
       mark[a-1][b-1]=1;   //标记认识的人
       mark[b-1][a-1]=1;
       flag[a]=1;                //标记出现过了的编号
       flag[b]=1;
      }
      for(i=1;i<=n;i++)
       if(flag[i])
        num++;    //统计标号人数
      num=n-num;   //计算出没有出现编号的人数 也就是说 他们一个人也不认识  要这么多的桌子
      for(i=0;i<n;i++)
       for(j=0;j<n;j++)
        if(mark[i][j])  //如果被标记过就开始搜索
        {
         bfs(i,j);
         num++;
        }

      cout<<num<<endl;
     }
     return 0;
    }

  • 相关阅读:
    C# 两个窗体中相互切换的方法
    Linq对DataTable数据的分组统计
    如何让窗体大小随着控件的大小变化而变化
    两张表解决用户自定义数据库之思路
    picturebox显示用字符串代表图片名称的(已导入资源的)图片
    如何将access高版本数据库保存为低版本的?
    Microsoft.Jet.OLEDB.4.0和Microsoft.ACE.OLEDB.12.0的区别
    条件编译解决AutoCAD多版本问题
    一个爬取股票信息的爬虫程序
    Python爬虫抓取东方财富网股票数据并实现MySQL数据库存储
  • 原文地址:https://www.cnblogs.com/crazyapple/p/2999505.html
Copyright © 2011-2022 走看看