题目大意:一国有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. 环境变量 - JDK

    Linux 1. 备份并编辑配置文件 # cp /etc/profile /etc/profile.bak # vi /etc/profile 2. 设置JDK环境变量 export JAVA_HOM ...

  2. Java的HashMap和HashTable

    Java的HashMap和HashTable 1. HashMap 1)  hashmap的数据结构 Hashmap是一个数组和链表的结合体(在数据结构称“链表散列“),如下图示: 当我们往hashm ...

  3. Android开发免费类库和工具集合

    用于Android开发的免费类库和工具集合,按目录分类. Action Bars ActionBarSherlock Extended ActionBar FadingActionBar GlassA ...

  4. UIButton内部子控件自定义布局-“UIEdgeInsets”

    UIButton UIButton做frame动画时,不响应点击 在一个View内部加入几个按钮,然后改变这个view的frame来做动画,但是按钮不响应点击事件. 问题代码 __block CGRe ...

  5. vue2.0 watch

    类型:string | Function | Object vue官网解释: 一个对象,键是需要观察的表达式,值是对应回调函数.值也可以是方法名,或者包含选项的对象.Vue 实例将会在实例化时调用 $ ...

  6. Hyperledger04

    代码 'use strict'; var Fabric_Client = require('fabric-client'); var path = require('path'); var util ...

  7. 1.16. BIP39协议:使用助记词生成确定性钱包

    以太坊系统学习教程: https://www.netkiller.cn/blockchain/bip39.html 1.16. BIP39协议:使用助记词生成确定性钱包 BIP:39 层:应用层 标题 ...

  8. 第18讲——string类

    关键字:string类  字符串  C-风格字符串  C库字符串函数 字符串:存储在内存的连续字节中的一系列字符. C++处理字符串的方式有两种: 来自C语言,常被称为C-风格字符串: 基于strin ...

  9. 软工实践Beta冲刺(3/7)

    队名:起床一起肝活队 组长博客:博客链接 作业博客:班级博客本次作业的链接 组员情况 组员1(队长):白晨曦 过去两天完成了哪些任务 描述: 1.界面的修改与完善 展示GitHub当日代码/文档签入记 ...

  10. PHP全局变量局部变量

    http://www.w3school.com.cn/php/php_variables.asp