题意

小组里有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. 关于Python装饰器内层函数为什么要return目标函数的一些个人见解

    https://blog.csdn.net/try_test_python/article/details/80802199 前几天在学装饰器的时候,关于装饰器内层函数调用目标函数时是否return目 ...

  2. ConcurrentHashMap实现解析

    ConcurrentHashMap是线程安全的HashMap的实现,具有更加高效的并发性.与HashTable不同,ConcurrentHashMap运用锁分离技术,尽量减小写操作时加锁的粒度,即在写 ...

  3. HTTP的常见状态码

    一.1xx(临时响应) 表示临时响应并需要请求者继续执行操作的状态码. 100(继续) 请求者应当继续提出请求.服务器返回此代码表示已收到请求的第一部分,正在等待其余部分. 101(切换协议) 请求者 ...

  4. Most efficient way to get the last element of a stream

    Do a reduction that simply returns the current value: Stream<T> stream; T last = stream.reduce ...

  5. uchome 缓存生成

    一.uchome的缓存目录 ---------data此目录要有777权限 (1)模板文件缓存机制 1:在要显示的页面通过include template($name) 语句来包含被编译后的模板文件 ...

  6. [WorldWind学习]19.WebDownload

    using System; using System.Diagnostics; using System.Globalization; using System.Net; using System.I ...

  7. PAT 1105 Spiral Matrix[模拟][螺旋矩阵][难]

    1105 Spiral Matrix(25 分) This time your job is to fill a sequence of N positive integers into a spir ...

  8. jQuery中的prop和attr区别

    最近在做一个项目用jq时发现一个问题  在谷歌中可以正常出效果  但是在火狐中就是不行 就是这个prop和attr   之前用的是attr方法   但是在火狐中不出效果  于是特意看了两者的区别 主要 ...

  9. 什么是T-SQL

    T-SQL T-SQL 即 Transact-SQL,是 SQL 在 Microsoft SQL Server 上的增强版,它是用来让应用程序与 SQL Server 沟通的主要语言.T-SQL 提供 ...

  10. 获取WebView加载的网页内容并进行动态修改

    http://www.jianshu.com/p/3f207a8e32cb [Android]WebView读取本地图片 http://www.cnblogs.com/kimmy/p/4769788. ...