题目链接:
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 integerF,
the number of friendships formed, which is no more than 100 000. Each of the followingFlines 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
题意:
如果A与B是朋友, C是B的朋友,那么A和B也就是朋友。 这样的话就会形成一个朋友关系网,只有一个人在这个关系网里面,就等于认识这个朋友网里的所有人。 输入一些朋友关系,每输入一个关系,就输出两个人所在朋友网里的总人数
分析与总结:
很明显的并查集题目。这题关键在与把人名转换成数字。
这题很坑爹的一个地方:输入的T也是多组的,要用 while(scanf("%d",&T)!=EOF){ while(T--){ ... } }
1.直接用STL的map来处理。但是速度不佳: 2.124s(UVa), 625MS(HDU)
/*
* 并查集 + 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;
}
2. 用哈希表映射字符串的关系。 速度挺不错的,在UVa和HDU都进入了rank
/*
* 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;
}
3. 用字典树(待补充。。。)
分享到:
相关推荐
判断输入字符串是否为镜像或回文串。 来源于UVaOJ - 401. 水题。
开源项目-codingsince1985-UVa#uva-online-judge-solutions-in-golang.zip,两年来每天都在解决一个uva在线裁判问题,算起来…
uva705 Slash Maze 的代码,在UVaOJ上通过
PDF试题
uva532 Dungeon Master的源代码,并且AC了
Algorithm-UVA-Solutions-in-Python.zip,python 3中各种uva(acm)问题的解决方案。,算法是为计算机程序高效、彻底地完成任务而创建的一组详细的准则。
这是UVA133 TheDoleQueue救济金发放问题,经典的算法问题。初学算法的人要对这种算法非常熟悉并且能熟练运用。
tpcw-nyu-uva-client 客户端
leetcode 2 算法-Java UVa Online Judge(ACM-ICPC Live ...使用:数组、哈希表、链表、二分搜索、动态规划、堆栈、堆、reedy、排序、树 DFS、BFS、图、二分搜索树、递归、记忆、队列、映射等。...Uva-ACM-ICPC