当前位置:网站首页>693. Travel sequencing

693. Travel sequencing

2022-07-07 06:13:00 Ray. C.L

 Insert picture description here

Ideas : Find a starting point ( The degree of 0), Go through it

Code :

#include <iostream>
#include <cstring>
#include <algorithm>
#include <unordered_map>
#include <unordered_set>

using namespace std;

int main()
{
    
    int T;
    cin >> T;
    for(int i = 1; i <= T; i ++){
    
        int n;
        cin >> n;
        
        unordered_map<string, string> next;
        unordered_set<string> S;
        while(n --){
    
            string a, b;
            cin >> a >> b;
            next[a] = b;
            S.insert(b);
        }
        
        string head;
        
        for(auto& [a, b] : next)
            if(!S.count(a)){
    
                head = a;
                break;
            }
            
        printf("Case #%d: ",i);
        
        while(next[head].size()){
    
            cout << head <<  '-' << next[head] << ' ';
            head = next[head];
        }
        cout << endl;
                
    }
}
原网站

版权声明
本文为[Ray. C.L]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/188/202207070108470931.html