結果

問題 No.922 東北きりきざむたん
ユーザー 🍮かんプリン🍮かんプリン
提出日時 2019-11-08 22:39:57
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 2,198 bytes
コンパイル時間 1,811 ms
コンパイル使用メモリ 164,428 KB
実行使用メモリ 11,496 KB
最終ジャッジ日時 2023-10-13 04:18:36
合計ジャッジ時間 6,746 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 AC 2 ms
4,372 KB
testcase_03 WA -
testcase_04 AC 1 ms
4,368 KB
testcase_05 WA -
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 WA -
testcase_15 AC 5 ms
5,800 KB
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 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 AC 118 ms
10,164 KB
testcase_29 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include "bits/stdc++.h"
#define REP(i, n) for(int i = 0; i < int(n); i++)
#define FOR(i,n,m) for(int i = int(n); i < int(m); i++)
using namespace std;
typedef long long ll;
const int MOD = 1e9 + 7;
const int INF = 1e9 + 6;
const ll LLINF = 1e18 + 1;

// UnionFind
class UnionFind {
private:
    vector<int> par;
public:
    UnionFind(int n) {
        par.resize(n, -1);
    }

    int root(int x) {
        if (par[x] < 0) return x;
        return par[x] = root(par[x]);
    }

    bool unite(int x, int y) {
        int rx = root(x);
        int ry = root(y);
        if (rx == ry) return false;
        if (size(rx) < size(ry)) swap(rx, ry);
        par[rx] += par[ry];
        par[ry] = rx;
        return true;
    }

    bool same(int x, int y) {
        int rx = root(x);
        int ry = root(y);
        return rx == ry;
    }

    int size(int x) {
        return -par[root(x)];
    }
};

ll dfs(int v,int depth, const vector<vector<int>> &G, vector<int> &cnt) {
    ll res = (ll)cnt[v] * depth;
    
    for (auto u : G[v]) {
        res += dfs(u, depth+1,G, cnt);
    }
    return res;
}

int main() {
    int n, m, q; cin >> n >> m >> q;
    UnionFind uni(n);
    vector<vector<int>> G(n);
    REP(i, m) {
        int u, v; scanf("%d %d", &u, &v);
        u--; v--;
        uni.unite(u,v);
        G[u].push_back(v);
        G[v].push_back(u);
    }
    vector<int> cnt(n);
    map<int, int> mp;
    REP(i, q) {
        int a, b; scanf("%d %d", &a, &b);
        a--; b--;
        if (uni.same(a, b)) continue;
        cnt[a]++;
        cnt[b]++;
        if (cnt[mp[uni.root(a)]] < cnt[a]) {
            mp[uni.root(a)] = a;
        }
        if (cnt[mp[uni.root(b)]] < cnt[b]) {
            mp[uni.root(b)] = b;
        }
    }
    ll ans = 0;
    struct ttt {
        int a, b, c;
    };
    for (auto p : mp) {
        queue<ttt> que;
        que.push({p.second, -1, 0});
        while (!que.empty()) {
            auto pp = que.front(); que.pop();
            ans += (ll)cnt[pp.a] * pp.c;
            for (auto v : G[pp.a]) {
                if (v == pp.b) continue;
                que.push({v,pp.a,pp.c + 1});
            }
        }
    }
    cout << ans << endl;
    return 0;
}
0