题目链接:HDU - 3062

有n对夫妻被邀请参加一个聚会,因为场地的问题,每对夫妻中只有1人可以列席。在2n 个人中,某些人之间有着很大的矛盾(当然夫妻之间是没有矛盾的),有矛盾的2个人是不会同时出现在聚会上的。有没有可能会有n 个人同时列席?
Input
n: 表示有n对夫妻被邀请 (n<= 1000)
m: 表示有m 对矛盾关系 ( m <
(n - 1) * (n -1))
在接下来的m行中,每行会有4个数字,分别是 A1,A2,C1,C2
A1,A2分别表示是夫妻的编号

C1,C2 表示是妻子还是丈夫 ,0表示妻子 ,1是丈夫
夫妻编号从 0 到 n -1
Output
如果存在一种情况 则输出YES
否则输出 NO 
题目描述:中文题目,如上所述。
算法分析:2-SAT的入门题,如果u和v有矛盾,选u的同时必须选v^1,选v的同时只能选择u^1,然后缩点判断即可。
 #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的更多相关文章

  1. HDU 3062 && HDU 1824 && POJ 3678 && BZOJ 1997 2-SAT

    一条边<u,v>表示u选那么v一定被选. #include <iostream> #include <cstring> #include <cstdio> ...

  2. hdu 3062 2-sat入门题

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3062 #include <cstdio> #include <cmath> # ...

  3. hdu 3062+1824(2-sat入门)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3062 思路:根据矛盾关系连边(如果a与b矛盾,则连边a'->b,b'->a),然后强连通缩 ...

  4. HDU 3062:Party(2-SAT入门)

    http://acm.hdu.edu.cn/showproblem.php?pid=3062 题意:中文. 思路:裸的2-SAT.判断二元组的两个人是否在同一个强连通分量. 学习地址:http://w ...

  5. HDU 3062 Party

    Party Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submi ...

  6. hdu 3062 2-SAT问题

    思路:裸的2-SAT. #include<map> #include<set> #include<cmath> #include<queue> #inc ...

  7. hdu 3062

    2-SAT的入门题: 网上说这个算法最好的入门教材是:伍昱的<由对称性解2-SAT问题>的ppt和赵爽的论文<2-SAT 解法浅析>: 看了一下伍昱的ppt,很好理解! 而这道 ...

  8. 图论(2-sat):HDU 3062 Party

    Party Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submi ...

  9. hdu 3062 2-Sat入门

    开始学习2-Sat,前面看了对称性解决2-sat的ppt,很有帮助. 题意:n对夫妻,夫妻需要出席一人,给出不相容的关系,求每对是否能完成出席方案. 思路:通过关系建图,Tarjan缩点,然后进行判断 ...

随机推荐

  1. SpringMVC基本概念

    DispatcherServlet:MVC的前端控制器,浏览器用户的请求经过DispatcherServlet的分发,到达合适的controller,生产业务数据所需要的model,model通过Di ...

  2. 【 Sqrt(x) 】cpp

    题目: Implement int sqrt(int x). Compute and return the square root of x. 代码: class Solution { public: ...

  3. Python+Selenium中级篇之-二次封装Selenium中几个方法

    本文来介绍,如何把常用的几个webdriver的方法封装到自己写的一个类中去,这个封装过程叫二次封装Selenium方法.我们把打开站点,浏览器前进和后退,关闭和退出浏览器这这个方法封装到一个新写的类 ...

  4. Mac OS 系统占用储存空间太大怎么办?

    存储空间 121 GB,系统就占用 106G,然后就是不断的弹窗提醒你! 解决方法: 终端先执行一下 du -sh * 查看具体是哪里使用了存储空间,一般都是 library 占用比较多的空间, 把可 ...

  5. (转载)CentOS 6.5使用aliyun镜像来源

    (原地址:http://www.linuxidc.com/Linux/2014-09/106675.htm) 当我们把CentOS 6.5安装好以后,可以使用这个脚本来使用国内的阿里云镜像源 #!/b ...

  6. [转]Linux 技巧:让进程在后台可靠运行的几种方法

    转自: https://www.ibm.com/developerworks/cn/linux/l-cn-nohup/index.html 我们经常会碰到这样的问题,用 telnet/ssh 登录了远 ...

  7. Sql数据表中的关系

  8. java 数据库连接 驱动相关参数

    mysql: driverClass=com.mysql.jdbc.Driver connectionURL=jdbc:mysql://localhost:3306/shiro userId=root ...

  9. JDBC 学习笔记(十二)—— DataSource

    在 JDBC 的实现过程中,最消耗资源的从来不是执行 SQL 之类的过程,而是获取-释放 数据库连接 Connection 的过程. 之前通过 DriverManager 获得的数据库连接对象,每一个 ...

  10. GDOI2018 爆零记,Challenge Impossibility

    蒟蒻的GDOI又双叒叕考挂啦...... Day 0 && Day -1 学校月考,貌似考的还不错? 然而考完试再坐船去中山实在是慢啊......晚上10点才到酒店 wifi差评... ...