题意

小组里有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. android的一些类库的优缺点

    经过本人的面试经验,以及接触的android项目,总结了一下android的一些类库的优缺点: 一,线程方面 1.AsyncTask 首先是线程优化以及缺陷方面,针对目前大多数类库来说,都有好的设计方 ...

  2. eval(PHP 4, PHP 5)

    eval — 把字符串作为PHP代码执行 说明 mixed eval ( string $code_str ) 把字符串 code_str 作为PHP代码执行. 除了其他,该函数能够执行储存于数据库文 ...

  3. PAT 1127 ZigZagging on a Tree[难]

    1127 ZigZagging on a Tree (30 分) Suppose that all the keys in a binary tree are distinct positive in ...

  4. JavaScript修改CSS属性的实例代码

    用原生的javascript修改CSS属性的方法. 用JavaScript修改CSS属性 只有写原生的javascript了.  1.用JS修改标签的 class 属性值:  class 属性是在标签 ...

  5. 从getApplicationContext和getApplication再次梳理Android的Application正确用法

    原文地址http://blog.csdn.net/ly502541243/article/details/52105466 原文地址http://blog.csdn.net/ly502541243/a ...

  6. (3)在Windows7上搭建Cocos2d-x

    工具准备 搭建开发环境需要安装工具包括 Visual Studio python ———(本教程以python2.7.3版本为例),下载地址:http://www.python.org/downloa ...

  7. (转)在 ListViewItem 上拖动进行框选

    public partial class Form1 : Form { private bool IsMouseDown = false; Rectangle MouseRect = Rectangl ...

  8. js文件被浏览器缓存

    如果修改了js文件中的js代码,发布代码到线上后.用户的浏览器使用的还是原来js缓存.所以并不会马上生效. 如何才能让浏览器使用最新的js文件呢? 我去看了一下淘宝,发现也是这样一种方式额,不知道对不 ...

  9. uva672

      Gangsters  N gangsters are going to a restaurant. The i-th gangster comes at the time Ti and has t ...

  10. python 解析xml遇到xml.etree.ElementTree.ParseError: not well-formed (invalid token): line 4, column 34

    在调试数字驱动用xml文件的方式时,包含读取xml文件的步骤,运行程序报错: d:\test\0629>python XmlUtil.pyTraceback (most recent call ...