Virtual Friends(普通的并查集)
来源:互联网 发布:怎样禁止电脑安装软件 编辑:程序博客网 时间:2024/06/18 10:27
Virtual Friends
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 504 Accepted Submission(s): 161
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
首先补充一下 c++ 几个常用的STL库函数
queue:
头文件是#include<queue>
queue<int> p;p.push(1);//入队int t = p.front();//访问队首元素p.pop();//出队p.empty();//判断队列是否为空p.back();//访问队尾元素p.size();//队列的长度
stack:
头文件是:#include<stack>
stack<int> q;q.push(1);//入栈int p = q.top();//访问栈顶元素q.size();//堆栈的大小q.empty();//判断堆栈是否为空q,pop();//弹出堆栈
set:
头文件是:#include<set>
特点:所有元素有且仅有一遍 并且自动排序默认从小到大查找的时间复杂度为O(logn)
set m;
m.insert(1);//向集合中插入元素
m.find(1);//查找元素
if(m.find(1)==m.end())
{
//未找到
}
else
{
//找到了
}
m.size();//集合的大小
m.clear();//清除集合里所有元素
m.empty();//判断当前集合是否为空
m.erase(1);//删除集合中元素1
//集合的遍历
set::iterator it = m.begin();
for(;it!=m.end();it++)
{
printf(“%d “,*it);
}
vector :
头文件:#include<vector>
vector<int> mm;mm.push_back(1);//向数组里面添加元素mm[0]//使用下标访问元素mm.front();//获取数组里面第一个元素mm.back();//获取元素里面最后一个元素mm.pop_back();//删除数组最后的元素 并且数组的长度减一mm.size();//数组的大小mm.insert(mm.beign() +i,x);//在第i+1个元素前面插入一个元素xmm.erase(mm.beign() +i);//删除第i+1个元素sort(mm.begin(),mm.end());//从小到大排序reverse(mm.begin(),mm.end());//将数组翻转
map:
头文件:#include<map>
map
#include<iostream>#include<stdio.h>#include<algorithm>#include<string>#include<map>#include<string.h>using namespace std; map<string,int> mymap;//string 类型为键值 int类型为value int ff[100007],rrank[100007];int father(int x){ if(x!=ff[x]) { ff[x] = father(ff[x]); } return ff[x];}void union_set(int a,int b){ int pa = father(a); int pb = father(b); if(pa!=pb) { ff[pa] = pb; rrank[pb] +=rrank[pa]; }}int main(){ int ncase; while(cin>>ncase) { while(ncase--) { int n; cin>>n; int cnt = 1; for(int i =1;i<100007;i++) { ff[i] = i; rrank[i] = 1; } for(int i =1;i<=n;i++) { string str1,str2; cin>>str1>>str2; if(mymap.find(str1)==mymap.end())//find()函数寻找键值 如果找到了则不等于 找不到则为end() { mymap[str1] = cnt++;//如果mymap[str1]存在 这样就是直接覆盖 不存在就是直接赋值 } if(mymap.find(str2)==mymap.end()) { mymap[str2] = cnt++; } union_set(mymap[str1],mymap[str2]); cout<<rrank[father(mymap[str1])]<<endl; } } } return 0;}
- Virtual Friends(普通的并查集)
- Virtual Friends (并查集)
- HDU 3172 Virtual Friends(并查集)
- HDU 3172 Virtual Friends(带权并查集)
- hdu-3172 Virtual Friends(并查集+容器)
- HDOJ 3172 Virtual Friends(并查集 + STL)
- Virtual Friends 字典树 并查集
- HDU3172 Virtual Friends 并查集
- hdu 3172 virtual friends 并查集
- HDU 3172 Virtual Friends (并查集)
- 【并查集】 HDU 3172 Virtual Friends
- Hdu3172 - Virtual Friends - 并查集
- hdu 3172 Virtual Friends(并查集)
- hdu 3172 Virtual Friends 并查集
- HDU 并查集 - 3172 Virtual Friends
- 3172 Virtual Friends【并查集】
- HDU 3172 Virtual Friends (并查集)
- hdu3172 Virtual Friends--并查集
- 观察者模式
- 41. First Missing Positive
- 自定义控件(三种)
- Ajax 知识地址总汇
- Android BLE Gatt返回错误对应宏
- Virtual Friends(普通的并查集)
- SVN 使用简介及个人总结
- pip命令显示不是内部或外部命令,也不是可运行的程序或批处理文件
- Myeclipse运行程序时,发生异常:Exception in thread ""http-bio-8080"-exec-1" java.lang.OutOfMemoryError: PermGen
- Android 高级混淆和代码保护技术
- LitPal数据库
- java无法收取邮件问题 Received fatal alert: handshake_failure
- HTTP文件断点上传
- Linux CentOS7 64位 配置nginx