题目链接

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 ;
}

最新文章

  1. switch语句的妙用
  2. applicationContext.xml的基本配置文件
  3. java学习中一些疑惑解答(2)
  4. 有return语句情况下,try-catch-finally的执行顺序
  5. PHP-----数据类型,运算符
  6. @SuppressWarnings有什么用处?
  7. js数据类型判断和数组判断
  8. Raw-OS源代码分析之消息系统-Queue_Size
  9. 使用oracle写if判断
  10. 使用git上传项目
  11. C++递归求解N个元素的所有子集
  12. 【工作笔记四】去掉a标签超链接的虚线框的方法
  13. OpenStack云平台的网络模式及其工作机制
  14. java.util报错
  15. 台达PLC modbus 不支持04功能码
  16. 简单代码生成csv文件(excel)
  17. Spring Boot + Spring Cloud 构建微服务系统(八):分布式链路追踪(Sleuth、Zipkin)
  18. Sitecore安装(手动方式)
  19. linux I/O状态实时监控iostat
  20. 面图层拓扑检查和错误自动修改—ArcGIS案例学习笔记

热门文章

  1. CentOS 8上安装Docker
  2. Python学习day43-数据库(多表关系)
  3. matlab 实现感知机线性二分类算法(Perceptron)
  4. 常用有三种json解析jackson、fastjson、gson。
  5. 如何将Map键值的下划线转为驼峰
  6. Gym - 102021E
  7. Python中的urlparse、urllib抓取和解析网页(一)
  8. 全栈之路-微信小程序-SKU开发(分析)
  9. springcloud-sleuth实现日志的链路追踪
  10. [欧拉路]CF1152E Neko and Flashback