码迷,mamicode.com
首页 > 其他好文 > 详细

hdu 3172

时间:2014-11-27 00:10:19      阅读:295      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   io   ar   color   os   sp   for   

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

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

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

bubuko.com,布布扣
#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;
}
View Code

 

hdu 3172

标签:style   blog   http   io   ar   color   os   sp   for   

原文地址:http://www.cnblogs.com/xiaohongmao/p/4125363.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!