HDU 3062 Party
Party
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3551 Accepted Submission(s): 1154
m: 表示有m 对矛盾关系 ( m < (n - 1) * (n -1))
在接下来的m行中,每行会有4个数字,分别是 A1,A2,C1,C2
A1,A2分别表示是夫妻的编号
C1,C2 表示是妻子还是丈夫 ,0表示妻子 ,1是丈夫
夫妻编号从 0 到 n -1
否则输出 NO
1
0 1 1 1
模型一:两者(A,B)不能同时取
那么选择了A就只能选择B’,选择了B就只能选择A’
连边A→B’,B→A’
模型二:两者(A,B)不能同时不取
那么选择了A’就只能选择B,选择了B’就只能选择A
连边A’→B,B’→A
模型三:两者(A,B)要么都取,要么都不取
那么选择了A,就只能选择B,选择了B就只能选择A,选择了A’就只能选择B’,选择了B’就只能选择A’
连边A→B,B→A,A’→B’,B’→A’
模型四:两者(A,A’)必取A
连边A’→A
#include <iostream>
#include <stdio.h>
#include <queue>
#include <stdio.h>
#include <string.h>
#include <vector>
#include <queue>
#include <set>
#include <algorithm>
#include <map>
#include <stack>
#include <math.h>
#define Max(a,b) ((a)>(b)?(a):(b))
#define Min(a,b) ((a)<(b)?(a):(b))
#pragma comment(linker, "/STACK:10240000000000,10240000000000")
using namespace std;
typedef long long LL ;
const int Max_N= ;
const int Max_M= ;
int id ;
int vec[Max_N] ,mystack[Max_N] ,top;
int low[Max_N] ,dfn[Max_N] ,idx ,num ;
bool instack[Max_N] ;
int belong[Max_N] ; //belong[i] ,i为哪个联通分量
//int sum[Max_N] ; //缩点后每个联通分量中点的个数
struct Edge{
int v ;
int next ;
};
Edge edge[Max_M] ;
inline void add_edge(int u,int v){
edge[id].v=v ;
edge[id].next=vec[u] ;
vec[u]=id++ ;
}
void tarjan(int u){
low[u]=dfn[u]=idx++ ;
mystack[++top]=u ;
instack[u]= ;
for(int e=vec[u];e!=-;e=edge[e].next){
int v=edge[e].v ;
if(dfn[v]==-){
tarjan(v) ;
low[u]=Min(low[u],low[v]) ;
}
else if(instack[v])
low[u]=Min(low[u],dfn[v]) ;
}
if(low[u]==dfn[u]){
int v ;
num++ ;
do{
v=mystack[top--] ;
instack[v]= ;
belong[v]=num ;
// sum[num]++ ;
}while(v!=u) ;
}
}
void init(){
idx= ;
top=- ;
num= ;
id=;
memset(dfn,-,sizeof(dfn)) ;
memset(vec,-,sizeof(vec)) ;
memset(instack,,sizeof(instack)) ;
// memset(sum,0,sizeof(sum)) ;
}
int N ;
int judge(){
for(int i=;i<=N;i++){
if(belong[i]==belong[i+N])
return ;
}
return ;
}
int main(){
int m ,a1, a2, c1, c2 ,wife_A ,wife_B ,hus_A ,hus_B;
while(scanf("%d%d",&N,&m)!=EOF){
init() ;
while(m--){
scanf("%d%d%d%d",&a1,&a2,&c1,&c2) ;
a1++ ;
a2++ ;
wife_A=a1 ;
hus_A=a1+N ;
wife_B=a2 ;
hus_B=a2+N ;
if(c1==&&c2==){
add_edge(wife_A,hus_B) ;
add_edge(wife_B,hus_A) ;
}
else if(c1==&&c2==){
add_edge(wife_A,wife_A) ;
add_edge(hus_B,hus_A) ;
}
else if(c1==&&c2==){
add_edge(hus_A,hus_B) ;
add_edge(wife_B,wife_A) ;
}
else if(c1==&&c2==){
add_edge(hus_A,wife_B) ;
add_edge(hus_B,wife_A) ;
}
}
for(int i=;i<=*N;i++){
if(dfn[i]==-)
tarjan(i) ;
}
printf("%s\n",judge()==?"YES":"NO") ;
}
return ;
}
HDU 3062 Party的更多相关文章
- HDU 3062 && HDU 1824 && POJ 3678 && BZOJ 1997 2-SAT
一条边<u,v>表示u选那么v一定被选. #include <iostream> #include <cstring> #include <cstdio> ...
- hdu 3062 Party 2-SAT
题目链接:HDU - 3062 有n对夫妻被邀请参加一个聚会,因为场地的问题,每对夫妻中只有1人可以列席.在2n 个人中,某些人之间有着很大的矛盾(当然夫妻之间是没有矛盾的),有矛盾的2个人是不会同时 ...
- 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 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缩点,然后进行判断 ...
随机推荐
- CSS命名规则
头:header 内容:content/container 尾:footer 导航:nav 侧栏:sidebar 栏目:column 页面外围控制整体布局宽度:wrapper 左右中:le ...
- 【shell】通配符
‘’与“” [root@andon ~]# name='$date' [root@andon ~]# echo $name $date [root@andon ~]# name=abc [root@a ...
- 十个 MongoDB 使用要点
转自: 十个 MongoDB 使用要点 从 mongodb 阶段性技术总结 中抽取并整理了对大家有帮助的十个要点: 1.mongodb 表名和字段名统一用小写字母 mongodb 是默认区分 ...
- html body标签的几个属性 禁用鼠标右键,禁用鼠标选中文字等
<body oncontextmenu='return false' ondragstart='return false' onselectstart ='return false' onsel ...
- 冒泡算法C#
冒泡算法C# namespace数组排序 { classProgram { staticvoidMain(string[]args) { inttemp=; ,,,,,,,,}; #region该段与 ...
- [Freescale]Freescale L3.14.52_1.1.0 yocto build
可参照:http://blog.csdn.net/wince_lover/article/details/51456745 1. Refer to <基于i.mx6处理器的Yocto项目及Lin ...
- 105. Construct Binary Tree from Preorder and Inorder Traversal
Given preorder and inorder traversal of a tree, construct the binary tree. ============== 基本功: 利用前序和 ...
- 常用的邮箱服务器(SMTP、POP3)地址、端口
常用的邮箱服务器(SMTP.POP3)地址.端口 gmail(google.com) POP3服务器地址:pop.gmail.com(SSL启用 端口:995) SMTP服务器地址:smtp.gm ...
- android中的数据库操作
如何在android中调用数据库资源 在android中主要有两种方法来实现对数据库的访问,一种是adb shell方式,另一种是通过相关的android 的java类来间接的对数据库来进行操作.其中 ...
- Java执行main方法,异常为:could not find the main class.program will exit
未解决. Java执行方法,异常为:could not find the main class.program will exitmain 原文地址:http://rogerfederer.iteye ...