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=;
const int M=;
int power(int a,int b,int c){int ans=;while(b){if(b%==){ans=(ans*a)%c;b--;}b/=;a=a*a%c;}return ans;}
int color[N], vis[N];
vector<int> G[N];
void dfs(int u)
{
vis[u] = ;
for (int i = ; i < G[u].size(); ++i)
{
int v = G[u][i];
if (!vis[v])
{
color[v] = - color[u];
dfs(v);
}
}
}
int main()
{
int n, t;
scanf("%d", &n);
for (int i = ; i <= n; ++i)
while (scanf("%d", &t) && t)
{
G[i].push_back(t);
}
memset(vis, , sizeof(vis));
memset(color, , sizeof(color));
for (int i = ; i <= n; ++i)
if (!vis[i])
{
color[i]=;
dfs(i);
}
int sum = ;
for (int i = ; i <= n; ++i)
if (color[i] == )
++sum;
printf("%d\n", sum);
for (int i = ; i <= n; ++i)
if (color[i] == )
printf("%d ", i);
return ;
}

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

  1. ural 1106. Two Teams 二分图染色

    链接:http://acm.timus.ru/problem.aspx?space=1&num=1106 描述:有n(n<=100)个人,每个人有一个或多个朋友(朋友关系是相互的).将其 ...

  2. ural 1106 Two Teams

    http://acm.timus.ru/problem.aspx?space=1&num=1106 #include <cstdio> #include <cstring&g ...

  3. URAL 1106 Two Teams二分图

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

  4. 1106. Two Teams(dfs 染色)

    1106 结点染色 当前结点染为黑 朋友染为白  依次染下去 这题是为二分图打基础吧 #include <iostream> #include<cstdio> #include ...

  5. URAL 1106 Two Teams (DFS)

    题意 小组里有N个人,每个人都有一个或多个朋友在小组里.将小组分成两个队伍,每个队伍的任意一个成员都有至少一个朋友在另一个队伍. 思路 一开始觉得和前几天做过的一道2-sat(每个队伍任意两个成员都必 ...

  6. ural 1106,二分图染色,DFS

    题目链接:http://acm.timus.ru/problem.aspx?space=1&num=1106 乍一眼看上去,好像二分图匹配,哎,想不出和哪一种匹配类似,到网上查了一下,DFS染 ...

  7. Rnadom Teams

    Rnadom  Teams 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.actioncid=88890#problem/B 题目: Descript ...

  8. poj 1106 Transmitters (叉乘的应用)

    http://poj.org/problem?id=1106 Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 4488   A ...

  9. URAL 1208 Legendary Teams Contest(DFS)

    Legendary Teams Contest Time limit: 1.0 secondMemory limit: 64 MB Nothing makes as old as years. A l ...

随机推荐

  1. wsgi协议

    用来为server程序和app/framework程序做连接桥梁的,使server和app/framework各自发展,任意组合 上图是python3.4标准库里面,关于wsgiserver的实现.从 ...

  2. ByteArray

    ByteArray:属性endian:String == Endian.BIG_ENDIAN/Endian.LITTLE_ENDIAN.length:uint ByteArray的字节数positio ...

  3. matlab 画框(一)

    matlab进行图像处理之后,很多时候需要在图像上画出矩形框:如,调用matlab的某个检测函数,得到结果之后,往往需要将检测结果的矩形框画在图像上,直观.方便的进行查看:下面的代码就是这个目的: f ...

  4. Vm下 linux与windowsxp文件共享的方法

    我的PC Operating System Host是Windows XP,Guest是Linux,virtualPC是VMware workstation.方法介绍大全请参见:Windows与Vmw ...

  5. python3使用requests发闪存

    闪存ing.cnblogs.com是博客园类似推特.饭否的服务, 我写了以下程序可以完成发闪存的操作,目的是顺便练习使用requests库. requests是一个python 轻量的http客户端库 ...

  6. hdu 2097

    ps:WA了两次好像....Sky数是三个进制下的各位数之和相等...而不是都等于22...我傻逼了... 代码: #include "stdio.h" int inp(int a ...

  7. 为什么socket编程要用到多线程

    不得不佩服计算机先驱的设计:socket编程为什么需要多线程.如果只有一个ServerSocket线程,那么如下代码: public void start() throws Exception { S ...

  8. HackRF实现无线门铃信号分析重放

    文章特点:数据解码方面实在是没什么信心,存在分析错乱的可能性,所幸发出来共同探讨,恳请鞭策. 0x01 概述 这是一款工作在315Mhz频段的无线遥控门铃,根据查阅官方手册以及芯片信息,确定其采用了e ...

  9. 监听Android CTS测试项解决方案(一)

    前言: 首先这里需要详细叙述一下标题中"监听Android CTS测试项解决方案"的需求.这里的需求是指我们需要精确的监听到当前CTS测试正在测试的测试项. 因为我们知道CTS认证 ...

  10. LeetCode----3 Sum

    Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all un ...