题目大意:一国有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. 『AngularJS』Service

    理解Angular 服务 什么是Angular Service Angular 服务是为web应用执行特定任务的单例对象或方法. 注:如果组件是为了内容呈现的功能复用,那么服务就是为组件进行功能复用. ...

  2. python3.6 新特性学习

    #支持类型提示 typing { def greeting(name: str) -> str: return 'Hello ' + name #在函数greeting中,参数名称的类型为str ...

  3. vs code 在终端下使用 code ./ 打开当前项目

    Mac OS Visual Studio Code的扩展工具菜单中有Install command line的快捷安装 运行 VS code并打开命令面板( ⇧⌘P ),然后输入 shell comm ...

  4. 【iOS开发】NSOperation简单介绍

    iOS开发多线程篇—NSOperation简单介绍 一.NSOperation简介 1.简单说明 NSOperation的作⽤:配合使用NSOperation和NSOperationQueue也能实现 ...

  5. 远程sql数据库连接不上,provider: 命名管道提供程序, error: 40 - 无法打开到 SQL Server 的连接 错误解决

    错误信息: “ 标题: 连接到服务器------------------------------ 无法连接到 192.168.1.20. ------------------------------其 ...

  6. No node available for block: blk

    刚才利用hadoop和mahout运行kmean是算法,一开始利用了10个节点,一个master,9个slave,运行了7分钟,我为了看速度的变化,就改用伪分布的形式,但是一开始运行就报错了: 17/ ...

  7. Laravel 5 如何实现网站在维护模式下允许指定 IP 用户访问(白名单)

    为了测试需要,有时候需要在网站处于维护模式下允许特定IP访问网站,在 Laravel 中,这可以通过为维护模式编写自定义中间件来实现. 默认情况下,Laravel 使用 CheckForMainten ...

  8. vue2.0中父子组件之间的通信总结

    父组件: 子组件: 接受父组件的信息: 向父组件发送事件: (其中slot是插槽,可以将父组件中的<p>123</p>插入进来,如果父组件没有插入的内容,则显示slot内部的内 ...

  9. RabbitMQ 的行为艺术

    RabbitMQ 的行为艺术 目录 简介 环境搭建 示例一:简单的 Hello World 示例二:发布/订阅模式 尝试发现 - 新物种 EasyNetQ 简介 RabbitMQ:一个消息系统,基于 ...

  10. Sql Server性能优化辅助指标SET STATISTICS TIME ON和SET STATISTICS IO ON

    1.前言 对于优化SQL语句或存储过程,以前主要是用如下语句来判断具体执行时间,但是SQL环境是复杂多变的,下面语句并不能精准判断性能是否提高:如果需要精确知道CPU.IO等信息,就无能为力了. ), ...