题目链接

Divide Groups

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1153    Accepted Submission(s): 418

Problem Description

  This year is the 60th anniversary of NJUST, and to make the celebration more colorful, Tom200 is going to invite distinguished alumnus back to visit and take photos.
  After carefully planning, Tom200 announced his activity plan, one that contains two characters:
  1. Whether the effect of the event are good or bad has nothing to do with the number of people join in.
2. The more people joining in one activity know each other, the more interesting the activity will be. Therefore, the best state is that, everyone knows each other.
  The event appeals to a great number of alumnus, and Tom200 finds that they may not know each other or may just unilaterally recognize others. To improve the activities effects, Tom200 has to divide all those who signed up into groups to take part in the activity at different time. As we know, one's energy is limited, and Tom200 can hold activity twice. Tom200 already knows the relationship of each two person, but he cannot divide them because the number is too large.
  Now Tom200 turns to you for help. Given the information, can you tell if it is possible to complete the dividing mission to make the two activity in best state.
Input
  The input contains several test cases, terminated by EOF.
  Each case starts with a positive integer n (2<=n<=100), which means the number of people joining in the event.
  N lines follow. The i-th line contains some integers which are the id
of students that the i-th student knows, terminated by 0. And the id starts from 1.
Output
  If divided successfully, please output "YES" in a line, else output "NO".
Sample Input
3
3 0
1 0
1 2 0
Sample Output
YES
思路:对于任意一个游客,要么属于第一个集合,要么属于第二个集合,因为题中的“关系”是单向的,
所以当两个人之间只有一条单向边时就意味着:如果A属于第一个集合,那么B就一定属于第二个集合,反之亦然。
这样就可以建图了。
Accepted Code:
 /*************************************************************************
> File Name: 4751.cpp
> Author: Stomach_ache
> Mail: sudaweitong@gmail.com
> Created Time: 2014年08月09日 星期六 22时55分53秒
> Propose: 2-SAT
************************************************************************/
#include <cmath>
#include <string>
#include <cstdio>
#include <vector>
#include <fstream>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std; const int maxn = ;
bool a[maxn][maxn];
struct SCC {
int n;
vector<int> g[maxn<<];
vector<int> rg[maxn<<];
vector<int> vs;
bool vis[maxn<<];
int cmp[maxn<<];
void addEdge(int from, int to) {
g[from].push_back(to);
rg[to].push_back(from);
}
void init(int nn) {
this->n = nn * ;
for (int i = ; i <= n; i++) {
g[i].clear();
rg[i].clear();
}
vs.clear();
}
void dfs(int u) {
vis[u] = true;
for (int i = ; i < (int)g[u].size(); i++) {
int v = g[u][i];
if (!vis[v]) dfs(v);
}
vs.push_back(u);
}
void rdfs(int u, int k) {
vis[u] = true;
cmp[u] = k;
for (int i = ; i < (int)rg[u].size(); i++) {
int v = rg[u][i];
if (!vis[v]) rdfs(v, k);
}
}
int find_scc() {
memset(vis, false, sizeof(vis));
for (int i = ; i < n; i++) if (!vis[i]) dfs(i);
memset(vis, false, sizeof(vis));
int k = ;
for (int i = (int)vs.size()-; i >= ; i--)
if (!vis[vs[i]]) rdfs(vs[i], k++);
return k;
}
};
SCC A; int main(void) {
int n;
while (~scanf("%d", &n)) {
memset(a, false, sizeof(a));
for (int i = ; i < n; i++) {
int to;
while (scanf("%d", &to), to) {
a[i][to-] = true;
}
}
A.init(n);
for (int i = ; i < n; i++) {
for (int j = ; j < n; j++) {
if (a[i][j]&&!a[j][i] || !a[i][j]&&a[j][i]) {
A.addEdge(i, j + n);
A.addEdge(i + n, j);
}
}
}
A.find_scc();
bool flag = true;
for (int i = ; i < n; i++) {
if (A.cmp[i] == A.cmp[i+n]) {
flag = false;
break;
}
}
puts(flag ? "YES" : "NO");
}
return ;
}

