6 8 5 3 5 2 6 4 5 6 0 0 8 1 7 3 6 2 8 9 7 5 7 4 7 8 7 6 0 0 3 8 6 8 6 4 5 3 5 6 5 2 0 0 -1 -1
Yes Yes No
HDU 2006-4 Programming Contest
原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=1272
题意:还是并查集,这题就判断一下是否是一个连通分块,是否形成环。但是我的输入有问题,一直错。。。
比较了几份代码,发现都差不多,但是运行时间差别很大,应该是cin和scanf的差别吧!
#include <iostream> #include <cstring> using namespace std; const int maxn=100000+5; int fa[maxn]; bool vis[maxn]; bool flag; void init() { for (int i=1; i<maxn ; i++ ) { vis[i]=false; fa[i]=i; } } int Find(int x) { if(x==fa[x]) return x; return fa[x]=Find(fa[x]); } void Bind(int x,int y) { int fx=Find(x); int fy=Find(y); if(fx > fy) fa[fx] = fy; else fa[fy] = fx; } int main() { int a,b; while(cin>>a>>b,a!=-1&&b!=-1) { init(); flag=false; while(a||b) { if(Find(a)==Find(b)) flag=true; Bind(a,b); vis[a]=vis[b]=true; cin>>a>>b; } if(flag) cout<<"No"<<endl; else { int sum=0; for(int i=1;i<maxn;i++) { if(vis[i]&&fa[i]==i) sum++; } //cout<<"sum="<<sum<<endl; if(sum>1) cout<<"No"<<endl; else cout<<"Yes"<<endl; } } return 0; }
//qscqesze #include <cstdio> #include <cmath> #include <cstring> #include <ctime> #include <iostream> #include <algorithm> #include <set> #include <vector> #include <sstream> #include <queue> #include <typeinfo> #include <fstream> #include <map> #include <stack> typedef long long ll; using namespace std; //freopen("D.in","r",stdin); //freopen("D.out","w",stdout); #define sspeed ios_base::sync_with_stdio(0);cin.tie(0) #define test freopen("test.txt","r",stdin) #define maxn 200001 #define mod 10007 #define eps 1e-9 int Num; char CH[20]; const int inf=0x3f3f3f3f; const ll infll = 0x3f3f3f3f3f3f3f3fLL; inline ll read() { ll x=0,f=1;char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();} return x*f; } inline void P(int x) { Num=0;if(!x){putchar('0');puts("");return;} while(x>0)CH[++Num]=x%10,x/=10; while(Num)putchar(CH[Num--]+48); puts(""); } //************************************************************************************** int fa[maxn]; bool mark[maxn]; int fi(int x) { int t=x; while(fa[t]!=t) t=fa[t]; return t; } bool merge(int x,int y) { int fx=fi(x),fy=fi(y); if(fx!=fy) { fa[fx]=fy; return true; } return false; } int main() { //test; int a,b,i,flag,cnt; while(scanf("%d%d",&a,&b)&&(a!=-1 && b!=-1)) { flag=1;cnt=0; if(a==0 && b==0) { printf("Yes\n"); continue; } for(i=0;i<100010;i++) { fa[i]=i;mark[i]=0; } while(a||b) { mark[a]=1;mark[b]=1; if(merge(a,b)==false) flag=0; scanf("%d%d",&a,&b); } if(flag==0) printf("No\n"); else { for(i=0;i<100010;i++) if(mark[i] && fa[i]==i) cnt++; if(cnt==1) printf("Yes\n"); else printf("No\n"); } } return 0; }
//flag[i]数组标记i是否出现,FLAG标记是否有环,sum记录集合的个数 #include<stdio.h> const int N = 100005; int flag[N], father[N]; void Init() { for(int i = 0; i <= 100000; i++) flag[i] = 0, father[i] = i; } int Find(int x) { if(x != father[x]) father[x] = Find(father[x]); return father[x]; } void Merge(int a, int b) { int p = Find(a); int q = Find(b); father[p] = q; } int main() { int a, b; while(~scanf("%d%d",&a,&b)) { if(a == -1 && b == -1) break; Init(); int FLAG = 0; while(1) { if(a == 0 && b == 0) break; if(Find(a) == Find(b)) FLAG = 1; Merge(a,b); flag[a] = 1, flag[b] = 1; scanf("%d%d",&a,&b); } if(FLAG == 1) printf("No\n"); else { int sum = 0; for(int i = 0; i <= 100000; i++) if(flag[i] && father[i] == i) sum++; //printf("%d\n",sum); if(sum > 1) printf("No\n"); else printf("Yes\n"); } } return 0; }