本文主要是介绍UVa 11503 - Virtual Friends,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
题目链接:
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
1 3 Fred Barney Barney Betty Betty 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
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 LOCALfreopen("input.txt","r",stdin);
#endifint 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的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!