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