码迷,mamicode.com
首页 > 其他好文 > 详细

URAL 1106. Two Teams (二分图)

时间:2015-06-20 11:56:45      阅读:138      评论:0      收藏:0      [点我收藏+]

标签:

1106. Two Teams

Time limit: 1.0 second
Memory limit: 64 MB
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 output
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
Problem Author: Dmitry Filimonenkov
Problem Source: Tetrahedron Team Contest May 2001




解析:貌似二分图。但是比二分图简单。

开两个标记数组,记录每个人是否有朋友在第一组,第二组。

扫一遍人:

1.若在第一组没有他的朋友:则将它放到第一组,并标记他的朋友在第一组都有朋友。

2.否则:

         (1) 若在第二组没有他的朋友,则将它放到第二组,并标记他的朋友在第二组都有朋友。

          (2)否则,说明在两组中都没有朋友,则可将他随便放在一个组里,也可不处理。




AC代码:

#include <bits/stdc++.h>
using namespace std;

bool c[105], b[105];
vector<int> a[105];
vector<int> ans;

int main(){
    #ifdef sxk
        freopen("in.txt", "r", stdin);
    #endif // sxk

    int n, x;
    while(scanf("%d", &n) != EOF){
        memset(c, false, sizeof(c));
        memset(b, false, sizeof(b));
        ans.clear();
        for(int i=1; i<=n; i++){
            while(scanf("%d", &x) && x) a[i].push_back(x);
        }
        for(int i=1; i<=n; i++){
            if(!c[i]){       //第一组没朋友
                ans.push_back(i);
                for(int j=0; j<a[i].size(); j++) c[ a[i][j] ] = true;
            }
            else{
                if(!b[i]){    //第二组没朋友
                    for(int j=0; j<a[i].size(); j++) b[ a[i][j] ] = true;
                }             //都没有,不处理
            }
        }
        int cnt = ans.size();
        cnt %= n;
        printf("%d\n", cnt);
        for(int i=0; i<cnt; i++){
            printf("%d%c", ans[i], i < cnt-1 ? ' ' : '\n');
        }
    }
    return 0;
}


URAL 1106. Two Teams (二分图)

标签:

原文地址:http://blog.csdn.net/u013446688/article/details/46572617

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!