Hdu 4751(2-SAT)
Divide Groups
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1153 Accepted Submission(s): 418Problem 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.InputThe 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.OutputIf divided successfully, please output "YES" in a line, else output "NO".Sample Input3
3 0
1 0
1 2 0Sample OutputYES
/*************************************************************************
> 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)的更多相关文章
- uva 10004 Bicoloring(dfs二分染色,和hdu 4751代码差不多)
Description In the ``Four Color Map Theorem" was proven with the assistance of a computer. This ...
- hdu 4751(dfs染色)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4751 思路:构建新图,对于那些两点连双向边的,忽略,然后其余的都连双向边,于是在新图中,连边的点是能不 ...
- HDU 4751 Divide Groups 2013 ACM/ICPC Asia Regional Nanjing Online
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4751 题目大意:判断一堆人能否分成两组,组内人都互相认识. 解题思路:如果两个人不是相互认识,该两人之 ...
- hdu 4751 2013南京赛区网络赛 二分图判断 **
和以前做过的一个二分图颇为相似,以前的是互相不认识的放在一组,这个是互相认识的,本质上是相同的 是 hdu 2444 #include<cstdio> #include<iostre ...
- HDU 4751 Divide Groups
题目链接 比赛时候,建图建错了.大体算法想到了,不过很多细节都没想好. #include <cstdio> #include <cstring> #include <cm ...
- hdu 4115 (2—SAT)
题意:两个人石头剪刀布,一个人的出法已确定,另一个人的出法有一定约束,某两次要相同或者不同,问你第二个人能否全部都不失败. 思路:根据Bob出的情况,我们可以确定每次Alice有两种方案. R与P,S ...
- hdu 4751
一道很简单的题,不过在比赛的时候没有写出来: 刚刚看到这个题,我以为是一个图论题,后来发现其实就是一个暴力的题: 用bfs,因为一个人与他不认识的人肯定不会在一个集合,如果判断出现冲突则分配失败,否则 ...
- hdu 4751 Divide Groups(dfs染色 或 2-sat)
Problem Description This year is the 60th anniversary of NJUST, and to make the celebration more c ...
- hdu 4751 Divide Groups bfs (2013 ACM/ICPC Asia Regional Nanjing Online 1004)
SDUST的训练赛 当时死磕这个水题3个小时,也无心去搞其他的 按照题意,转换成无向图,预处理去掉单向的边,然后判断剩下的图能否构成两个无向完全图(ps一个完全图也行或是一个完全图+一个孤点) 代码是 ...
随机推荐
- PAT甲级——A1095 Cars on Campus
Zhejiang University has 8 campuses and a lot of gates. From each gate we can collect the in/out time ...
- 《DSP using MATLAB》Problem 8.10
代码: %% ------------------------------------------------------------------------ %% Output Info about ...
- SQL ORM框架
[LINQ]using (SqlConnection conn = new SqlConnection(conStr)) { string sql = $@"select * from vi ...
- .Net Email操作类
using System; using System.Text; using System.Net.Mail; using System.Net; using System.Linq; using S ...
- 根据url的属性名来取属性值赋值给js
1.方法一:js的正则表达式:请求路径:http://127.0.0.1/pec/jsp/member/refundOrder.jsp?status=4 <script> var stat ...
- Redis Set ZSet类型的学习
- kuangbin带我飞QAQ 并查集
1. POJ 2236 给出N个点,一开始图是空白的,两个操作,一个是增加一个点(给出坐标),一个是查询两个点间是否相通,当两点间的距离小于D或者两点通过其他点间接相连时说这两个点相通.并查集维护,每 ...
- windows下bat批量处理启动exe
新建文本文档,start.dat start "" "D:\QQ\anzhaung\Bin\QQ.exe" 启动QQ cd ./当前文件夹下,../上一文件夹下 ...
- Ceisum官方教程1 -- 开始
原文地址:https://cesium.com/docs/tutorials/getting-started/ 学会使用全球地形.影像.3d tile(模型切片).地理编码创建一个Cesium程序. ...
- Effective Modern C++ 条款1:理解模板型别推导
成百上千的程序员都在向函数模板传递实参,并拿到了完全满意的结果,而这些程序员中却有很多对这些函数使用的型别是如何被推导出的过程连最模糊的描述都讲不出来. 但是当模板型别推导规则应用于auto语境时,它 ...
