S - Two Teams

Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u

Description

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

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

类似二分图,但是可以用dfs+染色的方法解决

#include<stdio.h>
#include<vector>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;
const int maxn=;
int color[maxn];
bool vis[maxn];
vector<int>g[maxn];
int ans[maxn]; void dfs(int u){
vis[u]=true;
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;
while(scanf("%d",&n)!=EOF){
memset(vis,false,sizeof(vis));
memset(color,,sizeof(color));
memset(ans,,sizeof(ans));
int x;
for(int i=;i<=n;i++){
g[i].clear();
for(int j=;;j++){
scanf("%d",&x);
if(!x)
break;
g[i].push_back(x);
}
}
for(int i=;i<=n;i++){
if(!vis[i]){
color[i]=;
dfs(i);
}
}
int sum=;
int cnt=-;
for(int i=;i<=n;i++){
if(color[i]==){
sum++;
ans[++cnt]=i;
}
}
printf("%d\n",sum);
for(int i=;i<=cnt;i++){
printf("%d%c",ans[i],i==cnt?'\n':' ');
}
}
return ;
}

URAL 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 (DFS)

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

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

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

  5. 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 ...

  6. 1106. Two Teams(dfs 染色)

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

  7. timus 1106 Two Teams(二部图)

    Two Teams Time limit: 1.0 secondMemory limit: 64 MB The group of people consists of N members. Every ...

  8. ural 1208 Legendary Teams Contest

    题意描述:给定K支队伍,每队三个队员,不同队伍之间队员可能部分重复,输出这些队员同时能够组成多少完整的队伍: DFS,利用DFS深度优先搜索,如果该队所有队员都没有被访问过,那么将该队计入结果,再去选 ...

  9. 二分图最大匹配(匈牙利算法) URAL 1721 Two Sides of the Same Coin

    题目传送门 /* 题意:三种人,statements,testdata,anthing.要求两个人能完成s和t两个工作,且rank相差2 二分图匹配:此题学习建图技巧,两个集和内部一定没有边相连,ra ...

随机推荐

  1. js字符串、数组、时间、日期对象

    js对字符串.数组.日期的操作是在以后项目中频繁使用的操作,所以呢....所以大家看着办,其实并不难哈,就是有点无聊,我承认这是我百度的,哈哈哈哈 <!DOCTYPE html><h ...

  2. 洛谷——普及练习场 普及组选手可冲刺训练,提高组选手亦可在此巩固基础。(Loading...)

    简单的模拟 关卡2-1,6 道题 开始普及组的训练!所谓模拟,就是直接根据题意编写,思维难度简单. //T1 铺地毯 #include <cstdio> #define N 10005 i ...

  3. [Docker] Docker安装和简单指令

    Docker笔记 安装 sudo apt install docker.io 启动和关闭Docker服务 # 启动Docker服务 sudo service docker start # 关闭Dock ...

  4. jQuery JavaScript Library v3.2.1

    /*! * jQuery JavaScript Library v3.2.1 * https://jquery.com/ * * Includes Sizzle.js * https://sizzle ...

  5. JDK安装及环境变量配置详解

    一.下载(Jdk 1.8 ) 1.下载地址:http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151 ...

  6. HTML5<header>元素

    HTML5<header>元素 1.header元素描述了文档的头部区域,主要用于定义内容的介绍展示区域. 2.实例: <header> <h2>heder元素描述 ...

  7. ios sinaweibo 客户端(二)

    这一篇博文讲述发微博界面的实现. 首先我们先了解一下在这个发微博界面中需要做哪些事情吧! (1) 发微博包括文字内容和微博图片,所以我们可以用一个textview来装载微博文字内容,用一个imagev ...

  8. iOS深拷贝与浅拷贝

    概念 对象拷贝有两种方式:浅复制和深复制.顾名思义,浅复制,并不拷贝对象本身,仅仅是拷贝指向对象的指针:深复制是直接拷贝整个对象内存到另一块内存中. 如图详解:

  9. 01、Linux介绍

    一. Linux介绍 Linux是一套免费使用和自由传播的类Unix操作系统,是一个基于POSIX和UNIX的多用户.多任务.支持多线程和多CPU的操作系统.它能运行主要的UNIX工具软件.应用程序和 ...

  10. LeetCode之Weekly Contest 93

    第一题:二进制间距 问题: 给定一个正整数 N,找到并返回 N 的二进制表示中两个连续的 1 之间的最长距离. 如果没有两个连续的 1,返回 0 . 示例 1: 输入:22 输出:2 解释: 22 的 ...