#include <iostream> #include <vector> using namespace std; const int MAX = 100; vector<int> G[MAX]; /** struct edge{ int to; int cost; }; vector<edge> G[MAX]; */ int main() { int V,E; cin>>V>>E; for(int i=0;i<V;i++){ int s,t; cin>>s>>t; G[s].push_back(t); } ... }
#include <iostream> #include <vector> using namespace std; const int MAX = 100; vector<int> G[MAX]; int V; int color[MAX]; bool dfs(int v,int c){ color[v] = c; for(unsigned int i=0;i<G[v].size();i++){ if(color[G[v][i]] == c) return false; if(color[G[v][i]]==0 && !dfs(G[v][i],-c)) return false; } return true; } void slove(){ for(int i=0;i<V;i++){ if(color[i]==0){ if(!dfs(i,1)){ cout<<"No"<<endl; return; } } } cout<<"Yes"<<endl; } int main() { cin>>V; for(int i=0;i<V;i++){ int s,t; cin>>s>>t; G[s].push_back(t); G[t].push_back(s); } slove(); }
原文地址:http://blog.csdn.net/xd_122/article/details/40538845