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

  • 相关阅读:
    转载: 如何让form表单在enter键入时不提交
    26个Jquery使用小技巧
    PHP网站页面静态化的生成方法介绍
    用jquery绑定输入框正在输入时返回操作
    PHP开发规范手册之PHP代码规范详解
    Web开发中9个有用的提示和技巧
    TSINGSEE青犀视频边缘计算网关EasyNVR在视频整体监控解决方案中的应用分析
    应急消防通道总是被占用?安防告警视频平台越加必要
    新冠疫情涨涨落落,企业做好线上办公该如何转型?
    【解决方案】如何实现AI自动识别高空抛物行为?
  • 原文地址:https://www.cnblogs.com/crazyapple/p/2999505.html
Copyright © 2011-2022 走看看