1 solutions

  • 0
    @ 2025-11-5 17:21:05

    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

    Information

    ID
    18331
    Time
    1000ms
    Memory
    128MiB
    Difficulty
    (None)
    Tags
    # Submissions
    0
    Accepted
    0
    Uploaded By