HDU 3172
来源:互联网 发布:如何修改淘宝店名 编辑:程序博客网 时间:2024/06/10 13:54
Virtual Friends
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 9472 Accepted Submission(s): 2758
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.
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).
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
1
3
Fred Barney
Barney Betty
Betty Wilma
Sample Output
2
3
4
Source
University of Waterloo Local Contest 2008.09
题意:朋友的朋友也是朋友,题目给出两个是朋友的人名,要求输出这当前两人所在朋友圈的人数。很显然的并查集。
注意:题目是多组输入,输入的T EOF才算结束。(自己做的时候一直wa找了好久才找到这个问题)
#include <cstdio>#include <cstring>#include <cmath>#include <iostream>#include <algorithm>#include <string>#include <map>#include <set>#include <vector>#include <queue>#include <stack>#define ll long long#define inf 0x3fffffffconst int maxn=200000+5;using namespace std;map<string,int> m;int root[maxn];int rank0[maxn];int num;void make_set(int n){ for(int i=1;i<=2*n;i++) { root[i]=i; rank0[i]=1; }}int find_set(int n){ if(n!=root[n]) { root[n]=find_set(root[n]); } return root[n];}void union_set(int a,int b){ int x=find_set(a); int y=find_set(b); if(x==y) { printf("%d\n",rank0[x]); return; } printf("%d\n",rank0[x]+rank0[y]); root[y]=x; rank0[x]+=rank0[y];}int main(){ int t,n,i,a,b; string n1,n2; while(~scanf("%d",&t)) { while(t--) { scanf("%d",&n); make_set(n); num=1; m.clear(); for(i=0;i<n;i++) { cin>>n1>>n2; if(!m.count(n1)) m[n1]=num++; if(!m.count(n2)) m[n2]=num++; a=m[n1]; b=m[n2]; //printf("%d %d\n",a,b); union_set(a,b); } } } return 0;}
- hdu 3172 && hdu 3047
- hdu 3172+hdu 3635+hdu 3926
- hdu 3172
- HDU 3172
- hdu 3172 Virtual Friends
- hdu 3172 Virtual Friends
- hdu 3172:Virtual Friends
- hdu 3172 Virtual Friends
- HDU 3172 (STL 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 3172Virtual Friends()
- hdu 3172 Virtual Friends
- 给初学者的RxJava2.0教程(四)
- 中断线程
- Hive metastore三种配置方式
- go 使用 goroutine channel 通信
- 【整理】Cordova 从相册中选择照片/拍照并上传
- HDU 3172
- Java学习笔记-《Java程序员面试宝典》-第5章Java Web-5.3框架(5.3.9-5.3.10)
- NFC
- 【笔记】Nexus的使用
- CXF框架
- TensorFlow 入门之第一个神经网络训练 MNIST
- 洛谷 3373(线段树,区间加减及乘法 询问区间和)
- Http请求中Content-Type讲解以及在Spring MVC中的应用(转载)
- netty(七)netty入门应用