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

P2814 家谱【map型的并查集】

时间:2020-06-28 22:21:52      阅读:51      评论:0      收藏:0      [点我收藏+]

标签:com   height   mic   ace   代码   www   初始   name   load   

题目

https://www.luogu.com.cn/problem/P2814

技术图片

 

 思路

我们跨一使用string类型的map直接进行并查集!

注意这种形式的map的初始化!!

代码

#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<map>
using namespace std;
map<string, string>list;
string find(string x)
{
    if (x == list[x])return x;
    return list[x] = find(list[x]);
}
int main()
{
    string temp, father="";
    while (1)
    {
        cin >> temp;
        if (temp == "$")break;
        if (temp[0] == #) { father = temp.substr(1, temp.length() - 1);  if(list[temp.substr(1, temp.length() - 1)]=="")list[temp.substr(1, temp.length() - 1)] = father;}
        if(temp[0]==+) list[temp.substr(1, temp.length() - 1)] = father;
        if (temp[0] == ?) { cout << temp.substr(1, temp.length() - 1) << <<find(temp.substr(1, temp.length() - 1)) << endl; }
    }


}

 

P2814 家谱【map型的并查集】

标签:com   height   mic   ace   代码   www   初始   name   load   

原文地址:https://www.cnblogs.com/Jason66661010/p/13205244.html

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