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

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

时间:2014-07-01 08:14:07      阅读:189      评论:0      收藏:0      [点我收藏+]

标签:blog   http   数据   2014   os   代码   

题目:

        链接:点击打开链接

题意:

        输入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 *child[52];
    node()
    {
        c = 0;
        for(int i=0; i<52; i++)
            child[i] = NULL;
    }
}*root1;

int n;
int cnt;
int root[M];
char stra[N],strb[N];

int insertTrie(char s[])//字典树功能:将名字映射成代表该名字的一个整数
{
    node *p = root1;
    int len = strlen(s);
    for(int k,i=0; i<len; i++,p=p->child[k])
    {
        if(s[i] >= 'a' && s[i] <= 'z')
            k = s[i] - 'a';
        else
            k = s[i] - 'A' + 26;
        if(!p->child[k])
            p->child[k] = new node();
    }
    if(p->c)
        return p->c;
    return p->c = ++cnt;
}

void init()
{
    for(int i=0; i<M; i++)
        root[i] = -1;
}

int findset(int x)
{
    int r;
    for(r=x; root[r]>0; r=root[r]);
    while(r != x)
    {
        int temp = root[x];
        root[x] = r;
        x = temp;
    }
    return r;
}

int mergeset(int r1,int r2)
{
    int temp = root[r1] + root[r2];
    if(root[r1] > root[r2])
    {
        root[r1] = r2;
        root[r2] = temp;
    }
    else
    {
        root[r2] = r1;
        root[r1] = temp;
    }
    return temp;
}

void dealTrie(node *p)
{
    for(int i=0; i<52; i++)
    {
        if(p->child[i])
            dealTrie(p->child[i]);
    }
    delete p;
    p = NULL;
}

int main()
{
    //freopen("input.txt","r",stdin);
    int t;
    while(scanf("%d",&t) != EOF)
    {
        while(t--)
        {
            scanf("%d",&n);
            getchar();
            cnt = 0;
            init();
            root1 = new node();
            for(int i=0; i<n; i++)
            {
                scanf("%s%s",stra,strb);
                int x = insertTrie(stra);
                int y = insertTrie(strb);
                int fx = findset(x);
                int fy = findset(y);
                if(fx != fy)
                    printf("%d\n",abs(mergeset(fx,fy)));
                else
                    printf("%d\n",abs(root[fx]));
            }
            dealTrie(root1);
        }
    }
    return 0;
}

------------------------------------------------------

战斗,从不退缩;奋斗,永不停歇~~~~~~~~~~~~

hdu 3172 Virtual Friends (并查集 + 字典树),布布扣,bubuko.com

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

标签:blog   http   数据   2014   os   代码   

原文地址:http://blog.csdn.net/u013147615/article/details/30225831

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