Hdu 4751(2-SAT)的更多相关文章

  1. uva 10004 Bicoloring(dfs二分染色,和hdu 4751代码差不多)

    Description In the ``Four Color Map Theorem" was proven with the assistance of a computer. This ...

  2. hdu 4751(dfs染色)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4751 思路:构建新图,对于那些两点连双向边的,忽略,然后其余的都连双向边,于是在新图中,连边的点是能不 ...

  3. HDU 4751 Divide Groups 2013 ACM/ICPC Asia Regional Nanjing Online

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4751 题目大意:判断一堆人能否分成两组,组内人都互相认识. 解题思路:如果两个人不是相互认识,该两人之 ...

  4. hdu 4751 2013南京赛区网络赛 二分图判断 **

    和以前做过的一个二分图颇为相似,以前的是互相不认识的放在一组,这个是互相认识的,本质上是相同的 是 hdu 2444 #include<cstdio> #include<iostre ...

  5. HDU 4751 Divide Groups

    题目链接 比赛时候,建图建错了.大体算法想到了,不过很多细节都没想好. #include <cstdio> #include <cstring> #include <cm ...

  6. hdu 4115 (2—SAT)

    题意:两个人石头剪刀布,一个人的出法已确定,另一个人的出法有一定约束,某两次要相同或者不同,问你第二个人能否全部都不失败. 思路:根据Bob出的情况,我们可以确定每次Alice有两种方案. R与P,S ...

  7. hdu 4751

    一道很简单的题,不过在比赛的时候没有写出来: 刚刚看到这个题,我以为是一个图论题,后来发现其实就是一个暴力的题: 用bfs,因为一个人与他不认识的人肯定不会在一个集合,如果判断出现冲突则分配失败,否则 ...

  8. hdu 4751 Divide Groups(dfs染色 或 2-sat)

    Problem Description   This year is the 60th anniversary of NJUST, and to make the celebration more c ...

  9. hdu 4751 Divide Groups bfs (2013 ACM/ICPC Asia Regional Nanjing Online 1004)

    SDUST的训练赛 当时死磕这个水题3个小时,也无心去搞其他的 按照题意,转换成无向图,预处理去掉单向的边,然后判断剩下的图能否构成两个无向完全图(ps一个完全图也行或是一个完全图+一个孤点) 代码是 ...

随机推荐

  1. “Error: Encountered an improper argument”的解决方法

    之前遇到过的问题,后来解决后再次遇到又忘记了, 这是keil 的bug 路径只要都是字母就可以了

  2. js中的继承和重载

      js中有三种继承方式:一.通过原型(prototype)实现继承 二.借用构造函数式继承,可分为通过call()方法实现继承和通过apply()方法实现继承 仅仅通过原型继承我们可以发现在实例化子 ...

  3. .net core 使用swagger生成API文档

    [1]安装Swashbuckle.AspNetCore包 [2]在Startup.cs中注册swagger //注册Swagger生成器,定义一个和多个Swagger 文档 services.AddS ...

  4. 2018-10-31-win10-uwp-使用-asp-dotnet-core-做图床服务器客户端

    title author date CreateTime categories win10 uwp 使用 asp dotnet core 做图床服务器客户端 lindexi 2018-10-31 14 ...

  5. Errors were encountered while processing: mysql-server-5.5

    ubuntu 中运行完sudo apt-get install curl之后,最后出现: ldconfig deferred processing now taking place Errors we ...

  6. 再不懂时序就 OUT 啦!,DBengine 排名第一时序数据库,阿里云数据库 InfluxDB 正式商业化!

    云数据库 InfluxDB® 版介绍 阿里云数据库 InfluxDB® 版已于近日正式启动商业化 . 云数据库 InfluxDB® 是基于当前最流行的开源数据库 InfluxDB 提供的在线数据库服务 ...

  7. Django项目:CRM(客户关系管理系统)--70--60PerfectCRM实现CRM学生上课记录

    #urls.py """PerfectCRM URL Configuration The `urlpatterns` list routes URLs to views. ...

  8. 深入浅出 Java Concurrency (3): 原子操作 part 2[转]

    在这一部分开始讨论数组原子操作和一些其他的原子操作. AtomicIntegerArray/AtomicLongArray/AtomicReferenceArray的API类似,选择有代表性的Atom ...

  9. jeecmsv9-adminVue 打包出错

    F:\jeecms\jeecmsv9-adminVue>node build\build.js - building for production...Error processing file ...

  10. 组件:slot插槽

    <!DOCTYPE html> <html lang="zh"> <head> <title></title> < ...