313. [POI2001] 和平委员会

★★☆   输入文件:spo.in   输出文件:spo.out   评测插件
时间限制:1 s   内存限制:128 MB

题目描述

根据宪法,Byteland民主共和国的公众和平委员会应该在国会中通过立法程序来创立。 不幸的是,由于某些党派代表之间的不和睦而使得这件事存在障碍。

此委员会必须满足下列条件:

  • 每个党派都在委员会中恰有1个代表,
  • 如果2个代表彼此厌恶,则他们不能都属于委员会。

每个党在议会中有2个代表。代表从1编号到2n。 编号为2i-1和2i的代表属于第I个党派。

任务

写一程序:

  • 从文本文件读入党派的数量和关系不友好的代表对,
  • 计算决定建立和平委员会是否可能,若行,则列出委员会的成员表,
  • 结果写入文本文件。

输入

在文本文件的第一个行有2非负整数n和m。 他们各自表示:党派的数量n,1 < =n < =8000和不友好的代表对m,0 <=m <=20000。 在下面m行的每行为一对整数a,b,1<=a<b<=2n,中间用单个空格隔开。 p="" 它们表示代表a,b互相厌恶。<="">

输出

如果委员会不能创立,文本文件中应该包括单词NIE。若能够成立,文本文件SPO.OUT中应该包括n个从区间1到2n选出的整数,按升序写出,每行一个,这些数字为委员会中代表的编号。如果委员会能以多种方法形成,程序可以只写他们的某一个。

样品输入

3 2
1 3
2 4

样品输出

1
4
5

分析

2-sat问题模板题。

算法实现:

  1. 连边
  2. tarjan
  3. 判可行性,即同一集合中的两个点是否同属一个强连通块
  4. 缩点建新图,连反边
  5. 拓扑序,若当前点没有被访问过,则选择该点,不选择其另外的点

详细的文章中有,文章 :http://blog.csdn.net/jarjingx/article/details/8521690

code

 #include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<vector> #define oth(x) x&1?x+1:x-1 using namespace std; const int N = ; struct Edge{
int to,nxt;
}e[];
int head[N],dfn[N],low[N],st[N],bel[N];
bool vis[N];
int ru[N],q[N],opp[N],pr[N];
int tot_edge,n,nn,m,tot_node,top,cnt_block,L,R;
vector<int>mp[N]; inline char nc() {
static char buf[],*p1 = buf,*p2 = buf;
return p1==p2&&(p2=(p1=buf)+fread(buf,,,stdin),p1==p2)?EOF:*p1++;
}
inline int read() {
int x = ,f = ;char ch = nc();
for (; ch<''||ch>''; ch=nc()) if(ch=='-') f=-;
for (; ch>=''&&ch<=''; ch=nc()) x=x*+ch-'';
return x * f;
} void add_edge(int u,int v) {
e[++tot_edge].to = v;e[tot_edge].nxt = head[u];head[u] = tot_edge;
} void tarjan(int u) {
dfn[u] = low[u] = ++tot_node;
st[++top] = u;
vis[u] = true;
for (int i=head[u]; i; i=e[i].nxt) {
int v = e[i].to;
if (!dfn[v]) {
tarjan(v);
low[u] = min(low[v],low[u]);
}
else if (vis[v])
low[u] = min(dfn[v],low[u]);
}
if (low[u] == dfn[u]) {
++cnt_block;
do {
vis[st[top]] = false;
bel[st[top]] = cnt_block;
top--;
} while (st[top+] != u);
}
}
void toposort() {
L = ;R = ;
for (int i=; i<=cnt_block; ++i)
if (ru[i]==) q[++R] = i;
while (L <= R) {
int u = q[L++];
if (pr[u] != ) continue;
pr[u] = ;pr[opp[u]] = ;
int sz = mp[u].size();
for (int i=; i<sz; ++i) {
int v = mp[u][i];
ru[v]--;
if (ru[v]==) q[++R] = v;
}
}
}
bool work() {
for (int i=; i<=nn; ++i) {
if (!dfn[i]) tarjan(i);
}
for (int i=; i<=nn; ++i) {
if (bel[i] == bel[oth(i)]) return false;
opp[bel[i]] = bel[oth(i)];
opp[bel[oth(i)]] = bel[i];
}
for (int u=; u<=nn; ++u) {
for (int i=head[u]; i; i=e[i].nxt) {
int v = e[i].to;
if (bel[u] != bel[v]) {
ru[bel[u]] ++;
mp[bel[v]].push_back(bel[u]);
}
}
}
toposort();
return true;
}
int main () {
freopen("spo.in","r",stdin);
freopen("spo.out","w",stdout);
n = read(),m = read();nn = n<<;
for (int i=; i<=m; ++i) {
int a = read(),b = read();
add_edge(a,oth(b));
add_edge(b,oth(a));
}
if (work()) {
for (int i=; i<=nn; ++i)
if (pr[bel[i]]==) printf("%d\n",i); //-
}
else puts("NIE");
return ;
}

