hdu 5.1.7 virtual friend
来源:互联网 发布:js调用angularjs方法 编辑:程序博客网 时间:2024/06/07 16:05
hdu终于正常了……之前的题,都有些记不清了(什么记性啊喂!)
Virtual Friends
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 54 Accepted Submission(s): 27Problem 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
Input file contains multiple test cases. ……害我wa了一次。
#include <iostream>#include <map>#include <string>using namespace std;int root[100002];int cnt[100002];//how many friend does (the root) haveint num;map<string ,int> p;//map, cast string to intvoid clear(){ int i; for(i=0;i<100002;i++) { root[i]=i; cnt[i]=1; } p.clear(); num=0;}int find(int x){ if(root[x]!=x) { root[x]=find(root[x]);//update root[] } return root[x];}void merge(int a,int b){ int fa=find(a); int fb=find(b); if(fa!=fb) { root[fb]=fa; cnt[fa]+=cnt[fb]; } cout<<cnt[fa]<<endl;} int main(){ int cas; while(cin>>cas)//Input file contains multiple test cases. { while(cas--) { int n; clear(); cin>>n; while(n--) { string a,b; cin>>a>>b; if(p.find(a)==p.end())//no a in map { p[a]=++num;//cast a to ++num } if(p.find(b)==p.end())//no a in map { p[b]=++num;// } merge(p[a],p[b]); } } } system("pause"); return 0;}
二专这周结课,可以周末和米娜(当然主要是队长大人哈哈)一起做题了~可惜本周的两个还是参加不了……残念……
恩,发现上面这种做法现在超时了,感谢 littlesheep_shaun~~
不过,网上找找不超时的方法只有hash表,这,看不懂...
唉...ctrl-c&v
#include<iostream>#include<cstring>#include<cstdio>#define MAXN 200003using namespace std;typedef char Word[21];Word que[MAXN];const int HashSize = 1000003;int head[HashSize], next[MAXN], rear;int father[MAXN], num[MAXN]; inline void init_lookup_table(){ rear=1; memset(head, 0, sizeof(head));}inline int hash(Word &s){ int seed = 131, v=0; for(int i=0; i<strlen(s); ++i){ v = (v*seed + s[i]); } return (v & 0x7FFFFFFF)%HashSize;}bool try_to_insert(int s){ int h = hash(que[s]); int u = head[h]; while(u){ if(strcmp(que[u], que[s])==0) return false; u = next[u]; } next[s] = head[h]; head[h] = s; num[s] = 1; // 加入成功后, 该位置的人数变为1 return true;}int search(Word &s){ int h = hash(s); int u = head[h]; while(u){ if( strcmp(que[u], s)==0 ) return u; u = next[u]; }}void init_unionset(){ for(int i=0; i<MAXN; i++){ father[i]=i; num[i] = 0; }}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){ x = find(x); y = find(y); if(x!=y){ father[y] = x; num[x] += num[y]; }}int main(){ int T, n; Word name1, name2; while(scanf("%d", &T)!=EOF){while(T--){ init_lookup_table(); init_unionset(); scanf("%d",&n); if(n==0) { printf("0\n"); continue; } for(int i=0; i<n; ++i){ scanf("%s %s",name1, name2); strcpy(que[rear], name1); if(try_to_insert(rear)) ++rear; strcpy(que[rear], name2); if(try_to_insert(rear)) ++rear; int a = search(name1), b = search(name2); Union(a, b); printf("%d\n",num[find(a)]); }} } return 0;}
咕噜咕噜滚走
- hdu 5.1.7 virtual friend
- friend static virtual 总结
- hdu 4460 Friend Chains
- hdu 4460 Friend Chains
- hdu 1719 Friend
- hdu(1719)Friend
- HDU 4460 Friend Chains
- HDU 4460 Friend Chains
- Hdu 3823 Prime Friend
- friend(hdu 1719)
- hdu 1781 Friend
- hdu-1719 Friend
- hdu-4460 Friend Chains
- hdu---1719Friend
- HDU 1719--Friend【数学】
- HDU 1719 Friend 【数学题】
- hdu 1719 Friend
- HDU 5305Friend
- smarty登录
- Oracle取两条记录的交集INTERSECT
- 自定义工具条的使用(一)
- 文件的上传和下载
- 1的数目
- hdu 5.1.7 virtual friend
- JAVA笔试题目
- 异步串行通讯和同步串行通讯对比
- SVN版本控制
- ASP.NET 安全认证(Form 认证)
- 用XmlReader 读取XML
- c# 程序只能运行一个实例
- Ubuntu Server基本的环境搭建
- sql和ACCESS 中当前日期的表示