标签:
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 30274 Accepted Submission(s):
9394
#include<cstdio> #include<set> #include<iostream> #include<cstring> #include<algorithm> using namespace std; int p[100005]; int find(int x) { return p[x]==x?x:p[x]=find(p[x]); } void solve() { int u,v; while(1){ set<int>s; int cnt=0,leap=0; for(int i=0;i<=100000;++i) p[i]=i; while(scanf("%d%d",&u,&v)) { if(!u&&!v) break; if(u==-1&&v==-1) return; cnt++; if(u==v) continue; s.insert(u);s.insert(v); int x=find(u);int y=find(v); if(x==y) leap=1; else p[y]=x; } int cnt2=0; if(!cnt) {cout<<"Yes"<<endl;continue;} for(int i=1;i<=100000;++i) if(p[i]!=i) cnt2++; if(leap) cout<<"No"<<endl; else if(s.size()-cnt2>1) cout<<"No"<<endl; else cout<<"Yes"<<endl; } } int main() { std::ios::sync_with_stdio(0); solve(); }
#include<cstdio> #include<vector> #include<queue> #include<set> #include<utility> #include<iostream> #include<cstring> #include<algorithm> #define MAXN 100000 using namespace std; vector<int>G[MAXN]; set<int>s; int vis[100000]; int g; int ecnt; void in_sert(int u,int v) { s.insert(u); s.insert(v); G[u].push_back(v); G[v].push_back(u); } int readg() { for(int i = 0 ;i < MAXN; i++) G[i].clear(); s.clear(); int u,v; cin>>u>>v; if(u==-1&&v==-1) return -1; if(!u&&!v) {cout<<"Yes"<<endl;return 0;} g=u; in_sert(u,v); ecnt=1; while(cin>>u>>v) { if(!u&&!v) break; ecnt++; in_sert(u,v); } return 1; } void bfs(int st) { memset(vis,0,sizeof(vis)); int now; queue<int>que; vis[st]=1; que.push(st); while(!que.empty()) { now=que.front(); que.pop(); for(int i=0; i<G[now].size(); ++i) { if(!vis[G[now][i]]) { que.push(G[now][i]); vis[G[now][i]]=1; } } } } int main() { //freopen("case.txt","r",stdin); std::ios::sync_with_stdio(0); while(1){ int t=readg(); if(t==-1) break; if(!t) continue; bfs(G[g][0]); int cnt=0; for(int i=0; i<100000; ++i) if(!vis[i]) cnt++; if(MAXN-cnt==s.size()&&ecnt==s.size()-1) cout<<"Yes"<<endl; else cout<<"No"<<endl; } }
标签:
原文地址:http://www.cnblogs.com/orchidzjl/p/4393891.html