POJ 3905 Perfect Election (2-SAT 判断可行)
题意:有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 判断可行)的更多相关文章
- POJ 3905 Perfect Election(2-sat)
POJ 3905 Perfect Election id=3905" target="_blank" style="">题目链接 思路:非常裸的 ...
- POJ 3905 Perfect Election (2-Sat)
Perfect Election Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 438 Accepted: 223 De ...
- POJ 3905 Perfect Election
2-SAT 裸题,搞之 #include<cstdio> #include<cstring> #include<cmath> #include<stack&g ...
- 图论--2-SAT--POJ 3905 Perfect Election
Perfect Election Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 964 Acce ...
- POJ 3398 Perfect Service(树型动态规划,最小支配集)
POJ 3398 Perfect Service(树型动态规划,最小支配集) Description A network is composed of N computers connected by ...
- OpenJudge 2810(1543) 完美立方 / Poj 1543 Perfect Cubes
1.链接地址: http://bailian.openjudge.cn/practice/2810/ http://bailian.openjudge.cn/practice/1543/ http:/ ...
- POJ 1679 The Unique MST 【判断最小生成树是否唯一】
Description Given a connected undirected graph, tell if its minimum spanning tree is unique. Defini ...
- POJ 1679 The Unique MST(判断最小生成树是否唯一)
题目链接: http://poj.org/problem?id=1679 Description Given a connected undirected graph, tell if its min ...
- POJ 3304 Segments 基础线段交判断
LINK 题意:询问是否存在直线,使得所有线段在其上的投影拥有公共点 思路:如果投影拥有公共区域,那么从投影的公共区域作垂线,显然能够与所有线段相交,那么题目转换为询问是否存在直线与所有线段相交.判断 ...
随机推荐
- ChemDraw怎么绘制H-点或H-划
ChemDraw软件是一款全球领先的化学绘图工具,能够绘制各类化学结构图形和化学方程式,在基础化学.有机化学和分析化学等领域得到了广泛的应用.H-点和H-划是日常作图过程中使用频率较高的化学符号,必须 ...
- C++ map修改指定key的value
对于修改C++指定key的value,网上查了很多,都说直接insert就会覆盖原来的值,是否是这样的呢? C++ Code 12345678910111213141516171819202122 ...
- SurvivalShooter学习笔记(九.游戏暂停、结束)
这里先补充一个得分管理器: 玩家得分设置成一个静态变量: public class ScoreManager : MonoBehaviour { public static int score; // ...
- 调用组件的C++代码
#include<stdio.h>#include "LJSummary.h"#include<iostream>int main(void){ print ...
- handlebears使用
Handlebars 文档笔记: http://www.ghostchina.com/handlebars-wen-dang-bi-ji/ Handlebars模板引擎中的each嵌套及源码浅读: h ...
- Hibernate中的映射关系(一对多)
在数据库中表和表之间的关系有几种,(一对一,一对多,多对多)一对一关系:可以选择任意一方插入外键(one-to-one:one-to-one<--->many-to-one[unique= ...
- C语言实现双链表(带头节点)
双链表和单链表性质相似只是在多加了一个前指针 1.定义结构体 typedef struct Node{ int data; struct Node *prior; struct Node *next; ...
- 对于应用需要记录某个方法耗时的场景,必须使用clock_gettime传入CLOCK_MONOTONIC参数,该参数获得的是自系统开机起单调递增的纳秒级别精度时钟,相比gettimeofday精度提高不少,并且不受NTP等外部服务影响,能准确更准确来统计耗时(java中对应的是System.nanoTime),也就是说所有使用gettimeofday来统计耗时(java中是System.curre
对于应用需要记录某个方法耗时的场景,必须使用clock_gettime传入CLOCK_MONOTONIC参数,该参数获得的是自系统开机起单调递增的纳秒级别精度时钟,相比gettimeofday精度提高 ...
- JavaScript自定义函数
js对象转成用&拼接的请求参数(转) var parseParam=function(param, key){ var paramStr=""; if(param inst ...
- 【opencv】caffe 读入空图导致opencv错误
OpenCV Error: Assertion failed (ssize.area() > ) /modules/imgproc/src/imgwarp. 根据错误提示,查看一下opencv源 ...