加深了对有向边意义的理解了。2-SAT

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std; const int MAXN=;
const int MAXM=; int head[MAXN],dfn[MAXN],low[MAXN],st[MAXN],stop,belong[MAXN],tot,index,pat;
bool stack[MAXN];
struct {
int u,v;
int next;
}edge[MAXM];
int n,m;
int s1,s2; void init(){
stop=; tot=; index=; pat=-;
for(int i=;i<*n;i++){
head[i]=-;
dfn[i]=low[i]=;
belong[i]=-;
stack[i]=false;
}
} void addedge(int u,int v){
edge[tot].u=u;
edge[tot].v=v;
edge[tot].next=head[u];
head[u]=tot++;
} void tarjan(int u){
int v;
dfn[u]=low[u]=++index;
st[stop++]=u; stack[u]=true;
for(int e=head[u];e!=-;e=edge[e].next){
v=edge[e].v;
if(dfn[v]==){
tarjan(v);
low[u]=min(low[u],low[v]);
}
else if(stack[v]){
low[u]=min(low[u],dfn[v]);
}
}
if(dfn[u]==low[u]){
pat++;
do{
v=st[--stop];
stack[v]=false;
belong[v]=pat;
}while(u!=v);
}
} int main(){
int u,v;
while(scanf("%d%d",&n,&m)!=EOF){
init();
for(int i=;i<=m;i++){
scanf("%d%d",&s1,&s2);
u=abs(s1)-;
v=abs(s2)-;
// cout<<u<<' '<<v<<endl;
if(s1>&&s2>){
addedge(*u,*v+);
addedge(*v,*u+);
}
else if(s1>&&s2<){
addedge(*u,*v);
addedge(*v+,*u+);
}
else if(s1<&&s2>){
addedge(*u+,*v+);
addedge(*v,*u);
}
else{
addedge(*u+,*v);
addedge(*v+,*u);
}
}
for(int i=;i<*n;i++){
if(dfn[i]==)
tarjan(i);
}
bool flag=true;
for(int i=;i<n;i++){
if(belong[i*]==belong[i*+]){
printf("0\n");
flag=false;
break;
}
}
if(flag)
printf("1\n");
}
}

POJ 3905的更多相关文章

  1. POJ 3905 Perfect Election(2-sat)

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

  2. POJ 3905 Perfect Election

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

  3. POJ 3905 Perfect Election (2-Sat)

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

  4. POJ 3905 Perfect Election (2-SAT 判断可行)

    题意:有N个人参加选举,有M个条件,每个条件给出:i和j竞选与否会只要满足二者中的一项即可.问有没有方案使M个条件都满足. 分析:读懂题目即可发现是2-SAT的问题.因为只要每个条件中满足2个中的一个 ...

  5. 【图论】2-sat总结

    2-sat总结 2-sat问题,一般表现的形式为.每一个点有两种方式a,b,要么选a,要么选b.而且点点之间有一些约束关系.比如:u和v至少一个选a.那么这就是一个表达式.把a当成真,b当成假,那就是 ...

  6. 【转载】图论 500题——主要为hdu/poj/zoj

    转自——http://blog.csdn.net/qwe20060514/article/details/8112550 =============================以下是最小生成树+并 ...

  7. POJ 3370. Halloween treats 抽屉原理 / 鸽巢原理

    Halloween treats Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 7644   Accepted: 2798 ...

  8. POJ 2356. Find a multiple 抽屉原理 / 鸽巢原理

    Find a multiple Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7192   Accepted: 3138   ...

  9. POJ 2965. The Pilots Brothers' refrigerator 枚举or爆搜or分治

    The Pilots Brothers' refrigerator Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 22286 ...

随机推荐

  1. kafka参数在线修改

    当kafka集群单个节点出现磁盘满了,需要清理历史topic数据:方法如下 1): 停掉kafka进程,将kafka的server.properties中的log.retention.hours=1/ ...

  2. preg_match_all匹配网络上文件

    <?php$ssa=file_get_contents("http://www.oschina.net/code/snippet_4873_5256");preg_match ...

  3. Django day24 cbv和APIView的源码分析 和 resful的规范

    一:cbv的源码分析 1.CBV和FBV的区别: - Class Base View   CBV(基于类的视图) - Function Base View   FBV(基于函数的视图) 2.as_vi ...

  4. css 继承性和层叠性

    css有两大特性:继承性和层叠性 继承性 面向对象语言都会存在继承的概念,在面向对象语言中,继承的特点:继承了父类的属性和方法.那么我们现在主要研究css,css就是在设置属性的.不会牵扯到方法的层面 ...

  5. C - Arrival of the General

    Problem description A Ministry for Defense sent a general to inspect the Super Secret Military Squad ...

  6. windows 装XP系统

    笔记本型号:HPCQ40-506AX 1.在BIOS中更改启动顺序:将USB设为第一启动项2.插入装有PE系统的USB设备3.开机后一直按F124.到达选择系统界面,目前我的HPCQ40用其他系统进去 ...

  7. html5与css3入门知识点精炼

    <meta name = "keywords" content="…………"/>(网页搜索时要输入的关键字) <meta name = &qu ...

  8. CentOS 安装dotNetCore

    如果要在CentOS上运行.net Core程序,必须安装.net Core Sdk 具体安装 方法,可以参考微软官方站点说明,非常详细: 1)百度搜索 .Net Core 2)先择CentOS版本: ...

  9. JavaOO小结二,及MySQL小结

    流按照传输内容分有几种?各自的父类是什么? 流按照传输内容有 字节流.字符流.对象流.但其本质都是字节流.字符流和对象流是在字节流基础上作了一层封装,以便更好对字符和对象进行操作. 字节流的父类:In ...

  10. BZOJ 4259: 残缺的字符串 FFT_多项式

    Code: #include<bits/stdc++.h> #define maxn 1200000 using namespace std; void setIO(string s) { ...