1 条题解

  • 0
    @ 2025-9-10 0:11:12

    C++ :

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

    信息

    ID
    3105
    时间
    1000ms
    内存
    128MiB
    难度
    (无)
    标签
    递交数
    0
    已通过
    0
    上传者