結果

問題 No.938 賢人を探せ
ユーザー koi_kotya
提出日時 2019-12-01 00:47:40
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
WA  
実行時間 -
コード長 1,596 bytes
コンパイル時間 2,409 ms
コンパイル使用メモリ 184,072 KB
実行使用メモリ 7,936 KB
最終ジャッジ日時 2024-11-21 03:47:42
合計ジャッジ時間 3,367 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 18 WA * 3
権限があれば一括ダウンロードができます

ソースコード

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