再放一发搜索代码

 #include<cstdio>
#include<algorithm>
#include<cstring> #define others(x) x&1?x+1:x-1 using namespace std; const int N = ; struct Edge{
int to,nxt;
}e[];
int head[N],mark[N],q[N];
int tot,c,n,m; inline char nc() {
static char buf[],*p1 = buf,*p2 = buf;
return p1==p2&&(p2=(p1=buf)+fread(buf,,,stdin),p1==p2)?EOF:*p1++;
}
inline int read() {
int x = ,f = ;char ch = nc();
for (; ch<''||ch>''; ch=nc()) if(ch=='-') f=-;
for (; ch>=''&&ch<=''; ch=nc()) x=x*+ch-'';
return x * f;
} void add_edge(int u,int v) {
e[++tot].to = v;e[tot].nxt = head[u];head[u] = tot;
}
bool dfs(int u) {
if (mark[u] != ) return mark[u] % ;
mark[u] = ;mark[others(u)] = ;
q[++c] = u;
for (int i=head[u]; i; i=e[i].nxt) {
int v = e[i].to;
if (!dfs(v)) return false;
}
return true;
}
bool work() {
for (int i=; i<=(n<<); ++i) {
if (mark[i]) continue;
c = ;
if (!dfs(i)) {
for (int j=; j<=c; ++j)
mark[q[j]] = mark[others(q[j])] = ;
if (!dfs(others(i))) return false;
}
}
return true;
} int main () {
freopen("spo.in","r",stdin);
freopen("spo.out","w",stdout);
n = read(),m = read();
for (int i=; i<=m; ++i) {
int a = read(),b = read();
add_edge(a,others(b));
add_edge(b,others(a));
}
if (work()) {
for (int i=; i<=(n<<); ++i)
if (mark[i]==) printf("%d\n",i);
}
else puts("NIE");
return ;
}

