HDU 并查集 - 3172 Virtual Friends

并查集题目,并的意思就是将两个不同类别的集合合并到一起,类似于两棵树合并;查的意思就是找到这个点所属集合的根节点。基本上并查集题目都是在大体架构上面加一些东西即可。并查集代码模板在这里点击打开链接

这一题为了找到输入的两个人组成的社交网络人数,也就是统计这两个人与前面的人组成的集合中有多少元素。我们加一个辅助数组sum,当两个集合并时,我们将父节点对应下标的sum值加上子节点的sum值,表达这个集合有多少元素。

#include<stdio.h>
#include<iostream>
#include<string>
#include<map>
using namespace std;
#define MAX 100000

int total;
map<string,int>A;
int  pa[MAX],sum[MAX];

int find_set(int x){
	if(x==pa[x])  return x;
    pa[x]=find_set(pa[x]);
    return pa[x];
}

void union_set(int x,int y){
	x = find_set(x);
	y = find_set(y);
	if(x!=y){
		pa[x]=y;
		sum[y]+=sum[x];
	}
}

int main(){
	int n,m;
	string  a,b;
	while(scanf("%d",&n)!=EOF){
	while(n--){
			total=0;
            A.clear();
            scanf("%d",&m);
            while(m--)
            {
                cin>>a>>b;
                if(A.find(a)==A.end())
                {
                    total++;
                    A[a]=total;
                    pa[total]=total;
                    sum[total]=1;  

                }
                if(A.find(b)==A.end())
                {
                    total++;
                    A[b]=total;
                    pa[total]=total;
                    sum[total]=1;
                }
                union_set(A[a],A[b]);
                int ans=find_set(A[a]);
                printf("%d\n",sum[ans]);
            }
	}
	}
}
时间: 2024-08-19 04:12:41

HDU 并查集 - 3172 Virtual Friends的相关文章

HDU 1232--并查集

畅通工程 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 35413    Accepted Submission(s): 18770 Problem Description 某省调查城镇交通状况,得到现有城镇道路统计表,表中列出了每条道路直接连通的城镇.省政府"畅通工程"的目标是使全省任何两个城镇间都可以实现交通(但不一定有

hdu 3635 Dragon Balls(并查集应用)

Problem Description Five hundred years later, the number of dragon balls will increase unexpectedly, so it's too difficult for Monkey King(WuKong) to gather all of the dragon balls together. His country has N cities and there are exactly N dragon bal

HDU 3172 Virtual Friends 带权并查集 -秩

ll T; while(~scanf("%d",&T)){ while(T--) { = = ... 思路: 用秩合并,看了题解才发现 if(fx == fy)要输出当前集合的秩而不是0... #include <cstdio> #include <iostream> #include <algorithm> #include <string.h> #include <vector> #include <map&

HDU 3172 Virtual Friends (map+并查集)

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), h

hdu 3172 Virtual Friends (并查集 + 字典树)

题目: 链接:点击打开链接 题意: 输入n,给出n行数据,每行有两个字符串,输出关系网络中朋友的个数,n行. 思路: 代码: #include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> using namespace std; const int N = 22; const int M = 200020; struct node { int c; node *chil

HDU 3172 Virtual Friends(带权并查集)

题目地址:HDU 3172 带权并查集水题.每次合并的时候维护一下权值.注意坑爹的输入.. 代码如下: #include <iostream> #include <cstdio> #include <string> #include <cstring> #include <stdlib.h> #include <math.h> #include <ctype.h> #include <queue> #inclu

hdu 3172 Virtual Friends(并查集)

题目比较简单,但作为长久不写题之后的热身题还是不错的. 统计每组朋友的朋友圈的大小. 如果a和b是朋友,这个朋友圈的大小为2,如果b和c也是朋友,那么a和c也是朋友,此时这个朋友圈的大小为3. 输入t,表示接下来有t组数据. 每组数据有n组朋友关系. 接下来n行,每行一组朋友关系,然后输出这组朋友的朋友圈大小,即有多少朋友. 然后又是t组数据……(这点好坑)重复上述输入,直到数据结束. 因为最多有10^5个人,那么如果用线性字符串数组保存人名,肯定超时得不要不要的,所以要用map(每次操作时间复

(hdu step 5.1.6)Virtual Friends(在结点为名字结点的条件下,求并查集的节点数)

题目: Virtual Friends Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 329 Accepted Submission(s): 98   Problem Description These days, you can do all sorts of things online. For example, you can use

HDU Virtual Friends(超级经典的带权并查集)

Virtual Friends Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 11092    Accepted Submission(s): 3221 Problem Description These days, you can do all sorts of things online. For example, you can