#210. 【UER #6】寻找罪犯

链接:http://uoj.ac/problem/210

想法:2-sat模型。每个人拆点,分别表示为犯人、非犯人。每个句供词拆点,分别表示真话、假话。供词与对应人的点连双向边。假如$x_i$非犯人,那么连向他的所有真供词。一个假供词意味着至少一个犯人以及这个人的其他供词为真。于是连边。优化连边就可以用前缀,后缀的方式优化。

方案的输出参见:论文 博客

复杂度$O(n+m)$,常数有点大...

#include< algorithm >
#include< cstdio >
#include< vector > #define gec getchar
#define FILE(F) freopen(F".in","r",stdin),freopen(F".out","w",stdout)
#define DEBUG fprintf(stderr,"Passing [%s] in Line (%d)\n",__FUNCTION__,__LINE__); typedef long long ll;
template
inline void read(T&x)
{
x=0;bool f=0;char c=gec();
for(;c<'0'||c>'9';c=gec())f=(c=='-');
for(;c>='0'&&c<='9';c=gec())x=x*10+c-'0';
x=f?-x:x;
}
const int N(100010),M(100010),MAXN((N+M+M)<<1),MAXM(M*20);
int n,m,x,y,t;
std::vectorconf[N];
//(i<<1)为正点,(i<<1)|1为负点 [2,n<<1|1]为犯人,[n<<1+2,n<<1|1+(m<<1|1)]为供词
struct Node{int nd,nx;}bot[MAXM];
int tot,first[MAXN],total,col[MAXN],ant,limt;
void add(int a,int b)
{bot[++tot]=(Node){b,first[a]};first[a]=tot;}
void addedge(int a,int b){add(a,b);add(b,a);}
int min(int a,int b){return a>b?b:a;}
namespace Tarjan
{
int low[MAXN],dfn[MAXN],st[MAXN],tp,cnt;bool vis[MAXN],flag[MAXN];
void Dfs(int x)
{
low[x]=dfn[x]=++cnt; vis[x]=flag[x]=1; st[++tp]=x;
for(int v=first[x];v;v=bot[v].nx)
{
if(!vis[bot[v].nd])
{
Dfs(bot[v].nd);
low[x]=min(low[x],low[bot[v].nd]);
}else if(flag[bot[v].nd])low[x]=min(low[x],dfn[bot[v].nd]);
}
if(low[x]==dfn[x])
for(ant++;flag[x];tp--)flag[st[tp]]=0,col[st[tp]]=ant;
}
void run()
{
for(int i=2;i<=total;i++)
if(!vis[i])Dfs(i);
}
}
namespace Coloring
{
std::vectorbelong[MAXN];
int nx[MAXM],nd[MAXM],head[MAXN],color[MAXN],in[MAXN],tot,st[MAXN],tp,now,no;
//color 1:不选,2选,0未知
void add(int a,int b)
{nd[++tot]=b; nx[tot]=head[a]; head[a]=tot; in[b]++;}
bool Get_plan()
{
for(int i=1;i<=n+m;i++)
if(col[i<<1]==col[i<<1|1])return false;
for(int i=2;i<=total;i++)
{
if(i<=limt)belong[col[i]].push_back(i);//不用管虚点
for(int v=first[i];v;v=bot[v].nx)
if(col[i]!=col[bot[v].nd])add(col[bot[v].nd],col[i]);
}
for(int i=1;i<=ant;i++)
if(!in[i])st[++tp]=i;
while(tp)
{
now=st[tp--];
no=color[now];
for(int v=0,sz=belong[now].size();v<sz;v++)
if(color[col[belong[now][v]^1]]==2)no=1;
color[now]=no?1:2;
for(int v=head[now];v;v=nx[v])
{
in[nd[v]]--; color[nd[v]]|=no;
if(!in[nd[v]])st[++tp]=nd[v];
}
}
for(int i=1;i<=n;i++)
if(color[col[i<<1|1]]==2)st[++tp]=i;
std::sort(st+1,st+1+tp);
printf("%d\n",tp);
for(int i=1;i<=tp;i++)
printf("%d ",st[i]);
return true;
} }
int main()
{
#ifndef ONLINE_JUDGE
FILE("C");
#endif
read(n);read(m);
for(int i=1,now;i<=m;i++)
{
read(x),read(y),read(t);
now=(i+n)<<1;
conf[x].push_back(now);
addedge(now,(y<<1)|(t^1));
addedge(now|1,(y<<1)|t);
}
limt=total=(n+m)<<1|1;
for(int i=1,last;i<=n;i++)
{
/*for(int v=0,sz=conf[i].size();v<sz;v++)
{
for(int u=0;u<sz;u++)
if(u!=v)add(conf[i][v]^1,conf[i][u]);
add(conf[i][v]^1,i<<1|1);
add(i<<1,conf[i][v]);
}*/
last=0;
for(int v=0,sz=conf[i].size();v<sz;v++)
{
if(last)add(conf[i][v]^1,last);
if(last)add(last+1,last);
add(last=++total,conf[i][v]);
add(conf[i][v]^1,i<<1|1);
}
last=0;
for(int sz=conf[i].size(),v=sz-1;v>=0;v--)
{
if(last)add(conf[i][v]^1,last);
if(last)add(last+1,last);
add(last=++total,conf[i][v]);
add(i<<1,conf[i][v]);
}
}
Tarjan::run();
if(!Coloring::Get_plan())printf("Impossible");
return 0;
}

