题意:有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. VC++ GetSafeHwnd用法

    GetSafeHwnd HWND GetSafeHwnd() const; 当我们想得到一个窗口对象(CWnd的派生对象)指针的句柄(HWND)时,最安全的方法是使用GetSafeHwnd()函数. ...

  2. python3----scrapy(笔记)

    import scrapy import sys # import io # sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='gb ...

  3. easyui上传文件

    效果图: 代码: <form id="importFileForm" method="post" enctype="multipart/form ...

  4. Fiddler实现手机抓包——小白入门 - 做一个不动声色的大人

    手机用fiddler抓包 电脑最好是笔记本,这样能和手机保持统一局域网内:其他不多说,直接说步骤了. 一.对PC(笔记本)参数进行配置    1. 配置fiddler允许监听到https(fiddle ...

  5. dreamweaver中Dw设置svn进行版本控制

    需要工具: VisualSVN dwcs5+ 点击查看教程

  6. 170316、spring4:@Cacheable和@CacheEvict实现缓存及集成redis

    注:1.放入cache中,采用@Cacheable;使缓存失效@CacheEvict 2.自定义CacheManager只需要继承org.springframework.cache.support.A ...

  7. 160419、CSS3实现32种基本图形

    CSS3可以实现很多漂亮的图形,我收集了32种图形,在下面列出.直接用CSS3画出这些图形,要比贴图性能更好,体验更加,是一种非常好的网页美观方式. 这32种图形分别为圆形,椭圆形,三角形,倒三角形, ...

  8. WebStorm 常用安装,配置,快捷键

    WebStorm  软件资源   http://pan.baidu.com/s/1c2OmyQW(不建议使用汉化版,都已可能出现很多意想不到的问题) * Web前端集成开发工具 - WebStorm ...

  9. Sublime Text 3如何编译运行c++程序

    扯 去了一趟清北学堂感觉自己玩的特别嗨,算法没学到什么,前端和爬虫的知识到是会了不少. 然后知道了有一个叫做sublime text 3的编辑器,好用不好用不知道,就冲着它好看,就决定以后就用它了. ...

  10. Exchange Pause or stop transport service

    The Microsoft Exchange Transport service is a service available both on the Microsoft Exchange Serve ...