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
思路:每对夫妻代表图中一个结点,只有 1、0 两种选择,对于有矛盾的夫妻对,使其不列席,让无矛盾夫妻对的列席即可
对于 m 矛盾关系,设 a、b 两对夫妇存在矛盾:
- 若第 a 对的妻子与第 b 对的妻子有矛盾(a b 0 0)
则 a 的妻子去了 b 的丈夫必须去,b 的妻子去了 a 的丈夫必须去:、,添边:,
- 若第 a 对的妻子与第 b 对的丈夫有矛盾(a b 0 1)
则 a 的妻子去了 b 的妻子必须去,b 的丈夫去了 a 的丈夫必须去:、,添边:,
- 若第 a 对的丈夫与第 b 对的妻子有矛盾(a b 1 0)
则 a 的丈夫去了 b 的丈夫必须去,b 的妻子去了 a 的妻子必须去:、,添边:,
- 若第 a 对的丈夫与第 b 对的丈夫有矛盾(a b 1 1)
则 a 的丈夫去了 b 的妻子必须去,b 的丈夫去了 a 的妻子必须去:、,添边:,
Source Program
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include