UVa 11503 - Virtual Friends
来源:互联网 发布:网络电视怎么看芒果tv 编辑:程序博客网 时间:2024/06/05 09:35
题目链接:
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.
Input Specification
The first line of input contains one integer specifying the number of test cases to follow. Each test case begins with a line containing an integer F, the number of friendships formed, which is no more than 100 000. Each of the following F lines contains the names of two people who have just become friends, separated by a space. A name is a string of 1 to 20 letters (uppercase or lowercase).Sample Input
13Fred BarneyBarney BettyBetty Wilma
Output Specification
Whenever a friendship is formed, print a line containing one integer, the number of people in the social network of the two people who have just become friends.Output for Sample Input
234
/* * 并查集 + map * Time: 2.124s(UVa), 625MS(HDU) */#include<iostream>#include<map>#include<string>#include<cstdio>#include<cstring>#define N 100005using 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 200005int 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 (转载请标明)
- UVa 11503 - Virtual Friends
- UVa 11503 - Virtual Friends
- Virtual Friends
- Virtual Friends
- Virtual Friends
- HDU3172--Virtual Friends
- hdu3172 Virtual Friends
- hdu3172 Virtual Friends
- hdu 3172 Virtual Friends
- hdu 3172 Virtual Friends
- HDU3172 Virtual Friends
- hdu 3172:Virtual Friends
- hdu3172 Virtual Friends
- NOJ [1246] Virtual Friends
- hdu 3172 Virtual Friends
- hdu 3172 Virtual Friends
- HDU 3172 Virtual Friends
- HDU3172 Virtual Friends
- 每一个导入的类库都生成各自的R类
- Linux 常用指令
- UVa 11218 - KTV, Rujia Liu的神题(一)
- [C-Library]ctype.h - iscntrl( )
- 秒杀多线程第一篇 多线程笔试面试题汇总
- UVa 11503 - Virtual Friends
- V4L2驱动的移植与应用
- UVa 10274 - Fans and Gems, 神牛Rujia Liu的神题(三)
- UVa 11198 - Dancing Digits,Rujia Liu的神题(二)
- Freemarker的内置函数及用法
- UVa 10125 - Sumsets
- Android系统默认值的设置
- 高考并非一切
- Servlet中获取来访机器的IP地址