The main topic: There are n kinds of materials, m judges. Each material has two different practices, each of which has two criteria, and the dishes must meet at least one request from each of the judges. Ask if there is such a solution.
Idea: 2-sat Classic map problem. Because each material can only be done in two ways, this constraint usually reminds of 2-sat. Each of the judges must satisfy at least one, which is the condition for the building of the map.
So even a ' B '
B ' A
This means that if a does not meet a certain jury, then you must let B meet the jury.
Then is the Tarjan shrinkage point, to determine whether there is a dish of two practices in an SCC, if there is no solution, there is no solution.
Cdoe:
#include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #define MAX 5010using namespace Std;int cases;int points,cnt;int head[max],_total;int _next[max],aim[max];int Dfn[max],low[max], Total;int stack[max],top;bool in_stack[max];int changed[max],scc;char c1,c2;inline void Initialize (), inline void Add ( int x,int y), void Tarjan (int x), int main () {for (CIN >> cases;cases;--cases) {scanf ("%d%d", &POINTS,&A MP;CNT); Initialize (); for (int x,y,i = 1;i <= cnt; ++i) {GetChar (); scanf ("%c%d%c%d", &c1,&x,&c2,&y); x = (x << 1) + (C1 = = ' h '); y = (y << 1) + (C2 = = ' h '); ADD (X^1,y); ADD (Y^1,X); } for (int i = 2;i <= (points << 1|1); ++i) if (!dfn[i]) Tarjan (i); BOOL flag = TRUE; for (int i = 1;i <= points; ++i) if (changed[i << 1] = = Changed[i << 1|1]) flag = False if (flag) puts ("good"); Elseputs ("bad"); } return 0;} inline void Initialize () {total = _total = top = SCC =0;memset (dfn,0,sizeof (DFN)); Memset (Head,0,sizeof (head)); Memset (In_ Stack,false,sizeof (In_stack));} inline void Add (int x,int y) {_next[++_total] = head[x]; Aim[_total] = y; HEAD[X] = _total;} void Tarjan (int x) {dfn[x] = low[x] = ++total; Stack[++top] = x; In_stack[x] = true; for (int i = Head[x];i;i = _next[i]) {if (!dfn[aim[i]) Tarjan (Aim[i]), low[x] = min (low[x],low[aim[i]]); else if (In_stack[aim[i]]) low[x] = min (low[x],dfn[aim[i]]); } if (low[x] = = Dfn[x]) {scc++; int temp; do {temp = stack[top--]; CHANGED[TEMP] = SCC; In_stack[temp] = false; }while (temp! = x); }}
Bzoj 1823 Jsoi 2010 Feast 2-sat