路径压缩版findx
int findx(int x) { if(x!=father[x]) x=findx(father[x]); else return x; } int findfast(int x) { if(x!=father[x]) father[x]=findx(father[x]); else return father[x]; }例HDU1181
#define DeBUG #include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <cstdlib> #include <algorithm> #include <vector> #include <stack> #include <queue> #include <string> #include <set> #include <sstream> #include <map> #include <bitset> using namespace std ; #define zero {0} #define INF 2000000000 #define EPS 1e-6 typedef long long LL; const double PI = acos(-1.0); inline int sgn(double x){return fabs(x) < EPS ? 0 :(x < 0 ? -1 : 1);} int father[1000]; int findx(int x) { if(x!=father[x]) x=findx(father[x]); else return x; } int findfast(int x) { if(x!=father[x]) father[x]=findx(father[x]); else return father[x]; } void merge(int a,int b) { int fx=findfast(a); int fy=findfast(b); if(fx!=fy) { father[fy]=fx; } } void init() { for(int i=0;i<1000;i++) father[i]=i; } int main() { #ifdef DeBUGs freopen("C:\\Users\\Sky\\Desktop\\1.in","r",stdin); #endif char s[1000]; int t=0; init(); while(scanf("%s", s)+1) { int len=strlen(s); merge(s[0],s[len-1]); if(s[0]=='0') { if(findx('m')=='b') printf("Yes.\n"); else printf("No.\n"); init(); } } return 0; } View Code
转载于:https://www.cnblogs.com/Skyxj/p/3178674.html