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. AD 域复制FRS 迁移到DFSR

    假设您尝试将在先前版本的Windows Server上运行的某个Active Directory域控制器(DC)升级到Windows Server 2019. 您可能会看到以下错误: “副本验证失败. ...

  2. LINQ新添知识

    linquser.ExecuteCommand("TRUNCATE TABLE Board");       linquser.ExecuteCommand("DELET ...

  3. javascript 和Jquery 互转

    jQuery对象转成DOM对象: 两种转换方式将一个jQuery对象转换成DOM对象:[index]和.get(index); (1)jQuery对象是一个数据对象,可以通过[index]的方法,来得 ...

  4. 【LeetCode】4.Median of Two Sorted Arrays 两个有序数组中位数

    题目: There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the ...

  5. Codeforces 724 G Xor-matic Number of the Graph 线性基+DFS

    G. Xor-matic Number of the Graph http://codeforces.com/problemset/problem/724/G 题意:给你一张无向图.定义一个无序三元组 ...

  6. UVA 11627 Slalom(二分)

    二分,判断的时候,一个点一个点的考虑肯定是不行啦,考虑的单位是一个区间, 每次左端点尽量向左边移动,右端点尽量向右,得到下次可以达到的范围,检查一下和下一个区间有没有交集. #include<b ...

  7. UVA 1609 Foul Play 不公平竞赛 (构(luan)造(gao)+递归)

    题意:有n支队伍(n是2的整数幂,2<=n<=4),打淘汰赛,胜者进入下一轮,其中1号队伍能打败至少一半的队伍,对于它不能打败的队伍l,一定存在一支它能够打败的队伍w,使得w能直接打败l, ...

  8. ANSI C 与 K&R C

    C语言由Dennis M.Ritchie在1973年设计和实现.从那以后使用者逐渐增加.到1978年Ritchie和Bell实验室的另一位程序专家Kernighan合写了著名的<TheC Pro ...

  9. Ubuntux下简单设置vim

    我自己在vim下的设置,基本写简单脚本用的,在~/.vimrc作出如下设置 syntax on "高亮 set nu "行号显示 set smartindent "基于a ...

  10. PHP 代码优化建议

    1.尽量静态化: 如果一个方法能被静态,那就声明它为静态的,速度可提高1/4,甚至我测试的时候,这个提高了近三倍.当然了,这个测试方法需要在十万级以上次执行,效果才明显.其实静态方法和非静态方法的效率 ...