题目大意:一国有n个党派,每个党派在议会中都有2个代表,

现要组建和平委员会,要从每个党派在议会的代表中选出1人,一共n人组成和平委员会。

已知有一些代表之间存在仇恨,也就是说他们不能同时被选为和平委员会的成员,

现要你判断满足要求的和平委员会能否创立?如果能,请任意给出一种方案。( POI 0106 )

—————————————————————————————————————————

这道题就是裸的2—SAT

不过我用了tarjan缩点 然后一波拓排

tips:一个问题无解当且仅当一个集合的两个点在同一个联通块里面

所以特判完无解之后一波拓排就可以解决问题辣

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
using std::min;
const int M=;
int read(){
int ans=,f=,c=getchar();
while(c<''||c>''){if(c=='-') f=-; c=getchar();}
while(c>=''&&c<=''){ans=ans*+(c-''); c=getchar();}
return ans*f;
}
int n,m;
int next(int x){return x&?x+:x-;}
struct node{int from,to,next;}e[*M],q[*M];
int first[M],cnt;
void ins(int a,int b){e[++cnt]=(node){a,b,first[a]}; first[a]=cnt;}
int star[M],sum;
void insq(int a,int b){q[++sum]=(node){a,b,star[a]}; star[a]=sum;}
int dfn[M],low[M],st[M],last[M],top,tot;
int color[M],h,in[M],op[M],ans[M];
void tarjan(int x){
dfn[x]=low[x]=++tot; st[++top]=x; last[x]=top;
for(int i=first[x];i;i=e[i].next){
int now=e[i].to;
if(!dfn[now]) tarjan(now),low[x]=min(low[x],low[now]);
else if(!color[now]) low[x]=min(low[x],dfn[now]);
}
if(dfn[x]==low[x]) for(h++;top>=last[x];top--) color[st[top]]=h;
}
bool f;
void topsort(){
std::queue<int>qu;
for(int i=;i<=h;i++)if(!in[i]) qu.push(i);
while(!qu.empty()){
int x=qu.front(); qu.pop();
if(!ans[x]) ans[x]=,ans[op[x]]=-;
for(int i=star[x];i;i=q[i].next){
int now=q[i].to; in[now]--;
if(!in[now]) qu.push(now);
}
}
}
void clear(){
f=false;
cnt=sum=h=tot=;
memset(color,,sizeof(color));
memset(last,,sizeof(last));
memset(dfn,,sizeof(dfn));
memset(st,,sizeof(st));
memset(low,,sizeof(low));
memset(in,,sizeof(in));
memset(first,,sizeof(first));
memset(star,,sizeof(star));
memset(op,,sizeof(op));
memset(ans,,sizeof(ans));
}
int main(){
int x,y;
while(scanf("%d %d",&n,&m)==){
clear();
for(int i=;i<=m;i++) x=read(),y=read(),ins(x,next(y)),ins(y,next(x));
for(int i=;i<=*n;i++) if(!dfn[i]) tarjan(i);
for(int i=;i<=n;i++)if(color[i<<]==color[next(i<<)]){printf("NIE\n"); f=true; break;}
else op[color[i<<]]=color[next(i<<)],op[color[next(i<<)]]=color[i<<];
if(f) continue;
for(int i=;i<=cnt;i++)if(color[e[i].from]!=color[e[i].to]) insq(color[e[i].to],color[e[i].from]),in[color[e[i].from]]++;
topsort();
for(int i=;i<=n;i++)
if(ans[color[i<<]]==) printf("%d\n",i<<);
else printf("%d\n",next(i<<));
}
return ;
}

