题意

小组里有N个人,每个人都有一个或多个朋友在小组里。将小组分成两个队伍,每个队伍的任意一个成员都有至少一个朋友在另一个队伍。

思路

一开始觉得和前几天做过的一道2-sat(每个队伍任意两个成员都必须互相认识)相似然后就往那边想了……看了题解才发现这题很简单……

我们注意到同组里的人是互不影响的,所以一个人如果已经确定在哪组的话是不会对后面进入这个组的人产生影响的。

所以我们按顺序直接搜就可以了,每个人在他朋友里面找一个已经确定组的,然后放到对面组。如果他的所有朋友都没确定,把他随便放一个组就行了。

代码

[java]
import java.util.*;
import java.util.Map.Entry;
import java.math.*;

public class Main{
static int group[] = new int [205];
static Vector <Vector> adj = new Vector <Vector> ();
public static int divide(int p){
group[p+1] = -2;
for (int j = 0; j < adj.get(p).size(); j ++){
int v = (int) adj.get(p).get(j);
if (group[v] == -1){
return (group[p+1] = 1 - divide(v-1));
}
if (group[v] > -1){
return (group[p+1] = 1 - group[v]);
}
}
if (group[p+1] == -2) return (group[p+1] = 0);
return 0;
}
public static void main(String args[]){
Scanner cin = new Scanner(System.in);
int n = cin.nextInt();
for (int i = 1; i <= n; i ++){
int tmp = 0;
Vector <Integer> tmpv = new Vector <Integer> ();
while((tmp = cin.nextInt()) != 0){
tmpv.addElement(tmp);
}
adj.addElement(tmpv);
}
boolean ok = true;
for (int i = 0; i < adj.size(); i ++){
group[i+1] = -1;
if (adj.get(i).size() == 0){
ok = false;
break;
}
}
for (int i = 0; i < adj.size(); i ++){
group[i+1] = divide(i);
}
if (ok == false){
System.out.println("0");
}
else{
Vector <Integer> ans = new Vector <Integer> ();
for (int i = 1; i <= n; i ++){
if (group[i] == 0){
ans.addElement(i);
}
}
System.out.println(ans.size());
for (int i = 0; i < ans.size(); i ++){
if (i > 0) System.out.print(" ");
System.out.print(ans.get(i));
}
System.out.println("");
}

cin.close();
}
}
[/java]

URAL 1106 Two Teams (DFS)的更多相关文章

  1. URAL 1106 Two Teams二分图

    S - Two Teams Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submi ...

  2. ural 1106. Two Teams 二分图染色

    链接:http://acm.timus.ru/problem.aspx?space=1&num=1106 描述:有n(n<=100)个人,每个人有一个或多个朋友(朋友关系是相互的).将其 ...

  3. ural 1106 Two Teams

    http://acm.timus.ru/problem.aspx?space=1&num=1106 #include <cstdio> #include <cstring&g ...

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

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

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

  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 1137Bus Routes (dfs)

    Z - Bus Routes Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Subm ...

  9. ural 1208 Legendary Teams Contest

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

随机推荐

  1. HDU_3193_Find the hotel

    Find the hotel Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

  2. What are Traceroute, Ping, Telnet and Nslookup commands?

    https://help.maximumasp.com/KB/a445/connectivity-testing-with-ping-telnet-tracert-and-pathping-.aspx ...

  3. web框架们~Django~Flask~Tornado

    1.web框架本质 2.Django 3.Flask 4.Tornado

  4. SQL Server批量数据导出导入BCP&Bulk使用

    数据导出导入,首先考虑使用什么技术实现导出与导入利用BCP结合Bulk技术实现数据的导出与导入 1.bcp数据导出(这里是命令行方式),导出的数据需是格式化的,有两种方式可选 a.对传输的数据格式要求 ...

  5. mysql 出现的错误

    1:创建函数时提示:This function has none of DETERMINISTIC, NO SQL, or READS SQL DATA ,因为当开启log-bin时,函数必须有参数, ...

  6. PHP对象转数组

    Solution json_decode( json_encode( $obj ), true ): But why?You should have a look at the function na ...

  7. Django进阶 (二)

    规范 确立规范的好处: 代码可读性高 方便代码的定位极其查找 为以后代码扩容带来便利 场景: 在多个APP的场景下,单个app的URL函数功能较多的时候,我们可以通过以下方法来解决. 把Views写成 ...

  8. G.Finding the Radius for an Inserted Circle 2017 ACM-ICPC 亚洲区(南宁赛区)网络赛

    地址:https://nanti.jisuanke.com/t/17314 题目: Three circles C_{a}C​a​​, C_{b}C​b​​, and C_{c}C​c​​, all ...

  9. 20155207 2016-2017-2 《Java程序设计》第八周学习总结

    20155207 2016-2017-2 <Java程序设计>第八周学习总结 教材学习内容总结 第15章 通用API 15.1 日志 15.1.1 日志API简介 java.util.lo ...

  10. bzoj1623 / P2909 [USACO08OPEN]牛的车Cow Cars

    P2909 [USACO08OPEN]牛的车Cow Cars 显然的贪心. 按速度从小到大排序.然后找车最少的车道,查询是否能填充进去. #include<iostream> #inclu ...