UOJ#210. 【UER #6】寻找罪犯 2-sat的更多相关文章

  1. uoj #210. 【UER #6】寻找罪犯【2-SAT】

    首先最直观的,列一排是罪犯一排不是罪犯,对于一个条件u说v(0是1否)f罪犯,如果u不是,那么vf罪犯:如果u是,枚举他说谎的一条wg罪犯,令w(g^1)罪犯连其他条的vf 但是这样有个电度数方,会炸 ...

  2. 【UOJ #210】【UER #6】寻找罪犯

    题目描述 通过一些不可描述的方式,妹滋滋算出了 51% 的得票率,于是就她就把这个公开给了广大用户 —— UOJ 解散已成定局. 几个小时后,UOJ 创始人伏特跳蚤国王宣布辞职,即日起退出 UOJ 团 ...

  3. [UOJ210]寻找罪犯

    2-sat神题.. 告诉是2-sat我也完全想不到正解. 看了看题解其实一步步分析也不算很难 这个题首先是要围绕每个人是否是犯人和每句话是否是真话来思考 首先要明确的是: 1.好人不说谎话 2.说了谎 ...

  4. UOJ #455 [UER #8]雪灾与外卖 (贪心、模拟费用流)

    题目链接 http://uoj.ac/contest/47/problem/455 题解 模拟费用流,一个非常神奇的东西. 本题即为WC2019 laofu的讲课中的Problem 8,经典的老鼠进洞 ...

  5. [UOJ#245][UER#7]天路(近似算法)

    允许5%的相对误差,意味着我们可以只输出$\log_{1.05} V$种取值并保证答案合法.并且注意到答案随着区间长度而单增,故取值不同的答案区间是$O(\log_{1.05} V)$的. 于是初始x ...

  6. 2-sat问题学习记录

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

  7. P4478 [BJWC2018]上学路线

    Description 小B 所在的城市的道路构成了一个方形网格,它的西南角为(0,0),东北角为(N,M). 小B 家住在西南角,学校在东北角.现在有T 个路口进行施工,小B 不能通过这些路口.小B ...

  8. Tarjan/2-SAT学习笔记

    Tarjan/2-SAT Tags:图论 作业部落 评论地址 Tarjan 用来求割边或者割点,求点双联通分量或者边双联通分量 点双联通分量:两个点之间有两条点不相交的路径 边双联通分量:两个点之间有 ...

  9. Tarjan&2-SAT 总结

    \(Tarjan\)&\(2-SAT\) 标签: 知识点总结 安利XZYXZY ps:里面的部分东西来自\(Anson\)和\(yler\)和\(XZY\) 阅读体验:https://zybu ...

随机推荐

  1. Libvirt磁盘加密

    Libvirt加密磁盘使用 创建加密磁盘 进入libvirt默认存储池目录 # cd /var/lib/libvirt/images 创建加密磁盘 # qemu-img convert -O qcow ...

  2. Redis常见7种使用场景(PHP)

    转发:https://www.jianshu.com/p/2f3add45351b Redis是一个开源的使用ANSI C语言编写.支持网络.可基于内存亦可持久化的日志型.Key-Value数据库,并 ...

  3. Highest Price in Supply Chain (25)(DFS)(PAT甲级)

    #include<bits/stdc++.h>using namespace std;int fa;int degree[100007];vector<int>v[100007 ...

  4. AIM Tech Round 5 (rated, Div. 1 + Div. 2) D(SET,思维)

    #include<bits/stdc++.h>using namespace std;const long long mod = 1e9+7;char s[370007][27];long ...

  5. Spark TaskScheduler 概述

    TaskScheduler 原理: 1. DAGScheduler 在提交Taskset给底层调度器的时候是面向接口TaskScheduler的, 这符合面向对象中依赖抽象原则,带来底层资源调度器的可 ...

  6. 洛谷P1034 矩形覆盖

    P1034 矩形覆盖 题目描述 在平面上有 n 个点(n <= 50),每个点用一对整数坐标表示.例如:当 n=4 时,4个点的坐标分另为:p1(1,1),p2(2,2),p3(3,6),P4( ...

  7. 关于Android模块化我有一些话不知当讲不当讲

    关于Android模块化我有一些话不知当讲不当讲 最近公司一个项目使用了模块化设计,本人参与其中的一个小模块开发,但是整体的设计并不是我架构设计的,开发半年有余,在此记录下来我的想法. 关于Andro ...

  8. JSP-用网页输出乘法表 三角形及菱形

    <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding= ...

  9. spring mvc做上传图片,文件小于10k就不生成临时文件了

    这是spring-mvc.xml中的 <bean id="multipartResolver" class="org.springframework.web.mul ...

  10. angular中[hidden]="expression"注意事项

    [hidden]="expression",右侧的表达式尽量使用布尔值:虽然比较运算符也可以达到效果,但时常会出现一些莫名其妙的错误.