COGS:313. [POI2001] 和平委员会的更多相关文章

  1. cogs 313. [POI2001] 和平委员会(2-SAT

    http://cogs.pro:8080/cogs/problem/problem.php?pid=pyzQimjkj 题意:有n个集合,每个集合有俩元素,要从n个中各选一个放一堆,但是有的俩不能同时 ...

  2. [POI2001]和平委员会

    题目描述 根据宪法,Byteland民主共和国的公众和平委员会应该在国会中通过立法程序来创立. 不幸的是,由于某些党派代表之间的不和睦而使得这件事存在障碍. 此委员会必须满足下列条件: 每个党派都在委 ...

  3. 【POI2001】【HDU1814】和平委员会

    题面 Description 根据宪法,Byteland民主共和国的公众和平委员会应该在国会中通过立法程序来创立. 不幸的是,由于某些党派代表之间的不和睦而使得这件事存在障碍. 此委员会必须满足下列条 ...

  4. HDU 1814 Peaceful Commission / HIT 1917 Peaceful Commission /CJOJ 1288 和平委员会(2-sat模板题)

    HDU 1814 Peaceful Commission / HIT 1917 Peaceful Commission /CJOJ 1288 和平委员会(2-sat模板题) Description T ...

  5. [模板]2-SAT 问题&和平委员会

    tarjan的运用 this is a problem:link 2-SAT处理的是什么 首先,把「2」和「SAT」拆开.SAT 是 Satisfiability 的缩写,意为可满足性.即一串布尔变量 ...

  6. HDU1814和平委员会

    题目大意: 有n对的人,编号从1-2*n,m对的人之间互相不喜欢,每对人中必徐选1个人加入和平委员会,求字典序最小的解 -------------------------------- 2-SAT问题 ...

  7. LOJ10097和平委员会

    POI 2001 根据宪法,Byteland民主共和国的公众和平委员会应该在国会中通过立法程序来创立. 不幸的是,由于某些党派代表之间的不和睦而使得这件事存在障碍. 此委员会必须满足下列条件: 每个党 ...

  8. P5782-[POI2001]和平委员会【2-SAT】

    正题 题目链接:https://www.luogu.com.cn/problem/P5782 题目大意 \(n\)对人,每对之间恰好有一个人出席.\(m\)对仇恨关系表示两个人不能同时出席. 求是否有 ...

  9. 2-sat问题学习记录

    如果你不知道什么是sat问题,请看以下问答. Q:sat问题是什麽?A:首先你有n个布尔变量,然后你有一个关于这n个布尔变量的布尔表达式,问你,如果让你随意给这n个布尔变量赋值,这个布尔表达式能否成立 ...

随机推荐

  1. java递归展示树形图代码实现以及遇到的问题

    我最近写到了一个项目中用到了树形图,不得不说这个树形图是真的扯淡: 我用到的是layui中的树形图,再展示数据过程中遇到了很多的问题,废话不多说,直接贴代码. 一.调用排序接口,对数据进行排序. 二. ...

  2. 处理 wait millis 60009, active 50 ,maxactive 200 异常 过程

    处理 wait millis 60009, active 50 ,maxactive 200 异常 过程2018年04月19日 16:48:46 守望dfdfdf 阅读数:1910 标签: druid ...

  3. 消息中间件之MQ详解及四大MQ比较

    一.消息中间件相关知识 1.概述 消息队列已经逐渐成为企业IT系统内部通信的核心手段.它具有低耦合.可靠投递.广播.流量控制.最终一致性等一系列功能,成为异步RPC的主要手段之一.当今市面上有很多主流 ...

  4. hibernate课程 初探单表映射1-9 创建关系映射文件

    创建关系映射文件:(把实体类映射成一个表) 1 右键src==>new==>other==>hibernate==>hbm.xml==>Student==>Fini ...

  5. LaTeX 符号大全

    常用数学符号的 LaTeX 表示方法 2016-10-31 16:22 | 黄荣生   常用数学符号的 LaTeX 表示方法 1.指数和下标可以用^和_后加相应字符来实现.比如: 2.平方根(squa ...

  6. 【Shell脚本学习22】Shell 函数:Shell函数返回值、删除函数、在终端调用函数

    函数可以让我们将一个复杂功能划分成若干模块,让程序结构更加清晰,代码重复利用率更高.像其他编程语言一样,Shell 也支持函数.Shell 函数必须先定义后使用. Shell 函数的定义格式如下: f ...

  7. thymeleaf 模板语言简介

    参考网址: https://blog.csdn.net/mlin_123/article/details/51816533 1.1 Thymeleaf 在有网络和无网络的环境下皆可运行,而且完全不需启 ...

  8. VS功能扩展--扩展介绍

    使用Eclipse的朋友都知道Eclipse是一个完全可扩展的IDE,那么在windows程序开发时,我们常使用的IDE(Visual studio)是否具有功能的扩展性呢?毫无疑问,回答是肯定的.我 ...

  9. [opencv3.2cmake error ] sys/videoio.h no such file or directories

    I don't have /usr/include/sys/videoio.h at all Before that , I have ipp download question. So I down ...

  10. HDU5269 字典树

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5269 ,BestCoder Round #44的B题,关于字典树的应用. 比赛的时候没想出做法,现在补 ...