标签:
Description
Input
Output
Sample Input
Sample Output
#pragma comment(linker, "/STACK:1024000000,1024000000") #include<iostream> #include<cstdio> #include<cstring> #include<cstdlib> #include<algorithm> #include<vector> #include<stack> #include<queue> #include<set> #include<map> #include<string> #include<math.h> #include<cctype> #define ll long long #define REP(i,a,b) for(int (i)=(a);(i)<=(b);(i)++) #define REPP(i,a,b,t) for(int (i)=(a);(i)<=(b);(i)+=(t)) #define rep(i,a,b) for(int (i)=(a);(i)>=(b);(i)--) #define repp(i,a,b,t) for(int (i)=(a);(i)>=(b);(i)-=(t)) #define PII pair<int,int> #define fst first #define snd second #define MP make_pair #define PB push_back #define RI(x) scanf("%d",&(x)) #define RII(x,y) scanf("%d%d",&(x),&(y)) #define RIII(x,y,z) scanf("%d%d%d",&(x),&(y),&(z)) #define DRI(x) int (x);scanf("%d",&(x)) #define DRII(x,y) int (x),(y);scanf("%d%d",&(x),&(y)) #define DRIII(x,y,z) int (x),(y),(z);scanf("%d%d%d",&(x),&(y),&(z)) #define RS(x) scanf("%s",x) #define RSS(x,y) scanf("%s%s",x,y) #define DRS(x) char x[maxn];scanf("%s",x) #define DRSS(x,y) char x[maxn],y[maxn];scanf("%s%s",x,y) #define MS0(a) memset((a),0,sizeof((a))) #define MS1(a) memset((a),-1,sizeof((a))) #define MS(a,b) memset((a),(b),sizeof((a))) #define ALL(v) v.begin(),v.end() #define SZ(v) (int)(v).size() using namespace std; const int maxn=1000100; const int INF=(1<<29); const double EPS=0.0000000001; const double Pi=acos(-1.0); int u,v; int fa[maxn]; bool flag; vector<int> V; bool vis[maxn]; void Init() { flag=1; REP(i,1,maxn-1) fa[i]=i; MS0(vis); V.clear(); } /* int find(int x) { return fa[x]==x?x:fa[x]=find(fa[x]); } */ int find(int x) { int p=x,t; while(fa[p]!=p) p=fa[p]; while(x!=p){ t=fa[x]; fa[x]=p; x=t; } return x; } void solve() { if(flag&&SZ(V)){ int x=find(V[0]); REP(i,0,SZ(V)-1){ int y=find(V[i]); if(x!=y){ flag=0;break; } } } puts(flag?"Yes":"No"); } int main() { freopen("in.txt","r",stdin); Init(); while(~RII(u,v)){ if(u==-1&&v==-1) return 0; if(u==0&&v==0){ solve(); Init(); continue; } if(!vis[u]) V.PB(u),vis[u]=1; if(!vis[v]) V.PB(v),vis[v]=1; int x=find(u),y=find(v); if(x==y) flag=0; else fa[x]=y; } return 0; }
标签:
原文地址:http://www.cnblogs.com/--560/p/4687565.html