timus 1106 Two Teams(二部图)

Posted 贱人方

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了timus 1106 Two Teams(二部图)相关的知识,希望对你有一定的参考价值。

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

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
Problem Author: Dmitry Filimonenkov
【分析】一个简单的二部图染色问题。
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <time.h>
#include <string>
#include <map>
#include <stack>
#include <vector>
#include <set>
#include <queue>
#define inf 10000000
#define mod 10000
typedef long long ll;
using namespace std;
const int N=105;
const int M=50000;
int power(int a,int b,int c){int ans=1;while(b){if(b%2==1){ans=(ans*a)%c;b--;}b/=2;a=a*a%c;}return ans;}
int color[N], vis[N];     
vector<int> G[N];
void dfs(int u)
{
    vis[u] = 1;
    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, t;
    scanf("%d", &n);
    for (int i = 1; i <= n; ++i)
        while (scanf("%d", &t) && t)
        {
            G[i].push_back(t);
        }
    memset(vis, 0, sizeof(vis));
    memset(color, 0, sizeof(color));
    for (int i = 1; i <= n; ++i)
        if (!vis[i])
        {
            color[i]=1;                
            dfs(i);
        }
    int sum = 0;
    for (int i = 1; i <= n; ++i)
        if (color[i] == 1)
            ++sum;
    printf("%d\\n", sum);
    for (int i = 1; i <= n; ++i)
        if (color[i] == 1)
            printf("%d ", i);
    return 0;
}
View Code

 

以上是关于timus 1106 Two Teams(二部图)的主要内容,如果未能解决你的问题,请参考以下文章

E. Two Teams(线段树+链表)

CF552 E. Two Teams

timus 1109 Conference(二分图匹配)

Codeforce 1335C - Two Teams Composing 统计技能种类数量+统计同一技能最大数量

Codeforces Round #552 (Div. 3)-1154E-Two Teams-(模拟+双指针)

SharePoint Online 触发Teams审批