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

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

【分析】一个简单的二部图染色问题。

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

时间: 2024-10-05 02:32:30

timus 1106 Two Teams(二部图)的相关文章

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 h

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 1049 Brave Balloonists

题目链接:http://acm.timus.ru/problem.aspx?space=1&num=1049 题目描述: 题目大意为给定10个数,然后求这10个数之积所对应的数的所有正因子的个数N的个位数. 那么直接对10个数进行质因数分解即可.(注意%10) 1 #include<cstdio> 2 #include<iostream> 3 #include<cstring> 4 using namespace std ; 5 6 const int MAXM

Teams(uva11609+组合)

I - Teams Time Limit:1000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit Status Practice UVA 11609 题意:有n个人,选多个人参加比赛,其中一个是队长,队长不同其他选手相同也算作不同的方案,.问你一共有多少种方案. 思路:自己才纸上稍微推理一下,n*2n-1%mod: 转载请注明出处:寻找&星空の孩子 题目链接:UVA 11609 也欢迎来我开的专题刷题.哈哈htt

Timus OJ 1057 数位dp

http://acm.timus.ru/problem.aspx?space=1&num=1057 1057. Amount of Degrees Time limit: 1.0 second Memory limit: 64 MB Create a code to determine the amount of integers, lying in the set [X;Y] and being a sum of exactlyK different integer degrees of B.

timus 1547. Password Search【题意思路+大数模板】

题目地址传送门:URAL 1547 这道题需要用到大数的很多模板,推荐大家去刷刷! 题目大意:Vova忘记了在Timus OJ上面的密码了,密码是由小写字母(a~z)组成的,他只知道密码长度不大于n位,现在他需要用m台数据处理器对密码进行检索,其中检索顺序需要满足字典序.比如他的密码长度不大于2,那就需要依次检索a,b,..........,y,z,aa,ab,..........,zy,zz.输出每台数据检索器的检索区间,使得总的检索效率可以达到最高. 已知密码的总可能数不少于数据处理器个数.

&quot;is not on any development teams &quot; Xcode的账号错误问题

"***@*** is not on any development teams " Xcode现在在"Accounts"中时不时会出现这个提示. 但其他按钮都是灰色的, 也没有"确定" "刷新"按钮, 实在无奈. 解决方法: 1:进入 "Organizer"页面, 选 "Archives", 2:随便找一个你的archives包, 3:选择右边的Validate, 这时会验证你的开发者

Hdu 1106 排序 (atoi函数与 strtok函数的应用

链接: http://acm.hdu.edu.cn/showproblem.php?pid=1106 好久都没刷题了,今天突然特别怀念以前刷题的日子,所以就找了几道水题来做做~~呵呵 在写这篇博客之前呢,已经很明了自己已经大三了,时光匆忙,在还没来得及转过头来,就已经过了两年了大学,原来走了这么远了~~感觉再怎么样,大三还是不敢偷懒~~不过有时候还真是挺迷茫的说实话,在这里呢,真心希望能得到你们各位的建议,只要是对我未来就业有好处的,我都会虚心取纳,(BTW:我是学嵌入式方向的)万分感谢~~~

uva10911 - Forming Quiz Teams(记忆化搜索)

题目:uva10911 - Forming Quiz Teams(记忆化搜索) 题目大意:给出N对点的坐标,然后将这2 * N个点分组,Xi代表第i组的点之间的距离,求sum(Xi)最小值. 解题思路:这里的点最多16个,如果暴力求解的话16!,会超时的.这里的点取和不取可以用0和1表示,这样的话所有的状态可以用二进制数X来表示.dp[X]  = Min (dp[newx] + d[i][j]):记录下状态来减少不必要的重复操作. i .j代表点的下标,这些点必须是X状态下没取过的,newx就是