HIT 1917 2—SAT的更多相关文章

  1. 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 ...

  2. HIT 1917 Peaceful Commission

    这道题题意就是给你n对人,一对中编号为x,x+1,给你m对矛盾,表示这两个人不能同时选. 然后就是Two-Sat的模板题了,就是根据对称性,连边,加缩点,最后拓扑排序,求出一组可行解就可以了. #in ...

  3. 2-SAT入门

    大概学了一下2-SAT,写了一道模板和一道USACO 输出一个方案的话,tarjan缩点后倒着拓扑,染色输出. 求任何解下选哪个就得枚举每个点dfs来判断选哪个. HIT 1917(2-sat模板) ...

  4. hdu 1814 2-sat 输出字典最小的和任意序列的 模板题

    /* 思路:http://blog.csdn.net/string_yi/article/details/12686873 hdu 1814 输出字典序最小的2-sat */ #include< ...

  5. Flume启动报错[ERROR - org.apache.flume.sink.hdfs. Hit max consecutive under-replication rotations (30); will not continue rolling files under this path due to under-replication解决办法(图文详解)

    前期博客 Flume自定义拦截器(Interceptors)或自带拦截器时的一些经验技巧总结(图文详解)   问题详情 -- ::, (SinkRunner-PollingRunner-Default ...

  6. [LeetCode] Design Hit Counter 设计点击计数器

    Design a hit counter which counts the number of hits received in the past 5 minutes. Each function a ...

  7. Buffer cache hit ratio性能计数器真的可以作为内存瓶颈的判断指标吗?

    Buffer cache hit ratio官方是这么解释的:“指示在缓冲区高速缓存中找到而不需要从磁盘中读取的页的百分比.” Buffer cache hit ratio被很多人当做判断内存的性能指 ...

  8. 多边形碰撞 -- SAT方法

    检测凸多边形碰撞的一种简单的方法是SAT(Separating Axis Theorem),即分离轴定理. 原理:将多边形投影到一条向量上,看这两个多边形的投影是否重叠.如果不重叠,则认为这两个多边形 ...

  9. LeetCode Design Hit Counter

    原题链接在这里:https://leetcode.com/problems/design-hit-counter/. 题目: Design a hit counter which counts the ...

随机推荐

  1. 20160120使用myeclipse一年开始转IntelliJ IDEA 15做以下总结

    20160120使用myeclipse一年开始费元星转IntelliJ IDEA 15做以下总结 1.输入psv就会看到一个psvm的提示,此时点击tab键一个main方法就写好了.psvm 也就是p ...

  2. js键盘相关知识总结

    一.相应事件 keydown:按下键盘上的任意键都可触发,按着不放则重复触发 keypress:按下键盘上的字符键时触发,按着不放则重复触发 keyup:释放按键时触发 事件顺序:keydown> ...

  3. 接口自动化测试框架Karate入门

    介绍 在这篇文章中,我们将介绍一下开源的Web-API自动化测试框架--Karate Karate是基于另一个BDD测试框架Cucumber来建立的,并且共用了一些相同的思想.其中之一就是使用Gher ...

  4. Python 3 学习笔记之——错误和异常

    1. 语法错误 Python 的语法错误被称为解析错,语法分析器会指出出错的代码行,并且在最先找到的错误的位置标记一个小小的箭头. >>> while True File " ...

  5. 深度可分卷积(Depthwise Separable Conv.)计算量分析

    上次读到深度可分卷积还是去年暑假,各种细节都有些忘了.记录一下,特别是计算量的分析过程. 1. 标准卷积和深度可分卷积 标准卷积(MobileNet论文中称为Standard Convolution, ...

  6. PHP 用Symfony VarDumper Component 调试

    Symfony VarDumper 类似 php var_dump() 官方文档写的安装方法 : 按照步骤 就可以在 running any PHP code  时候使用了 In order to h ...

  7. 解决ecplise安装mybatipse插件时报找不到jar包的错

    在安装mybatipse插件的时候一直报这个错,脑袋疼,在网上搜了半天也没有结果,最后摸索了半天解决了,这里先贴一张图 1.先找到eclipse的安装目录,然后把相应的jar包拷到plugins里去, ...

  8. lintcode-127-拓扑排序

    127-拓扑排序 给定一个有向图,图节点的拓扑排序被定义为: 对于每条有向边A--> B,则A必须排在B之前 拓扑排序的第一个节点可以是任何在图中没有其他节点指向它的节点 找到给定图的任一拓扑排 ...

  9. .Net MVC中使用css 和js

    @section script { <script language="javascript"> </script> }

  10. C# 获取方法所在的 命名空间 类名 方法名

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.D ...