Question link:
Http://acm.hdu.edu.cn/showproblem.php? PID = 1, 1824
[Question]
Problem description when I was a child, homesickness was a small stamp. I was here, and my mother was there.
-- Yu Guangzhong
Training is hard, the road is bumpy, and the rest is necessary. After a period of training, lcy decided to let everyone go home to relax, but the training had to proceed as usual. lcy came up with the following rule: Each team (three teams) either the team leader or the other two members stay at the same time. For each team member, if Team A stays at the same time, Team B must go home and have a rest, or Team B stays at the same time. As the number of training teams this year has exceeded the highest record of the same period in previous years, the management is quite difficult, and lcy does not know whether his decision is feasible, so this problem is handed over to you ~, Free ** one day of drifting.
The first line of input has two integers: T and M. 1 <= T <= 1000 indicates the number of teams, and 1 <= m <= 5000 indicates the logarithm.
Next there are three integers in Line T, indicating the number of a team member. The first team member is the team leader.
Then there are m rows. Each line has two integers, indicating the number of a pair of players.
Each player belongs to only one team. The player ID starts from 0.
Output can output Yes rows. Otherwise, output no ends with EOF.
Sample Input
1 20 1 20 11 22 40 1 23 4 50 30 41 31 4
Sample output
yesno
[Idea] in each team, either the team leader or the other two team members are left behind. This is a contradiction. we can regard the team leader as a point and abstract the two team members into a point, map them to a ing. For a, B, c ,! A-> B ,! B-> ,! C-> A, that is, F [a] = B, F [B] = A, F [c] = A, and then directly use 2-sat to determine. [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];private: 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;}