結果

問題 No.938 賢人を探せ
ユーザー koi_kotyakoi_kotya
提出日時 2019-12-01 00:47:40
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,596 bytes
コンパイル時間 1,800 ms
コンパイル使用メモリ 184,304 KB
実行使用メモリ 8,064 KB
最終ジャッジ日時 2024-05-01 03:27:56
合計ジャッジ時間 3,095 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 46 ms
7,936 KB
testcase_01 AC 2 ms
6,944 KB
testcase_02 AC 1 ms
6,940 KB
testcase_03 AC 2 ms
6,944 KB
testcase_04 WA -
testcase_05 AC 2 ms
6,940 KB
testcase_06 AC 1 ms
6,940 KB
testcase_07 AC 2 ms
6,940 KB
testcase_08 WA -
testcase_09 WA -
testcase_10 AC 2 ms
6,944 KB
testcase_11 AC 42 ms
6,944 KB
testcase_12 AC 31 ms
6,940 KB
testcase_13 AC 32 ms
6,940 KB
testcase_14 AC 33 ms
6,944 KB
testcase_15 AC 33 ms
6,940 KB
testcase_16 AC 31 ms
6,944 KB
testcase_17 AC 32 ms
6,940 KB
testcase_18 AC 32 ms
6,940 KB
testcase_19 AC 32 ms
6,944 KB
testcase_20 AC 32 ms
6,944 KB
testcase_21 AC 32 ms
6,940 KB
testcase_22 AC 46 ms
8,064 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int,int> P;

#define p_ary(ary,a,b) do { cout << "["; for (int count = (a);count < (b);++count) cout << ary[count] << ((b)-1 == count ? "" : ", "); cout << "]\n"; } while(0)
#define p_map(map,it) do {cout << "{";for (auto (it) = map.begin();;++(it)) {if ((it) == map.end()) {cout << "}\n";break;}else cout << "" << (it)->first << "=>" << (it)->second << ", ";}}while(0)

struct UnionFind {
private:
    vector<int> par,depth;
public:
    UnionFind (int n) {
        par.resize(n);
        depth = vector<int>(n,0);
        for (int i = 0;i < n;++i) par[i] = i;
    }

    int find(int x) {
        if (par[x] == x) return x;
        else return par[x] = find(par[x]);
    }

    void unite(int x,int y) {
        x = find(x);
        y = find(y);
        if (x == y) return;
        par[y] = x;
        if (depth[x] == depth[y]) depth[x]++;
        else depth[x] = max(depth[x],depth[y]);
    }

    bool same(int x,int y) {
        return find(x) == find(y);
    }
};

int main() {
    int n,m;
    cin >> n;
    vector<string> a(n),b(n);
    for (int i = 0;i < n;++i) cin >> a[i] >> b[i];
    map<string,int> mp;
    for (int i = 0;i < n;++i) mp[a[i]] = i;
    m = n;
    for (int i = 0;i < n;++i) if (mp.find(b[i]) == mp.end()) mp[b[i]] = m,m++;
    UnionFind uf(m);
    for (int i = 0;i < n;++i) uf.unite(mp[b[i]],mp[a[i]]);
    vector<bool> used(m,false);
    for (int i = 0;i < n;++i) if (uf.find(mp[b[i]]) == mp[b[i]] && !used[mp[b[i]]]) {
        cout << b[i] << "\n";
        used[mp[b[i]]] = true;
    }
}
0