BZOJ2199: [Usaco2011 Jan]奶牛议会
趁此机会学了一下2-SAT。
以前的2-SAT都是用并查集写的,只能应用于极小的一部分情况,这次学了一正式的2-SAT,是用一张有向图来表示其依赖关系。
2-SAT的介绍参见刘汝佳《训练指南》。
/**************************************************************
Problem: 2199
User: zhuohan123
Language: C++
Result: Accepted
Time:140 ms
Memory:1388 kb
****************************************************************/ #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int n,m;
struct point{int y,n;}p[];int pnum;
int head[];
struct edge{int next,to;}g[];int gnum;
void addedge(int from,int to)
{
g[++gnum].to=to;g[gnum].next=head[from];head[from]=gnum;
}
int q[],l,r;
bool cango[];
bool check(int po)
{
memset(cango,,sizeof cango);
cango[po]=true;l=;r=;q[++r]=po;
while(l<=r)
for(int i=head[q[l++]];i;i=g[i].next)
if(!cango[g[i].to])cango[q[++r]=g[i].to]=true;
for(int i=;i<=n;i++)
if(cango[p[i].y]&&cango[p[i].n])return false;
return true;
}
char ans[];
int main(int argc, char *argv[])
{
#ifndef ONLINE_JUDGE
freopen("1.in","r",stdin);
freopen("1.out","w",stdout);
#endif
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++)p[i].n=++pnum,p[i].y=++pnum;
while(m--)
{
int b,c;char vb[],vc[];
scanf("%d%s%d%s",&b,vb,&c,vc);
if(vb[]=='Y'&&vc[]=='Y')addedge(p[b].n,p[c].y),addedge(p[c].n,p[b].y);
if(vb[]=='Y'&&vc[]=='N')addedge(p[b].n,p[c].n),addedge(p[c].y,p[b].y);
if(vb[]=='N'&&vc[]=='Y')addedge(p[b].y,p[c].y),addedge(p[c].n,p[b].n);
if(vb[]=='N'&&vc[]=='N')addedge(p[b].y,p[c].n),addedge(p[c].y,p[b].n);
}
for(int i=;i<=n;i++)
{
bool by=check(p[i].y),bn=check(p[i].n);
if(by&&bn)ans[i]='?';
else if(by)ans[i]='Y';
else if(bn)ans[i]='N';
else {puts("IMPOSSIBLE");return ;}
}
puts(ans+);
return ;
}
P.S.这个代码写的相当非主流啊!
BZOJ2199: [Usaco2011 Jan]奶牛议会的更多相关文章
- BZOJ2199: [Usaco2011 Jan]奶牛议会(2-SAT)
Time Limit: 10 Sec Memory Limit: 259 MBSubmit: 559 Solved: 360[Submit][Status][Discuss] Descriptio ...
- BZOJ2199[Usaco2011 Jan]奶牛议会——2-SAT+tarjan缩点
题目描述 由于对Farmer John的领导感到极其不悦,奶牛们退出了农场,组建了奶牛议会.议会以“每头牛 都可以获得自己想要的”为原则,建立了下面的投票系统: M只到场的奶牛 (1 <= M ...
- 【BZOJ2199】[Usaco2011 Jan]奶牛议会 2-SAT
[BZOJ2199][Usaco2011 Jan]奶牛议会 Description 由于对Farmer John的领导感到极其不悦,奶牛们退出了农场,组建了奶牛议会.议会以“每头牛 都可以获得自己想要 ...
- BZOJ 2199: [Usaco2011 Jan]奶牛议会
2199: [Usaco2011 Jan]奶牛议会 Time Limit: 10 Sec Memory Limit: 259 MBSubmit: 375 Solved: 241[Submit][S ...
- 【BZOJ2199】 [Usaco2011 Jan]奶牛议会
Description 由于对Farmer John的领导感到极其不悦,奶牛们退出了农场,组建了奶牛议会.议会以“每头牛 都可以获得自己想要的”为原则,建立了下面的投票系统: M只到场的奶牛 (1 & ...
- BZOJ 2199: [Usaco2011 Jan]奶牛议会 [2-SAT 判断解]
http://www.lydsy.com/JudgeOnline/problem.php?id=2199 题意:裸的2-SAT,但是问每个变量在所有解中是只能为真还是只能为假还是既可以为真又可以为假 ...
- BZOJ.2199.[USACO2011 Jan]奶牛议会(2-SAT)
题目链接 建边不说了.对于议案'?'的输出用拓扑不好判断,直接对每个议案的结果DFS,看是否会出现矛盾 Tarjan也用不到 //964kb 76ms #include <cstdio> ...
- 2199: [Usaco2011 Jan]奶牛议会 2-sat
链接 https://www.luogu.org/problemnew/show/P3007 https://www.lydsy.com/JudgeOnline/problem.php?id=2199 ...
- bzoj 1823: [JSOI2010]满汉全席 && bzoj 2199 : [Usaco2011 Jan]奶牛议会 2-sat
noip之前学的内容了,看到题竟然忘了怎么建图了,复习一下. 2-sat 大概是对于每个元素,它有0和1两种选择,必须选一个但不能同时选.这之间又有一些二元关系,比如x&y=1等等... 先把 ...
随机推荐
- JLink V8初始化exynos4412脚本
/** ****************************************************************************** * @author Maox ...
- [terry笔记]更改oracle用户名
更改oracle的用户名 之前有个需求,整理一个schema的表.索引等规划到一个表空间里,利用expdp/impdp然后remap就完成了,但是整理好的用户名remap变更了,应用又不想修改其连接信 ...
- [div+css布局]命名规则
//首页可能碰到的 页头:header登录条:loginBar标志:logo侧栏:sideBar广告:banner导航:nav子导航:subNav菜单:menu子菜单:subMenu搜索:search ...
- HTML5 API 之 history
*:first-child { margin-top: 0 !important; } body>*:last-child { margin-bottom: 0 !important; } /* ...
- [转]C++编写Config类读取配置文件
//Config.h #pragma once #include <string> #include <map> #include <iostream> #incl ...
- Mybatis动态SQL
1.动态SQL基本标签 •if •choose (when, otherwise) •trim (where, set) •foreach 2.IF 具体用法 <select id=" ...
- hdu 4255 A Famous Grid
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=4255 A Famous Grid Description Mr. B has recently dis ...
- Powershell下设置环境变量
NODE中经常会用到process.env.NODE_ENV来判断当前环境,因为使用的系统是windows,在sublime text下安装terminal插件后,默认启动的时Powershell, ...
- 为什么Linux的fdisk分区时第一块磁盘分区的First Sector是2048?
这个问题曾经困扰我很久,在了解了MBR之后,我曾认为第一块分区之前为一个block.但是用fdisk查看是2048,一直不了解其中的缘由,今天查了一下资料,大概了解了,其中的细节留着慢慢去了解. 最直 ...
- Log4j2 配置笔记(Eclipse+maven+SpringMVC)
Log4j2相关介绍可以百度看下,这里只注重配置Log4j2 能够马上跑起来: 1.pom.xml文件中添加Log4j2的相关Maven配置信息 <!-- log4j2 --> <d ...