hdu 3062 Party 2-SAT
题目链接:HDU - 3062
m: 表示有m 对矛盾关系 ( m <
(n - 1) * (n -1))
在接下来的m行中,每行会有4个数字,分别是 A1,A2,C1,C2
A1,A2分别表示是夫妻的编号
C1,C2 表示是妻子还是丈夫 ,0表示妻子 ,1是丈夫
夫妻编号从 0 到 n -1
否则输出 NO
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<queue>
#include<vector>
#include<stack>
using namespace std;
const int maxn=+;
const int M=+; int n,m;
struct node
{
int v,next;
}edge[M*];
int head[maxn],edgenum;
int dfn[maxn],low[maxn],scc[maxn],ind[maxn],vis[maxn];
stack<int> S;
int color[maxn],f[maxn];
int dfs_clock,scc_cnt;
vector<int> dag[maxn];
void add(int u,int v)
{
edge[edgenum].v=v ;edge[edgenum].next=head[u] ;
head[u]=edgenum++;
}
void init()
{
memset(head,-,sizeof(head));
memset(vis,,sizeof(vis));
memset(dfn,,sizeof(dfn));
memset(ind,,sizeof(ind));
memset(color,,sizeof(color));
edgenum=dfs_clock=scc_cnt=;
}
void tarjan(int u)
{
dfn[u]=low[u]= ++dfs_clock;
vis[u]=;
S.push(u);
for (int i=head[u] ;i!=- ;i=edge[i].next)
{
int v=edge[i].v;
if (!dfn[v])
{
tarjan(v);
low[u]=min(low[u],low[v]);
}
else if (vis[v])
low[u]=min(low[u],dfn[v]);
}
if (low[u]==dfn[u])
{
scc_cnt++;
while (true)
{
int v=S.top() ;S.pop() ;
vis[v]=;
scc[v]=scc_cnt;
if (u==v) break;
}
}
}
void buildDag(int n)
{
for (int u= ;u<*n ;u++)
{
for (int i=head[u] ;i!=- ;i=edge[i].next)
{
int v=edge[i].v;
if (scc[v] != scc[u])
{
dag[scc[v]].push_back(scc[u]);
ind[scc[u]]++;
}
}
}
}
void topsort()
{
queue<int> Q;
for (int i= ;i<=scc_cnt ;i++) if (!ind[i]) Q.push(i);
while (!Q.empty())
{
int u=Q.front() ;Q.pop();
if (!color[u]) color[u]=,color[f[u]]=;
for (int i= ;i<(int)dag[u].size() ;i++)
{
int v=dag[u][i];
ind[v]--;
if (!ind[v]) Q.push(v);
}
}
}
void solve(int n)
{
int flag=;
for (int i= ;i<*n ;i++) if (!dfn[i]) tarjan(i);
for (int i= ;i<n ;i++)
{
if(scc[*i]==scc[(*i)^])
{
printf("NO\n");
flag=;
return ;
}
//else f[scc[i]]=scc[i+1],f[scc[i+1]]=scc[i];
}
if (!flag) printf("YES\n");
// for (int i=0 ;i<=scc_cnt ;i++) dag[i].clear();
// buildDag(n);
// topsort();
return ;
}
int main()
{
while (scanf("%d%d",&n,&m)!=EOF)
{
init();
int a,b,c,d;
for (int i= ;i<m ;i++)
{
scanf("%d%d%d%d",&a,&b,&c,&d);
int u=*a+c,v=*b+d;
add(u,v^) ;add(v,u^);
}
solve(n);
}
return ;
}
hdu 3062 Party 2-SAT的更多相关文章
- HDU 3062 && HDU 1824 && POJ 3678 && BZOJ 1997 2-SAT
一条边<u,v>表示u选那么v一定被选. #include <iostream> #include <cstring> #include <cstdio> ...
- hdu 3062 2-sat入门题
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3062 #include <cstdio> #include <cmath> # ...
- hdu 3062+1824(2-sat入门)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3062 思路:根据矛盾关系连边(如果a与b矛盾,则连边a'->b,b'->a),然后强连通缩 ...
- HDU 3062:Party(2-SAT入门)
http://acm.hdu.edu.cn/showproblem.php?pid=3062 题意:中文. 思路:裸的2-SAT.判断二元组的两个人是否在同一个强连通分量. 学习地址:http://w ...
- HDU 3062 Party
Party Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submi ...
- hdu 3062 2-SAT问题
思路:裸的2-SAT. #include<map> #include<set> #include<cmath> #include<queue> #inc ...
- hdu 3062
2-SAT的入门题: 网上说这个算法最好的入门教材是:伍昱的<由对称性解2-SAT问题>的ppt和赵爽的论文<2-SAT 解法浅析>: 看了一下伍昱的ppt,很好理解! 而这道 ...
- 图论(2-sat):HDU 3062 Party
Party Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submi ...
- hdu 3062 2-Sat入门
开始学习2-Sat,前面看了对称性解决2-sat的ppt,很有帮助. 题意:n对夫妻,夫妻需要出席一人,给出不相容的关系,求每对是否能完成出席方案. 思路:通过关系建图,Tarjan缩点,然后进行判断 ...
随机推荐
- 【ansible】使用ansible安装nginx
一.主机准备 ServerIP:10.10.10.102 ClientIP: 10.10.10.103,10.10.10.104 二.安装ansible yum -y install ansible ...
- 14、响应式布局和BootStrap 全局CSS样式知识点总结-part1
1.什么是响应式布局 响应式布局是Ethan Marcotte在2010年5月份提出的一个概念,这个概念是为解决移动互联网浏览而诞生的. 简而言之,就是一个网站能够兼容多个终端——而不是为每个终端做一 ...
- IOS开发学习笔记006 - 指针
C语言 指针 简单使用如下: int *p;//定义 int a = 10; p = &a;//赋值 int * b = &a;//定义并赋值 定义格式:类型 *指针变量名: 注意事项 ...
- 【SDOI2009】HH的项链 线段树
题目描述 HH 有一串由各种漂亮的贝壳组成的项链.HH 相信不同的贝壳会带来好运,所以每次散步完后,他都会随意取出一段贝壳,思考它们所表达的含义.HH 不断地收集新的贝壳,因此,他的项链变得越来越长. ...
- Halcon11 Linux 下载
Halcon11 Linux下载地址:http://www.211xun.com/download_page_3.html HALCON 11 是一套机器视觉图像处理库,由一千多个算子以及底层的数据管 ...
- [oldboy-django][5python基础][高级特性]Iterator迭代器
# 区分可迭代对象iterable, 迭代器iterator, 生成器generator a. iterable 可直接用for循环的对象,都称为可迭代对象, from collections imp ...
- PAT1038(两个运行超时 未解决
# include<iostream> # include<algorithm> using namespace std; int jishu(int a[],int N,in ...
- poj3748 位运算 bitset
位操作 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9064 Accepted: 3626 Description 假 ...
- HDU 4391 Paint The Wall(分块+延迟标记)
Paint The Wall Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- [AGC004F] Namori
Description 现在给你一张N个点M条边的连通图,我们保证N−1≤M≤N,且无重边和自环. 每一个点都有一种颜色,非黑即白.初始时,所有点都是白色的. "全"想通过执行若干 ...