hdu3062:http://acm.hdu.edu.cn/showproblem.php?pid=3062

题意:中文题。

题解:很明显的2-sat。然后要深刻理解命题和逆否命题。如这一题,c1,c2,表示矛盾。则可以推出如果选c1,则要选~c2,逆否就是不选~c2就要选~c1,就是选c2和~c1,所以既可以加边了,然后就是2-sat。

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
using namespace std;
const int N=;
const int M=;
struct Node{
int u;
int v;
}e[N*];
struct Edge{
int to,next;
} edge[M]; int n,m,cnt,dep,top,atype;
int dfn[N],low[N],vis[N],head[N],st[N],belong[N],in[N],out[N],sum[N];
//sum[i]记录第i个连通图的点的个数,in[i],out[i],表示缩点之后点的入度和初度。
void init(){
cnt=dep=top=atype=;
memset(head,-,sizeof(head));
memset(dfn,,sizeof(dfn));
memset(low,,sizeof(low));
memset(vis,,sizeof(vis));
memset(belong,,sizeof(belong));
memset(in,,sizeof(in));
memset(out,,sizeof(out));
memset(sum,,sizeof(sum));
}
void addedge(int u,int v){
edge[cnt].to=v;
edge[cnt].next=head[u];
head[u]=cnt++;
} void Tarjan(int u){
dfn[u]=low[u]=++dep;
st[top++]=u;
vis[u]=;
for(int i=head[u]; i!=-; i=edge[i].next){
int v=edge[i].to;
if(!dfn[v]){
Tarjan(v);
low[u]=min(low[u],low[v]);
}
else if(vis[v]){
low[u]=min(low[u],dfn[v]);
}
}
int j;
if(dfn[u]==low[u]){
atype++;
do{
j=st[--top];
belong[j]=atype;
sum[atype]++; //记录每个连通分量中点的个数
vis[j]=;
}
while(u!=j);
}
}
int main(){
while(~scanf("%d%d",&n,&m)){
init();
for(int i=;i<=m;i++){
int a1,a2,c1,c2;a1++,a2++;
scanf("%d%d%d%d",&a1,&a2,&c1,&c2);
a1++;a2++;
if(c1==&&c2==){
addedge(a1,a2+n);
addedge(a2,a1+n);
}
if(c1==&&c2==){
addedge(a1,a2);
addedge(a2+n,a1+n);
}
if(c1==&&c2==){
addedge(a1+n,a2+n);
addedge(a2,a1);
}
if(c1==&&c2==){
addedge(a1+n,a2);
addedge(a2+n,a1);
}
}
for(int i=;i<=n*;i++)
if(!dfn[i])Tarjan(i);
bool flag=false;
for(int i=;i<=n;i++){
if(belong[i]==belong[i*]){
flag=true;
}
}
if(!flag){
printf("YES\n");
}
else
printf("NO\n");
}
}

随机推荐

  1. Mysql : L闪存卡linux中的内核参数设置

    将 Nytro WarpDrive 加速卡配置为文件系统 本节说明的操作使您可调整 Nytro WarpDrive 加速卡,增强使用 Oracle Linux with Unbreakable Ent ...

  2. JDBC学生管理系统--处理分页显示

    分页的思想: 假设一共有104条数据,每页显示10条数据: select * from student limit 0,10; 页数是index,第index页,对应的sql语句是: select * ...

  3. linux sed使用

    原文引用:http://www.cnblogs.com/ggjucheng/archive/2013/01/13/2856901.html [root@www ~]# sed [-nefr] [动作] ...

  4. 使用gradle构建java项目

    gradle是什么东东 gradle是继ant,maven之后另外一种的面向java的自动化项目构建工具,他是基于groovy语言的.相对于ant,maven,gradle显得更加简单. 安装grad ...

  5. Spring声明式事务(xml配置事务方式)

    Spring声明式事务(xml配置事务方式) >>>>>>>>>>>>>>>>>>>& ...

  6. CSS排版页面

    创建CSS文件如下: @charset "utf-8"; /* CSS Document */ *{ margin:0px; padding:0px; border:0px; } ...

  7. not exists 跟not in 纪念一下

  8. C#中volatile的用法

    恐怕比较一下volatile和synchronized的不同是最容易解释清楚的.volatile是变量修饰符,而synchronized则作用于一段代码或方法:看如下三句get代码: int i1;  ...

  9. Java Map集合按照key和value排序之法

    一.理论基点 Map是键值对的集合接口,它的实现类主要包括:HashMap,TreeMap,Hashtable以及LinkedHashMap等. TreeMap:基于红黑树(Red-Black-Tre ...

  10. 设计模式之 Singleton 单例模式

    先上两段代码,区别仅在于是否涉及线程安全. 首先是不涉及多线程的单例: public class Singleton { private final static Singleton INSTANCE ...