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一个完全图也行或是一个完全图+一个孤点) 代码是 ...
随机推荐
- [原创]Java调用PageOffice给Word中的Table赋值
Word中的table操作需要借助数据区域(DataRegion)实现的,要求数据区域完整的包含了整个Table的内容,这样才可以通过数据区域控制和操作table.因此,要想使用table,则必须在w ...
- Django之模板语言(三)------>自定义filter
1.自定义filter: 1.在app01下面新建一个templatetags的python package包. 如果没有app01的话,可以通过命令行在manage中进行创建:python mana ...
- MySQL数据库--创建表,查询
MySQL创建表: 表(一)Student (学生表): CREATE TABLE `Student` ( `sno` ) DEFAULT NULL, `sname` ) DEFAULT NULL, ...
- sql server2008安装时提示重启计算机失败怎么办
安装SQL Server 2008时,经常会遇到这样一个问题,软件提示“重启计算机失败”,如果忽略的话,会给后面的安装带来很大的麻烦,这里如何解决呢? 工具/原料 注册表 解决方法 在键盘上按 ...
- Redis List类型学习
- 细说WPF自定义路由事件
WPF中的路由事件 as U know,和以前Windows消息事件区别不再多讲,这篇博文中,将首先回顾下WPF内置的路由事件的用法,然后在此基础上自定义一个路由事件. 1.WPF内置路由事件 W ...
- hibernate4注解字段为mysql的text
文章的正文detail就需要设置为text 在getter方法上添加注解 @Lob @Basic(fetch = FetchType.LAZY) @Type(type = "text&quo ...
- springcloud:Eureka的使用
1.认识Eureka 服务的管理. 问题分析 在刚才的案例中,itcast-service-provider对外提供服务,需要对外暴露自己的地址.而consumer(调用者)需要记录服务提供者的地址. ...
- Vuejs实战项目四:权限校验
路由跳转参考文档:https://router.vuejs.org/zh/guide/advanced/navigation-guards.html 在/src下创建permission.js进行权限 ...
- Spring Cloud Alibaba迁移指南(一):一行代码从 Hystrix 迁移到 Sentinel
摘要: 本文对Hystrix.Resilience4j.Sentinel进行对比,并探讨如何使用一行代码这种极简的方式,将Hystrix迁移到Sentinel. Hystrix 自从前段时间 宣布停止 ...
