Party
Time Limit: 2000/1000 MS (Java/others) memory limit: 32768/32768 K (Java/Others)
Total submission (s): 4331 accepted submission (s): 1415
Problem description: N couples are invited to a party. Due to venue issues, only one of them can attend. Among the 2N individuals, some people have great conflicts (of course, there is no conflict between husband and wife), and the two who have conflicts will not appear at the same time. Is there a possibility that N people will attend at the same time?
Inputn: N couples are invited (n <= 1000)
M: represents a conflict between m pairs (M <(n-1) * (N-1 ))
In the next m row, there are four numbers in each row, namely A1, A2, C1, and C2.
A1 and A2 indicate the number of the husband and wife respectively.
C1, C2 indicates the wife or husband, 0 indicates the wife, and 1 indicates the husband.
Husband and wife numbers from 0 to n-1
If output exists, the output is yes.
Otherwise, no
Sample input2 10 1 1 1
After reading two set, sample outputyes can't wait to find a question to do it. This is the question of the first two set. The question has been clearly stated above, and then the practice is to set the I-th husband and wife, 2 * I represents the wife, 2 * I + 1 represents the husband. Then follow the M link edge given. I directly connected the edge. The U-> V edge indicates that these two people have revenge ~ Therefore, in the DFS process, if u is set up, V ^ 1 must be set up. Many people on the internet use the Tarjan contraction point. If a couple is in the same strongly connected component, no. It should be that the method of the diagram is different.
#include <iostream>#include <cstdio>#include <cstring>#include <stack>using namespace std;const int N = 2010;const int M = 2000010;int n , m ;int st[N] , top ;bool mark[N];int eh[N] , et[M] , nxt[M] , tot ;void init(){ tot = 0 ; memset( eh , -1 , sizeof eh ); memset( mark , false , sizeof mark );}void addedge( int u , int v ){ et[tot] = v , nxt[tot] = eh[u] , eh[u] = tot ++ ; et[tot] = u , nxt[tot] = eh[v] , eh[v] = tot ++ ;}bool dfs( int u ){ if( mark[u] ) return true; if( mark[u^1] ) return false; mark[u] = true ; st[top++] = u ; for( int i = eh[u] ; ~i ; i = nxt[i] ){ int v = et[i]; if( !dfs(v^1) ) return false; } return true;}bool solve(){ for(int i = 0 ; i < 2 * n ; i += 2 ){ if( !mark[i] && !mark[i+1] ){ top = 0 ; if( !dfs(i) ){ while( top > 0 ) mark[ st[--top] ] = false; if( !dfs(i+1) ) return false ; } } } return true;}int main(){ #ifdef LOCAL freopen("in.txt","r",stdin); #endif // LOCAL ios::sync_with_stdio(0); int id1 , id2 , x1 , x2 ; while( cin >> n >> m ){ init(); for( int i = 0 ; i < m ; ++i ){ cin >> id1 >> id2 >> x1 >> x2 ; addedge( 2*id1 + x1 , 2*id2 + x2 ); } if( solve() ) cout << "YES" << endl; else cout << "NO" << endl; }}
HDU 3062 party