标签:des style blog color io os ar java for
Time Limit: 10000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1435 Accepted Submission(s): 573
#include <iostream> #include <cstdio> #include <cstring> #include <stack> #include <map> using namespace std; const int N = 2010; const int M = 10010; int n , m ; int st[N] , top ; bool mark[N]; int eh[N] , et[M] , nxt[M] , tot ; map<int,int>mp; void init() { mp.clear(); 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 x1 , x2 , x3 ; while( cin >> n >> m ){ init(); for( int i = 0 ; i < n ; ++i ){ cin >> x1 >> x2 >> x3 ; mp[x1] = 2 * i ; mp[x2] = 2 * i + 1 ; mp[x3] = 2 * i + 1 ; } for( int i = 0 ; i < m ; ++i ){ cin >> x1 >> x2 ; x1 = mp[x1] ; x2 = mp[x2] ; addedge( x1 , x2 ); } if( solve() ) cout << "yes" << endl; else cout << "no" << endl; } }
标签:des style blog color io os ar java for
原文地址:http://www.cnblogs.com/hlmark/p/4019339.html