Hdu 3062 Party
2016-04-01 20:28
232 查看
Party
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Problem Description
有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
Sample Input
2
1
0 1 1 1
Sample Output
YES
Source
2009 Multi-University Training Contest 16 - Host
by NIT
思路:假设有夫妻<A,A’>,<B,B’>,如果A和B之间有矛盾,则建边<A,B’>,<B,A’>
#include<bits/stdc++.h> using namespace std; const int maxn=2010; bool Instack[maxn]; int Stack[maxn],Low[maxn],DFN[maxn],Belong[maxn]; int Index,top,scc; int head[maxn],tot; struct Edge{ int to,next; }e[maxn*maxn]; void init(){ tot=0; memset(head,-1,sizeof(head)); } void addedge(int from,int to){ e[tot].to=to; e[tot].next=head[from]; head[from]=tot++; } void Tarjan(int u){ Low[u]=DFN[u]=++Index; Stack[top++]=u; Instack[u]=true; for(int i=head[u];i!=-1;i=e[i].next){ int v=e[i].to; if(!DFN[v]){ Tarjan(v); Low[u]=min(Low[v],Low[u]); } else if(Instack[v]) Low[u]=min(DFN[v],Low[u]); } int v; if(Low[u]==DFN[u]){ ++scc; do{ v=Stack[--top]; Instack[v]=false; Belong[v]=scc; }while(v!=u); } } bool solveable(int n){ memset(DFN,0,sizeof(DFN)); memset(Low,0,sizeof(Low)); memset(Instack,false,sizeof(Instack)); Index=scc=top=0; for(int i=0;i<n;i++) if(!DFN[i]) Tarjan(i); for(int i=0;i<n;i+=2) if(Belong[i]==Belong[i^1]) return false; return true; } int main(){ int n,m; while(scanf("%d%d",&n,&m)!=EOF){ init(); int u,v,x,y; for(int i=1;i<=m;i++){ scanf("%d%d%d%d",&u,&v,&x,&y); addedge(u*2+x,(v*2+y)^1); addedge(v*2+y,(u*2+x)^1); } if(solveable(2*n)) printf("YES\n"); else printf("NO\n"); } return 0; }
相关文章推荐
- Swift延迟加载的一种用途
- Swift延迟加载的一种用途
- 蓝桥杯_算法提高_排列数(深度搜索、递归)
- 一个简单的时间获取服务器程序
- java之文件
- FatMouse' Trade
- 搜索二维矩阵
- 【BZOJ1458】士兵占领【最大流】
- 深度学习难在哪里?
- 2016-4-01华为校招实习机试
- ListView,GridView,单选默认选中,单选和多选盖布效果
- WEB服务器2--IIS架构(转)
- 关于XShell远程登录显示乱码问题的解决方案
- 网络:网络基础知识
- Use libvirt to conmunicate with qemu
- cocos2dx 3.3 + lua 学习笔记(04)--- 动作回调、函数回调、骨骼动画回调
- C++上机练习
- 113.失恋28天-缝补礼物(多重背包)
- C#-get/set和方法重载
- 专题一 Problem C