結果

問題 No.938 賢人を探せ
ユーザー kappybarkappybar
提出日時 2020-03-30 12:06:43
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 1,212 bytes
コンパイル時間 2,136 ms
コンパイル使用メモリ 181,504 KB
実行使用メモリ 17,460 KB
最終ジャッジ日時 2023-09-02 11:56:31
合計ジャッジ時間 9,398 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 TLE -
testcase_01 -- -
testcase_02 -- -
testcase_03 -- -
testcase_04 -- -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<n;i++)
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
const int INF = 1e9;
const int MOD = 1000000007;

int n = 20005;
vector<vector<int>> graph(2*n,vector<int>());
vector<int> visited(2*n,-1);

void dfs(int i){
    if(graph[i].size() == 0) visited[i] = 1;
    else{
        visited[i] = 0;
        for(int p:graph[i]){
            if(visited[p] == -1) dfs(p);
        }
    }
}

int main(){
    cin >> n;
    map<string,int> mp;
    int id = 0;
    rep(i,n){
        string a,b;
        cin >> a >> b;
        if(mp.find(a) == mp.end()){
            mp[a] = id++;
        } 
        if(mp.find(b) == mp.end()){
            mp[b] = id++;
        }
        graph[mp[a]].push_back(mp[b]);
    }

    rep(i,id){
        if(visited[i] != -1) continue;
        dfs(i);
    }

    vector<int> idx;
    rep(i,id){
        if(visited[i] == 1){
            idx.push_back(i);
        }
    }


    vector<string> ans;
    for(int i:idx){
        for(auto p:mp){
            if(i == p.second){
                ans.push_back(p.first);
                break;
            }
        }
    }

    for(string s:ans) cout << s << endl;

    return 0;
}
0