P2814 家谱 map模版

#include<iostream>
#include<string>
#include<map>
using namespace std;
map<string,string> par;
string find(string x)
{
    return x==par[x] ? x : par[x]=find(par[x]);
}
int main()
{
    char c;
    string s,now;
    while(cin>>c)
    {
        if(c=='$')
            break;
        cin>>s;
        if(c=='#')
        {
            now=s;
            if(!par.count(s))
                par[s]=s;
        }
        else if(c=='+')
            par[s]=now;
        else
            cout<<s<<' '<<find(s)<<endl;
    }
    return 0;
}

  就是个简单的模版(才知道并查集可以直接用map做)

  map<int,int>不就是数组么……>&<

  直接上代码:(第一次这么短)

原文地址:https://www.cnblogs.com/popo-black-cat/p/10053778.html