题目链接:
UVa : http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=2498
HDU: http://acm.hdu.edu.cn/showproblem.php?pid=3172
类型: 并查集, 哈希
原题:
These days, you can do all sorts of things online. For example, you can use various websites to make virtual friends. For some people, growing their social network (their friends, their friends' friends, their friends' friends' friends, and so on), has become an addictive hobby. Just as some people collect stamps, other people collect virtual friends.
Your task is to observe the interactions on such a website and keep track of the size of each person's network.
Assume that every friendship is mutual. If Fred is Barney's friend, then Barney is also Fred's friend.
1 3 Fred Barney Barney Betty Betty Wilma
2 3 4
/* * 并查集 + map * Time: 2.124s(UVa), 625MS(HDU) */ #include<iostream> #include<map> #include<string> #include<cstdio> #include<cstring> #define N 100005 using namespace std; map<string,int>mp; int father[N], num[N]; void init(){ for(int i=0; i<N; ++i) father[i] = i; } int find(int x){ int i, j = x; while(j!=father[j]) j = father[j]; while(x!=j){ i =father[x]; father[x] = j; x = i; } return j; } void Union(int x, int y){ int a=find(x); int b=find(y); if(a!=b){ father[a] = b; num[b] += num[a]; } } int main(){ #ifdef LOCAL freopen("input.txt","r",stdin); #endif int T, n, K; string person1, person2; while(scanf("%d", &T)!=EOF){ while(T--){ scanf("%d",&n); int cnt=1; init(); mp.clear(); while(n--){ cin >> person1 >> person2; int x1, x2; if(!(x1=mp[person1])){ x1 = mp[person1] = cnt++; num[cnt-1]=1; } if(!(x2=mp[person2])){ x2 = mp[person2] = cnt++; num[cnt-1]=1; } Union(x1, x2); int x = find(x1); printf("%d\n",num[x]); } } } return 0; }
/* * UVa 11503 - Virtual Friends * 并查集 + 哈希 * Time: 0.240 s(UVa), 156MS(HDU) */ #include<cstdio> #include<cstring> #define N 200005 int n, f[N], rank[N]; char name[N][25]; const int MaxHashSize = 1000003; int head[MaxHashSize], next[N]; inline void init_lookup_table(){ memset(head,0,sizeof(head)); } inline int hash(char *str){ int seed=131, v=0; while(*str) v = v*seed+(*str++); return (v & 0x7FFFFFFF)%MaxHashSize; } int try_to_insert(int s){ int h = hash(name[s]); int u = head[h]; while(u){ if(strcmp(name[u],name[s])==0) return u; u = next[u]; } next[s] = head[h]; head[h] = s; return s; } void init(){ for(int i=0; i<N; ++i) f[i]=i, rank[i]=1; } int find(int x){ int i, j=x; while(j!=f[j]) j=f[j]; while(x!=j){i=f[x]; f[x]=j; x=i;} return j; } int Union(int x,int y){ int a=find(x), b=find(y); if(a==b) return rank[a]; rank[a] += rank[b]; f[b] = a; return rank[a]; } int main(){ int T; char name1[25], name2[25]; while(~scanf("%d",&T)){ while(T--){ init(); init_lookup_table(); scanf("%d",&n); int pos=1; if(n==0){ puts("0"); continue; } for(int i=0; i<n; ++i){ int a, b; scanf("%s",name[pos]); if((a=try_to_insert(pos))==pos){ ++pos; } scanf("%s",name[pos]); if((b=try_to_insert(pos))==pos){ ++pos; } printf("%d\n", Union(a,b)); } } } return 0; }
—— 生命的意义,在于赋予它意义。
原创 http://blog.csdn.net/shuangde800 , By D_Double (转载请标明)