結果

問題 No.1813 Magical Stones
ユーザー ぷらぷら
提出日時 2022-01-14 23:20:00
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,567 bytes
コンパイル時間 2,594 ms
コンパイル使用メモリ 215,616 KB
実行使用メモリ 13,056 KB
最終ジャッジ日時 2024-04-30 17:21:24
合計ジャッジ時間 6,936 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 AC 2 ms
6,940 KB
testcase_02 WA -
testcase_03 AC 51 ms
13,056 KB
testcase_04 WA -
testcase_05 AC 2 ms
6,940 KB
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 AC 51 ms
12,928 KB
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 AC 3 ms
6,944 KB
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 AC 151 ms
7,424 KB
testcase_29 AC 130 ms
8,320 KB
testcase_30 AC 119 ms
7,936 KB
testcase_31 AC 143 ms
9,344 KB
testcase_32 WA -
testcase_33 WA -
testcase_34 WA -
testcase_35 WA -
testcase_36 WA -
testcase_37 WA -
testcase_38 WA -
testcase_39 WA -
testcase_40 WA -
testcase_41 WA -
testcase_42 WA -
testcase_43 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

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

struct UnionFind {
    vector<int> par;
    vector<int> size;
    UnionFind(int n) {
        par.resize(n);
        size.resize(n,1);
        for(int i = 0; i < n; i++) {
            par[i] = i;
        }
    }
    int find(int x) {
        if(par[x] == x) {
            return x;
        }
        return par[x] = find(par[x]);
    }
    bool same(int x, int y) {
        return find(x) == find(y);
    }
    int consize(int x) {
        return size[find(x)];
    }
    bool unite(int x, int y) {
        x = find(x);
        y = find(y);
        if(x == y) {
            return false;
        }
        if(size[x] > size[y]) {
            swap(x,y);
        }
        par[x] = y;
        size[y] += size[x];
        return true;
    }
};

int main() {
    int N,M;
    cin >> N >> M;
    vector<int>cnt(N);
    vector<vector<int>>road(N);
    UnionFind uf(N);
    for(int i = 0; i < M; i++) {
        int A,B;
        cin >> A >> B;
        A--;
        B--;
        uf.unite(A,B);
        road[A].push_back(B);
        cnt[B]++;
    }
    map<int,pair<int,int>>tmp;
    for(int i = 0; i < N; i++) {
        if(!cnt[i]) {
            tmp[uf.find(i)].first++;
        }
        if(road[i].empty()) {
            tmp[uf.find(i)].second++;
        }
    }
    int ans = 0,ret = 0;
    for(auto i:tmp) {
        ans += max(i.second.second,i.second.first);
        if(i.second.second > 0 || i.second.first > 0) {
            ret++;
        }
    }
    cout << min(N,ans-max(0,ret-1)+(int)tmp.size()-1+min(1,ret)) << endl;
}
0