timus 1106 Two Teams(二部图)
Two Teams
Memory limit: 64 MB
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
Output
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 |
4 |
#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(二部图)的更多相关文章
- ural 1106. Two Teams 二分图染色
链接:http://acm.timus.ru/problem.aspx?space=1&num=1106 描述:有n(n<=100)个人,每个人有一个或多个朋友(朋友关系是相互的).将其 ...
- ural 1106 Two Teams
http://acm.timus.ru/problem.aspx?space=1&num=1106 #include <cstdio> #include <cstring&g ...
- URAL 1106 Two Teams二分图
S - Two Teams Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submi ...
- 1106. Two Teams(dfs 染色)
1106 结点染色 当前结点染为黑 朋友染为白 依次染下去 这题是为二分图打基础吧 #include <iostream> #include<cstdio> #include ...
- URAL 1106 Two Teams (DFS)
题意 小组里有N个人,每个人都有一个或多个朋友在小组里.将小组分成两个队伍,每个队伍的任意一个成员都有至少一个朋友在另一个队伍. 思路 一开始觉得和前几天做过的一道2-sat(每个队伍任意两个成员都必 ...
- ural 1106,二分图染色,DFS
题目链接:http://acm.timus.ru/problem.aspx?space=1&num=1106 乍一眼看上去,好像二分图匹配,哎,想不出和哪一种匹配类似,到网上查了一下,DFS染 ...
- Rnadom Teams
Rnadom Teams 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.actioncid=88890#problem/B 题目: Descript ...
- poj 1106 Transmitters (叉乘的应用)
http://poj.org/problem?id=1106 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 4488 A ...
- 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 ...
随机推荐
- 戴文的Linux内核专题:02源代码
转自Linux中国 在下载并解压内核源代码后,用户可以看到许多文件夹和文件.尝试去找一个特定的文件或许是一个挑战.谢天谢地,源代码以一个特定的方式组织的.这使开发者能够轻松找到任何文件或者内核的一部分 ...
- Problem K 栈
Description A math instructor is too lazy to grade a question in the exam papers in which students a ...
- Oracle常用的函数
1.常用的函数分为五大类: 字符函数.数字和日期函数.数字函数.转换函数.混合函数 2.字符函数 字符函数主要用于修改字符列.这些函数接受字符输入,返回字符或数字值.Oracle 提供的一些字符函数如 ...
- 获取UIColor中的RGB值(本人亲测多个获取RGB值的方法,这个最有效)
在自己研发的项目个人项目中,碰到一个从颜色中获取RGB值的需求. 在网上找了许久,也有一些方法可以获取RGB值,但不能获取黑白以及灰色的值(他们是非RGB颜色空间,不清楚什么意思,反正亲测确实获取不了 ...
- JS学习第一课
1.js 按照编写顺序执行 2.输出使用document.write. 3.申明数组 var array = [1,2,3,5] ; var arrStr = ["sgsg",& ...
- java.util 集合框架集合
java的集合框架为程序提供了一种处理对象组的标准方式.设计了一系列标准泛型接口: ⑴Collection ()接口,扩展了Iterable接口,位于集合层次结构的顶部,因此所有的集合都实现Colle ...
- windows防火墙添加规则
#include <windows.h> #include <crtdbg.h> #include <netfw.h> #include <objbase.h ...
- 从对SAE的一次授权安全评估浅谈云安全
EMail: jianxin#80sec.comSite: http://www.80sec.comDate: 2011-12-20From: http://www.80sec.com/ [ 目录 ...
- .NET快速开发平台(DevExpress)免费下载
Express开发的eXpressApp Framework为简单快速实现商业应用的提供了有效的手段.强大的模块化结构支持跨平台的特定域扩展.典型应用实例包括:商业/销售/客户关系管理系统,项目,文档 ...
- Ubuntu 开启ssh
sudo apt-get install openssh-server Ubuntu缺省安装了openssh-client,所以在这里就不安装了,如果你的系统没有安装的话,再用apt-get安装上即可 ...