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 ...
随机推荐
- DotNetBar v12.5.0.2 Fully Cracked
更新信息: http://www.devcomponents.com/customeronly/releasenotes.asp?p=dnbwf&v=12.5.0.2 如果遇到破解问题可以与我 ...
- 【转发】RedHat Enterprise Linux 6.4 使用 Centos 6 的yum源问题
作为一名新手,学习Linux已经一个月了,其间遇到了不少问题,而今天笔者遇到的问题是 #yum install pam-devel #This system is not registered to ...
- 如何通过web查看job的运行情况
当我们将作业提交到hadoop 的集群上之后,我们会发现一个问题就是无法通过web查看job运行情况,比如启动了多少个map任务,启动多少个reduce任务啊,分配多少个conbiner等等.这些信息 ...
- lib静态链接库,dll动态链接库,h文件
最近在弄摄像头,发现我在调用摄像头自带的函数的时候,库没连接上,于是经过高人指点,学习了一下lib静态链接库,dll动态链接库来补充一下自己的基础知识. 一.首先我们来介绍一下lib静态链接库. li ...
- Python OpenCV ——Matplotlib显示图片
Color image loaded by OpenCV is in BGR mode.But Matplotlib displays in RGB mode.So color images will ...
- HDU1556-color the ball(线段树)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1556 #include<cstdio> using namespace std; stru ...
- HDU 4869 (递推 组合数取模)
Problem Turn the pokers (HDU 4869) 题目大意 有m张牌,全为正面朝上.进行n次操作,每次可以将任意ai张反面,询问n次操作可能的状态数. 解题分析 记正面朝上为1,朝 ...
- Smart210学习记录-------文件操作
一.linux文件操作(只能在linux系统上用) 创建:int creat(const char* filename, mode_t mode) filename 表示要创建的文件名,mode表示对 ...
- Magento给新产品页面添加分页
本文介绍如何让magento创建一个带分页功能的新到产品页面,方便我们在首页或者其它CMS Page调用和展示新到产品. 在Magento我们经常有的做法是建立一个可以调用新产品的block,然后通过 ...
- 即使连网了ping也会失败
/*************************************************************************** * 即使连网了ping也会失败 * 说明: * ...