题意:有N个人参加选举,有M个条件,每个条件给出:i和j竞选与否会只要满足二者中的一项即可。问有没有方案使M个条件都满足。

分析:读懂题目即可发现是2-SAT的问题。因为只要每个条件中满足2个中的一个即可,因此将人i拆成 点i表示不竞选,和点i+N表示竞选,根据合取式的满足条件建图跑Tarjan。

最后检查每个i与i+N是否在同一强连通分量中。

#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<vector>
#include<stack>
#include<cmath>
using namespace std;
typedef long long LL;
const int maxn =2e3+5;
const int maxm = 2e6+5;
struct Edge{
int v,next;
}edges[maxm];
int head[maxn],tot;
stack<int> S;
int pre[maxn],low[maxn],sccno[maxn],dfn,scc_cnt;
void init()
{
tot = dfn = scc_cnt=0;
memset(pre,0,sizeof(pre));
memset(sccno,0,sizeof(sccno));
memset(head,-1,sizeof(head));
}
void AddEdge(int u,int v) {
edges[tot] = (Edge){v,head[u]};
head[u] = tot++;
}
void Tarjan(int u)
{
int v;
pre[u]=low[u]=++dfn;
S.push(u);
for(int i=head[u];~i;i=edges[i].next){
v= edges[i].v;
if(!pre[v]){
Tarjan(v);
low[u]=min(low[u],low[v]);
}
else if(!sccno[v]){
low[u]=min(low[u],pre[v]);
}
}
if(pre[u]==low[u]){
int x;
++scc_cnt;
for(;;){
x = S.top();S.pop();
sccno[x]=scc_cnt;
if(x==u)break;
}
}
} int N,M; bool check()
{
int all = 2*N;
for(int i=1;i<=all;++i){
if(!pre[i]) Tarjan(i);
}
for(int i=1;i<=N;i++){
if(sccno[i]==sccno[i+N]) return false;
}
return true;
} int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
freopen("out.txt","w",stdout);
#endif
while(scanf("%d %d",&N,&M)==2){
init();
int x,y,x1,y1,tmp; char c1,c2;
for(int i=1;i<=M;++i){
scanf("\n%c%d %c%d",&c1,&x,&c2,&y);
x1 = x+N,y1 = y+N; //x1表示x被选
if(c1=='+' && c2=='+'){
AddEdge(y,x1), AddEdge(x,y1);
}
else if(c1=='+'){
AddEdge(x,y), AddEdge(y1,x1);
}
else if(c2=='+'){
AddEdge(x1,y1), AddEdge(y,x);
}
else{
AddEdge(x1,y), AddEdge(y1,x);
}
}
if(check()) puts("1");
else puts("0");
}
return 0;
}

POJ 3905 Perfect Election (2-SAT 判断可行)的更多相关文章

  1. POJ 3905 Perfect Election(2-sat)

    POJ 3905 Perfect Election id=3905" target="_blank" style="">题目链接 思路:非常裸的 ...

  2. POJ 3905 Perfect Election (2-Sat)

    Perfect Election Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 438   Accepted: 223 De ...

  3. POJ 3905 Perfect Election

    2-SAT 裸题,搞之 #include<cstdio> #include<cstring> #include<cmath> #include<stack&g ...

  4. 图论--2-SAT--POJ 3905 Perfect Election

    Perfect Election Time Limit: 5000MS         Memory Limit: 65536K Total Submissions: 964         Acce ...

  5. POJ 3398 Perfect Service(树型动态规划,最小支配集)

    POJ 3398 Perfect Service(树型动态规划,最小支配集) Description A network is composed of N computers connected by ...

  6. OpenJudge 2810(1543) 完美立方 / Poj 1543 Perfect Cubes

    1.链接地址: http://bailian.openjudge.cn/practice/2810/ http://bailian.openjudge.cn/practice/1543/ http:/ ...

  7. POJ 1679 The Unique MST 【判断最小生成树是否唯一】

    Description Given a connected undirected graph, tell if its minimum spanning tree is unique.  Defini ...

  8. POJ 1679 The Unique MST(判断最小生成树是否唯一)

    题目链接: http://poj.org/problem?id=1679 Description Given a connected undirected graph, tell if its min ...

  9. POJ 3304 Segments 基础线段交判断

    LINK 题意:询问是否存在直线,使得所有线段在其上的投影拥有公共点 思路:如果投影拥有公共区域,那么从投影的公共区域作垂线,显然能够与所有线段相交,那么题目转换为询问是否存在直线与所有线段相交.判断 ...

随机推荐

  1. log4j配置文件

    log4j.rootLogger=INFO,CONSOLElog4j.addivity.org.apache=truelog4j.appender.stdout=org.apache.log4j.Co ...

  2. 数据库unsigned char*类型图片存进

    loadimage1();测试: void Caccess_test_1Dlg::loadimage1()//存入unsigned char*类型的数据图片 { CFileException e; I ...

  3. VS2008设置快捷键Ctrl+W关闭当前打开的文本编辑器窗口

    好多友好的软件关闭多标签页的当前页时都有Ctrl+W的快捷键,如Chrome浏览器,使用起来还是很方便的. 但是作为程序员,使用VS2008时有时会打开好多C++或C#源文件,需要关闭某个源文件时你需 ...

  4. 解决error: Unable to find vcvarsall.bat【python 2.7/vs2010】

    转自:http://blog.csdn.net/secretx/article/details/17472107 去下载安装VS2010(08版貌似也行,不过没必要用旧版,指不定哪个库又无法编译),给 ...

  5. tcp断开时分几步

    连接时是三次握手 断开时是四次握手,因为它是半关闭造成的

  6. Android无线测试之—UiAutomator UiScrollable API介绍六

    向前与向后滚动API 一.向前与向后滚动相关API 返回值 API 描述 boolean scrollBackward(int steps) 自动以步长向后滑动 boolean scrollBackw ...

  7. Java Web项目--使用Servlet生成一个页面

    为了生成一个servlet对应的网页.我们需要新建一个web.xml,其中将会放置servlet的相关信息.web.xml文件放置在WebContent/WEB-INF/目录下.(我们在Eclipe中 ...

  8. 微软发布新版 Skype Linux 客户端

    导读 前两天,微软说要给Linux 用户带来一个令人兴奋的新闻,今天,这个新闻来了.它刚刚为 Linux 发布了一个新的 Skype 客户端.此次发布,微软为 Linux 带来的 Skype 客户端与 ...

  9. ORA-00845 MEMORY_TARGET not supported on this system解决办法

    ORA-00845: MEMORY_TARGET not supported on this system报错解决 Oracle 11g数据库修改pfile参数后启动数据库报错ora-00845 SQ ...

  10. 【BZOJ2525】[Poi2011]Dynamite 二分+树形DP

    [BZOJ2525][Poi2011]Dynamite Description Byteotian Cave的结构是一棵N个节点的树,其中某些点上面已经安置了炸.药,现在需要点燃M个点上的引线引爆所有 ...