This is a creation in Article, where the information may have evolved or changed.
Topic Links:
http://acm.hdu.edu.cn/showproblem.php?pid=1824
Topic
Problem description As a child, homesickness is a small stamp, I am in this, mother in the head.
--Yu Guangzhong
Training is hard, the road is bumpy, rest is necessary. After a period of training, LCY decided to let everyone go home to relax, but the training is still going on, LCY came up with the following home rules, each team (three people a team) or the captain left or the remaining two players left at the same time; each pair of players, if player a leaves, then team B must go home to rest, or b leave , a go home. As the number of training team this year to break through the highest record of previous years, management difficulty is quite large, LCY also do not know whether their decision is feasible, so this problem will be handed to you, hehe, benefits ~, FREE * * Rafting day.
The first line of input has two integers, T and m,1<=t<=1000 represent the number of troops, and 1<=m<=5000 represents the logarithm.
Next there are t-lines, three integers per line, indicating the number of team members, and the first player is the team leader.
Then there are m lines, two integers per line, indicating the number of a pair of players.
Each team member belongs to only one team. The player number starts at 0.
Output Yes, otherwise output no, end with EOF.
Sample Input
1 20 1 20 11 22 40 1 23 4 50 30 41 31 4
Sample Output
Yesno
"Train of thought" each team, either the captain left, or another two players left, this is a contradiction to, can take the captain as a point, the two players also abstracted into a point, they carry out a mapping, for A,b,c,!a->b,!b->a,!c->a, that F[a ] = B, F[b]=a, F[c]=a, and then directly with the 2-sat judgement can be.
Code
#include <iostream> #include <cstdio> #include <cstring>using namespace std;typedef long long int64; const int MAXN = 3010;const int VN = maxn*2;const int EN = Vn*2;int T, m;int f[maxn];struct edge{int V, next;}; struct Graph{public:void init () {size = 0; Memset (Head,-1, sizeof (head)); } void Addedge (int u, int v) {e[size].v = v; E[size].next = Head[u]; Head[u] = size++; }public:int HEAD[VN]; Edge E[en];p rivate:int size; }g;class tow_sat{public:bool Check (const graph&g, const int n) {SCC (g, N); for (int i=0; i<n; ++i) if (belong[3*i] = = Belong[f[3*i] | | Belong[3*i+1] = = belong[f[3*i+1]]) return false; return true; }private:int top, bcnt, IDX; int DFN[VN]; int LOW[VN]; int BELONG[VN]; int STA[VN]; BOOL INSTACK[VN]; void Targan (const graph&g, const int u) {int V; Dfn[u] = low[u] = ++idx; Sta[top++] = u; Instack[u] = true; for (int e=g.head[u]; e!=-1; e=g.e[e].next) {v = g.e[e].v; if (Dfn[v] < 0) {Targan (g, v); Low[u] = min (Low[u], low[v]); }else if (Instack[v]) {Low[u] = min (Low[u], dfn[v]); }} if (dfn[u] = = Low[u]) {++bcnt; do{v = sta[--top]; INSTACK[V] = false; BELONG[V] = bcnt; }while (U! = v); }} void SCC (const graph&g, int n) {top = bcnt = idx = 0; memset (DFN,-1, sizeof (DFN)); memset (instack, 0, sizeof (instack)); for (int i=0; i<3*n; ++i) if (Dfn[i] < 0) Targan (g, I); }}sat;int Main () {int a,b,c; while (~SCANF ("%d%d", &t, &m)) {g.init (); for (int i=0; i<t; ++i) {scanf ("%d%d%d", &a,&b,&c); G.addedge (b, c); G.addedge (c, b); F[a] = b; F[B] = f[c] = A; } bool flag = TRUE; for (int i=0; i<m; ++i) {scanf ("%d%d", &a,&b); G.addedge (A, f[b]); G.addedge (b, f[a]); } if (Flag && sat.check (g, T)) puts ("yes"); Else puts ("no"); } return 0;}