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 ;
}

最新文章

  1. [Android] 怎么在应用中实现密码隐藏?
  2. gloolooer
  3. apt-get install *** 出现 软件包***没有提供可供安装的候选者
  4. Azure CDN Smart Routing 功能正式上线
  5. jquery中prop()与attr()方法的区别
  6. Discuz API的延伸
  7. tortoisegit教程
  8. EMVTag系列13《脱机PIN》
  9. .NET DLL 保护措施详解(三)最终效果
  10. Asp.Net中文本换行
  11. hdu 2716 Message Decowding
  12. 构建可比较的对象(IComparable)
  13. windows7 java环境配置
  14. Failed global initialization:FileNotOpen: Failed to open &quot;C:\MongoDB\data\log\mongo.log&quot; 安装MongoDB时卡死
  15. Long类型时间如何转换成视频时长?
  16. 百度echarts样式开发
  17. 深度解析 Vue 响应式原理
  18. 贝塞尔曲线UIBezierPath简单使用
  19. Linux 之 AT&amp;T汇编语言 mov、add、sub指令、数据段
  20. vue-router 2 跳转失败原因

热门文章

  1. 数据库limit子句
  2. SharePoint 2010 缺少站点保存为模板选项
  3. Python+Selenium之摘取网页上全部邮箱
  4. fpga Verilog hdl 按键消抖 部分程序讲解
  5. 【Python图像特征的音乐序列生成】如何标记照片的特征
  6. xcdatamodel的实质
  7. Problem Q: C语言习题 计算该日在本年中是第几天
  8. python面试笔试题汇总
  9. 最全面的 python 字符串拼接总结(带注释版)
  10. kubernetes-平台日志收集(ELK)