hdu 3172 Virtual Friends 并查集的应用,输入有坑!!!!
来源:互联网 发布:中国环保网络电视台 编辑:程序博客网 时间:2024/06/05 13:00
Virtual Friends
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 5717 Accepted Submission(s): 1606
Problem Description
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.
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
Input file contains multiple test cases.
The first line of each case indicates the number of test friendship nest.
each friendship nest begins with a line containing an integer F, the number of friendships formed in this frindship nest, 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).
The first line of each case indicates the number of test friendship nest.
each friendship nest begins with a line containing an integer F, the number of friendships formed in this frindship nest, 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).
Output
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.
Sample Input
13Fred BarneyBarney BettyBetty Wilma
Sample Output
234
假设有t组测试数据应该这样写:
int t ;while(~scanf("%d",&t))//特别 需要注意
我的代码:
#include <iostream>#include <cstdio>#include <string>#include <set>#include <algorithm>const int MAX = 100100 ;using namespace std ;string name[MAX] ,line1[MAX],line2[MAX];int f[MAX] ,c[MAX];void init(int n){for(int i = 0 ; i < n + 10 ; ++i){f[i] = i ;c[i] = 1 ;}}int find(int x){int r = x ;while(r!=f[r]){r = f[r] ;}int temp ;while(x != f[x]){temp = f[x] ;f[x] = r ;x = temp ;}return r ;}void join(int x ,int y){int fx = find(x) , fy = find(y) ;if(fx != fy){f[fx] = fy ;c[fy] += c[fx] ;}}bool cmp(const string &a , const string &b){return a<b ;}int Bsearch(string &s , int len){int i = 0 , j = len-1 ;while(i<=j){int mid = (i+j)>>1 ; if(name[mid] > s){j = mid - 1 ;}else if(name[mid]<s){i = mid + 1 ;}else{return mid ;}}return i ;}int main(){int t ;while(~scanf("%d",&t))//特别 需要注意 {while(t--){set<string>s;int n ;scanf("%d",&n);for(int i = 0 ; i < n ; ++i){cin>>line1[i]>>line2[i] ;s.insert(line1[i]);s.insert(line2[i]) ;}set<string>::iterator it = s.begin() ;int len = 0 ;for( ; it != s.end() ; ++it){name[len++] = *it ;}sort(name,name+len,cmp) ;init(len) ;for(int i = 0 ; i < n ; ++i){int x = Bsearch(line1[i],len) ;int y = Bsearch(line2[i],len) ;join(x,y) ;printf("%d\n",c[find(y)]) ;}}}return 0 ;}
与君共勉
0 0
- hdu 3172 Virtual Friends 并查集的应用,输入有坑!!!!
- HDU 3172 Virtual Friends(并查集+map+输入)
- hdu 3172 virtual friends 并查集
- HDU 3172 Virtual Friends (并查集)
- 【并查集】 HDU 3172 Virtual Friends
- hdu 3172 Virtual Friends(并查集)
- hdu 3172 Virtual Friends 并查集
- HDU 并查集 - 3172 Virtual Friends
- HDU 3172 Virtual Friends (并查集)
- 并查集 HDU 3172 Virtual Friends
- HDU 3172 并查集-Virtual Friends
- hdu 3172 Virtual Friends 字典树+并查集
- hdu 3172 Virtual Friends(简单并查集)
- hdu 3172 Virtual Friends (cmp + 并查集)
- HDU-3172 Virtual Friends 并查集+map
- HDU 3172 Virtual Friends(并查集)
- hdu 3172 Virtual Friends (并查集 + 字典树)
- hdu 3172 Virtual Friends(并查集+map)
- PAT 1023. 组个最小数
- POJ 2010 - Moo University - Financial Aid(优先队列)
- BZOJ 3319 黑白树 并查集+线段树
- 再谈用c++实现property,不明白那些委员会是干嘛吃的。
- 8 ② RecognizedGesture 本例识别不了 待解决
- hdu 3172 Virtual Friends 并查集的应用,输入有坑!!!!
- util.Date 和 sql.Date
- php 图片文件上传类
- android开源数据库解决方案——geenDao指南
- 深入理解Java的方法调用
- ios 转场动画
- Ros中2D_Slam说明: turtlebot+kinect改写成turtlebot+laser(hokuyo)
- 【SSH进阶之路】一步步重构容器实现Spring框架——从一个简单的容器开始(八)
- php SOAP客户端访问 JAVA soap服务端示例