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

  • 相关阅读:
    Android-adb相关
    我想和iOS大牛们交流的问题
    iOS应用 bug定位
    iOS 中Block的基础用法
    iOS9 耗电量惊人
    三人小团队git分支协作试水
    今天开通了博客
    解决XCode安装插件后插件不能使用的问题(转载)
    (转载)iOS- 指压即达,如何集成iOS9里的3D Touch
    人魔七七
  • 原文地址:https://www.cnblogs.com/crazyapple/p/2999505.html
Copyright © 2011-2022 走看看