URAL 1106. Two Teams (二分图)

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;
}
时间: 2024-10-27 13:14:43

URAL 1106. Two Teams (二分图)的相关文章

URAL 1106 Two Teams

S - Two Teams Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Practice URAL 1106 Description The group of people consists of N members. Every member has one or more friends in the group. You are to write pro

timus 1106 Two Teams(二部图)

Two Teams Time limit: 1.0 secondMemory 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 fri

ural 1076 Trash 二分图最大权匹配(费用流实现)

统计每种垃圾的总和,若将K种垃圾倒入第F个垃圾桶,那么花费就是K-F(k)  (自己已经有的垃圾不用倒). 然后就是简单的二分图建图. #include<cstdio> #include<queue> #include<algorithm> #include<cstring> using namespace std; #define MAXN 1000 #define MAXM 1000000 #define INF 0x3f3f3f3f struct no

二分图匹配(匈牙利算法) URAL 1721 Two Sides of the Same Coin

题目传送门 1 /* 2 题意:三种人,statements,testdata,anthing.要求两个人能完成s和t两个工作,且rank相差2 3 二分图匹配:此题学习建图技巧,两个集和内部一定没有边相连,rank模4小于2和大于等于2的人才能搭配,并且相差正好2, 4 两个人会的不能一样.以上条件删选出两个集合,不能按照anthing来分. 5 */ 6 #include <cstdio> 7 #include <iostream> 8 #include <algorit

URAL 2017 Best of a bad lot 二分图染色 使x集点数最少

题目链接:点击打开链接 题意: 有n个嫌疑犯.[1,n] 第i行表示第i个嫌疑犯说案发时他所在的地名,后面一个数m表示当时他看到m个人,后面m个数表示他看到的人. 找出最小的犯罪团体(即多数人都是好人原则) 若大家都是好人则随便输出一个人当坏人== 思路: 当一个人x被2个不同地方的人u, v看到时,则u v其中一个一定是犯人. 所以u-v建一条边. 然后二分图染色使得x点集点数最少. 先染一个子图,然后把点集少的归成x集就可以了,因为任意两个子图都是互不影响的. #include<bits/s

【POJ 1112】Team Them Up!(二分图染色+DP)

Description Your task is to divide a number of persons into two teams, in such a way, that: everyone belongs to one of the teams; every team has at least one member; every person in the team knows every other person in his team; teams are as close in

kuangbin带你飞 匹配问题 二分匹配 + 二分图多重匹配 + 二分图最大权匹配 + 一般图匹配带花树

二分匹配:二分图的一些性质 二分图又称作二部图,是图论中的一种特殊模型. 设G=(V,E)是一个无向图,如果顶点V可分割为两个互不相交的子集(A,B),并且图中的每条边(i,j)所关联的两个顶点i和j分别属于这两个不同的顶点集(i in A,j in B),则称图G为一个二分图. 1.一个二分图中的最大匹配数等于这个图中的最小点覆盖数 König定理是一个二分图中很重要的定理,它的意思是,一个二分图中的最大匹配数等于这个图中的最小点覆盖数.如果你还不知道什么是最小点覆盖,我也在这里说一下:假如选

ural 1249. Ancient Necropolis

1249. Ancient Necropolis Time limit: 5.0 secondMemory limit: 4 MB Aerophotography data provide a bitmap picture of a hard-to-reach region. According to the suggestions of scientists, this region is a cemetery of an extinct civilization. Indeed, the p

URAL 1572. Yekaterinozavodsk Great Well(数学啊)

题目链接:http://acm.timus.ru/problem.aspx?space=1&num=1572 About a month ago two teams of the Ural State University returned from the Yekaterinozavodsk training camp. This northern city impressed them so much that they decided to return there by any mean