题目大意:一国有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. springmvc基础篇—修改默认的配置文件名称及位置

    springmvc的默认配置文件是放在WEB-INF下的,叫action-servlet.xml.根据咱们编程的习惯,一般都将配置文件放到src的根目录下,那么如何将这个文件迁移过来呢?其实很简单,请 ...

  2. mybatis if标签比较字符串

    项目中需要在mybatis后台比较字符串 因为mybatis映射文件使用的是ognl表达式,所以不能使用 <if test="type == '0'"> 解决: < ...

  3. 「日常训练」Our Tanya is Crying Out Loud (CFR466D2B)

    题意(Codeforces 940B) 对一个数字$x$,你有两个决策:花费$A$减一.或花费$B$除以$k$(但必须可以除尽).问使之到$1$的最少花费. 分析 贼鸡儿简单,但我花式犯蠢……如果除不 ...

  4. 适用于Linux的windows子系统

    Windows基于图形界面的易用性是有目共睹的,这也是很多普通用户往往难以舍弃的原因.但是Linux系统更强大的网络应用开发能力,却又是Windows系统所无法比拟的.一直以来,很多人都在试图采用各种 ...

  5. Linux-Shell脚本编程-学习-7-总结前面开启后面的学习

    国庆前期后国庆回来也都比较忙,把书一放下,在那起来,就难了,发现好多都开始忘记了,今天好不容易硬着头片看来两章,算是马马虎虎的把前面的基础性质的只是看完了吧. 后面讲开始学习Shell编程的高级阶段, ...

  6. Qt_C++交换两个数

    在编程过程中,交换两个数是我用常用的 ,这里做下简单的搬运 bool Widget::swap(int a, int b) { int temp =a; a= b; b = temp; } 这种方式其 ...

  7. Kotlin操作符重载:把标准操作加入到任何类中(KAD 17)

    作者:Antonio Leiva 时间:Mar 21, 2017 原文链接:https://antonioleiva.com/operator-overload-kotlin/ 就像其他每种语言一样, ...

  8. MySQL☞dual虚拟表

    Dual表:虚拟表,专门用来测试各种函数:(本来以为跟Oracle中的dual表一样,发现还是不太一样)

  9. storm_jdbc 最完整的版本

    开头:我这里是根据bolt与trident进行分类的,写入和读取的方法可能会在同一个类中,最后会展示一个测试的类来说明怎么用. JdbcSpout:这个类是我写入数据和读取数据的公用spout,细节注 ...

  10. 【Solr】——Solr7安装教程

    前提 solr已经升级7.1,但是我们公司的solr还是使用的4.4,你们说low不low!!!重要的是,人家花费了大气将solr升级,从技术的角度来说solr7比solr4那是翻天覆地的改变! so ...