1 条题解

  • 0
    @ 2025-8-24 21:41:54

    自动搬运

    查看原文

    来自洛谷,原作者为

    avatar zhmshitiancai
    **

    搬运于2025-08-24 21:41:54,当前版本为作者最后更新于2016-12-25 16:01:29,作者可能在搬运后再次修改,您可在原文处查看最新版

    自动搬运只会搬运当前题目点赞数最高的题解,您可前往洛谷题解查看更多

    以下是正文


    发一个简单的题解 用stl的map把他和父亲直接连起来

    不要转成序号再做:

    #include<cstdio>
    #include<iostream>
    #include<map>
    using namespace std;
    map<string,string>p;
    string find(string x)
    {
        if(x!=p[x]) 
            p[x]=find(p[x]);
        return  p[x];
    }
    string s,s1;
    int main()
    {
        char ch;
        cin>>ch;
        while(ch!='$')
        {
            cin>>s;
            if(ch=='#')
            {
                s1=s;
                if(p[s]=="") p[s]=s;
            }
            else if(ch=='+')
                p[s]=s1;
            else 
                cout<<s<<' '<<find(s)<<endl;    
            cin>>ch;
        }
        return 0;
    }
    
    • 1

    信息

    ID
    1890
    时间
    1000ms
    内存
    125MiB
    难度
    3
    标签
    递交数
    0
    已通过
    0
    上传者