hdu 3172

http://acm.hdu.edu.cn/showproblem.php?pid=3172

题意:输出每对朋友的关系网大小

并查集的时候维护一个数组记录根节点的大小即可,水题,这题坑在T组数据这个也要读到EOF,开始莫名其妙wa...

#include <iostream>
#include <cstdio>
#include <cstring>
#include <map>
#include <algorithm>
#include <queue>
#include <cmath>
#include <stack>
#include <set>

using namespace std;

int fa[100005],sum[100005],cnt[100005];

int find(int x){
    if(fa[x]!=x){
        int pre=fa[x];
        fa[x]=find(fa[x]);
        sum[x]+=sum[pre];
    }
    return fa[x];
}

int main(){
    int T;
    while(~scanf("%d",&T)){
        while(T--){
            int n;
            scanf("%d",&n);
            for(int i=0;i<100005;i++){
                fa[i]=i;
                cnt[i]=1;
            }
            memset(sum,0,sizeof(sum));
            map <string,int> mp;
            int st=1;
            while(n--){
                char s1[25],s2[25];
                scanf("%s%s",s1,s2);
                if(!mp[s1])mp[s1]=st++;
                if(!mp[s2])mp[s2]=st++;
                int pa=find(mp[s1]);
                int pb=find(mp[s2]);
                if(pa!=pb){
                    fa[pa]=pb;
                    cnt[pb]+=cnt[pa];
                }
                printf("%d\n",cnt[pb]);
            }
        }
    }
    return 0;
}

时间: 2024-12-09 01:41:04

hdu 3172的相关文章

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 带权并查集 -秩

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 (并查集 + 字典树)

题目: 链接:点击打开链接 题意: 输入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

原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=3172 并查集的运用... 1 #include<algorithm> 2 #include<iostream> 3 #include<cstdlib> 4 #include<cstdio> 5 #include<map> 6 using std::map; 7 using std::string; 8 const int Max_N = 100010

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 并查集+map

/*这里将fa[]数组初始化为-1比较方便 输入格式有点坑 看的讨论*/ 1 #include "cstdio" 2 #include "iostream" 3 #include "cstring" 4 #include "vector" 5 #include "queue" 6 #include "map" 7 #include "string" 8 #includ

hdu 3172 Virtual Friends (映射并查集)

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

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(并查集)

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