zoukankan      html  css  js  c++  java
  • URAL 1106 Two Teams二分图

    S - Two Teams
    Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u

    Description

    The group of people consists of N members. Every member has one or more friends in the group. You are to write program that divides this group into two teams. Every member of each team must have friends in another team.

    Input

    The first line of input contains the only number N ( N ≤ 100). Members are numbered from 1 to N. The second, the third,…and the ( N+1)th line contain list of friends of the first, the second, …and the Nth member respectively. This list is finished by zero. Remember that friendship is always mutual in this group.

    Output

    The first line of output should contain the number of people in the first team or zero if it is impossible to divide people into two teams. If the solution exists you should write the list of the first group into the second line of output. Numbers should be divided by single space. If there are more than one solution you may find any of them.

    Sample Input

    inputoutput
    7
    2 3 0
    3 1 0
    1 2 4 5 0
    3 0
    3 0
    7 0
    6 0
    
    4
    2 4 5 6
    

    类似二分图,但是可以用dfs+染色的方法解决

    #include<stdio.h>
    #include<vector>
    #include<string.h>
    #include<iostream>
    #include<algorithm>
    using namespace std;
    const int maxn=200;
    int color[maxn];
    bool vis[maxn];
    vector<int>g[maxn];
    int ans[maxn];
    
    void dfs(int u){
         vis[u]=true;
         for(int i=0;i<g[u].size();i++){
              int v=g[u][i];
             if(!vis[v]){
                  color[v]=3-color[u];
                 dfs(v);
             }
         }
    }
    
    int main(){
      int n;
        while(scanf("%d",&n)!=EOF){
            memset(vis,false,sizeof(vis));
            memset(color,0,sizeof(color));
            memset(ans,0,sizeof(ans));
            int x;
            for(int i=1;i<=n;i++){
                g[i].clear();
               for(int j=1;;j++){
                  scanf("%d",&x);
                  if(!x)
                   break;
                   g[i].push_back(x);
               }
            }
            for(int i=1;i<=n;i++){
               if(!vis[i]){
                  color[i]=1;
                   dfs(i);
               }
            }
            int sum=0;
            int cnt=-1;
            for(int i=1;i<=n;i++){
               if(color[i]==1){
                sum++;
                   ans[++cnt]=i;
               }
            }
            printf("%d
    ",sum);
            for(int i=0;i<=cnt;i++){
                printf("%d%c",ans[i],i==cnt?'
    ':' ');
            }
        }
        return 0;
    }
  • 相关阅读:
    谁在TDD
    开源许可证简单总结
    【转】IIS HTTP500错误以及COM+应用程序8004e00f错误的解决方法
    [原]Linux平台Boost的编译方法
    [原]linux下格式化磁盘的相关问题
    [原]编译MongoDB,C++连接MongoDB测试
    [转]谈谈Unicode编码,简要解释UCS、UTF、BMP、BOM等名词(科普)
    [转]linux下如何查看文件编码格式及转换文件编码
    [原]linux(虚拟机)下安装MySQL
    [转]Linux下比较全面的监控工具dstat
  • 原文地址:https://www.cnblogs.com/13224ACMer/p/4830476.html
Copyright © 2011-2022 走看看