题目链接

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. Django 异步任务、定时任务Celery

    将任务分配给其他的进程去运行,django的主进程只负责发起任务,而执行任务的不在使用django的主进程.Python有一个很棒的异步任务框架,叫做celery. Django为了让开发者开发更加方 ...

  2. rpm包安装过程中依赖问题“libc.so.6 is needed by XXX”解决方法-转

    原文:http://raksmart.idcspy.com/781 在CentOS上的Canon LBP2900安装打印机驱动,中间遇到了一些问题,主要是安装rpm包出现的依赖问题,现在解决了,现在简 ...

  3. mariadb ROW格式复制下从库结构变更引发1677错误

    stop slave;set global slave_type_conversions=ALL_LOSSY;start slave; 详细度娘slave_type_conversions的参数说明

  4. Activiti数据库

    数据库 Activiti的后台是有数据库的支持,所有的表都以ACT_开头. 第二部分是表示表的用途的两个字母标识. 用途也和服务的API对应. 1)     ACT_RE_*: 'RE'表示repos ...

  5. 如何转移Pycharm的设置或者缓存到其他盘

    因为Pycharm项目缓存C:\Users\wq\.PyCharm2017.2\system\caches下面的content.dat.storageData特别大,占用很多C盘空间,所以我就想办法, ...

  6. Redis高级命令的使用学习

  7. [转]绑定到异步的ObservableCollection

    在进行WPF开发过程中,需要从一个新的线程中操作ObservableCollection,结果程序抛出一个NotSupportedException的错误: This type of Collecti ...

  8. CA证书制作

    目录 手动制作CA证书 1.安装 CFSSL 2.初始化cfssl 3.创建用来生成 CA 文件的 JSON 配置文件 4.创建用来生成 CA 证书签名请求(CSR)的 JSON 配置文件 5.生成C ...

  9. Python-购物车系统

    # coding=utf-8 import os, pickle class color: def echo_error(self, red): print(f"\033[31;1m {re ...

  10. 同名的cookie会不会存在多个

    cookie new了多个.同一个名字.会不会存在多个呢. //若果不设置Cookie的path,则名字相同的Cookie视为相同的Cookie,后面的覆盖前面的,注意:大小写敏感 Cookie c1 ...