結果

問題 No.922 東北きりきざむたん
ユーザー first_vilfirst_vil
提出日時 2020-12-28 16:08:40
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 109 ms / 2,000 ms
コード長 6,804 bytes
コンパイル時間 2,345 ms
コンパイル使用メモリ 222,980 KB
実行使用メモリ 25,192 KB
最終ジャッジ日時 2024-10-03 06:20:33
合計ジャッジ時間 5,055 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 1 ms
5,248 KB
testcase_02 AC 2 ms
5,248 KB
testcase_03 AC 2 ms
5,248 KB
testcase_04 AC 1 ms
5,248 KB
testcase_05 AC 2 ms
5,248 KB
testcase_06 AC 2 ms
5,248 KB
testcase_07 AC 2 ms
5,248 KB
testcase_08 AC 2 ms
5,248 KB
testcase_09 AC 38 ms
12,848 KB
testcase_10 AC 26 ms
6,272 KB
testcase_11 AC 34 ms
10,700 KB
testcase_12 AC 18 ms
13,812 KB
testcase_13 AC 13 ms
6,400 KB
testcase_14 AC 60 ms
18,928 KB
testcase_15 AC 13 ms
14,356 KB
testcase_16 AC 90 ms
18,580 KB
testcase_17 AC 93 ms
18,448 KB
testcase_18 AC 95 ms
18,708 KB
testcase_19 AC 92 ms
18,584 KB
testcase_20 AC 95 ms
18,608 KB
testcase_21 AC 106 ms
18,792 KB
testcase_22 AC 109 ms
18,788 KB
testcase_23 AC 105 ms
17,512 KB
testcase_24 AC 103 ms
17,380 KB
testcase_25 AC 80 ms
17,920 KB
testcase_26 AC 85 ms
18,048 KB
testcase_27 AC 80 ms
18,052 KB
testcase_28 AC 56 ms
19,504 KB
testcase_29 AC 81 ms
25,192 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using VI = vector<int>;
using VL = vector<ll>;
using VS = vector<string>;
template<class T> using PQ = priority_queue<T, vector<T>, greater<T>>;
#define FOR(i,a,n) for(int i=(a);i<(n);++i)
#define eFOR(i,a,n) for(int i=(a);i<=(n);++i)
#define rFOR(i,a,n) for(int i=(n)-1;i>=(a);--i)
#define erFOR(i,a,n) for(int i=(n);i>=(a);--i)
#define SORT(a) sort(a.begin(),a.end())
#define rSORT(a) sort(a.rbegin(),a.rend())
#define fSORT(a,f) sort(a.begin(),a.end(),f)
#define all(a) a.begin(),a.end()
#define out(y,x) ((y)<0||h<=(y)||(x)<0||w<=(x))
#define tp(a,i) get<i>(a)
#ifdef _DEBUG
#define line cout << "-----------------------------\n"
#define stop system("pause")
#define debug(x) print(x)
#endif
constexpr ll INF = 1000000000;
constexpr ll LLINF = 1LL << 60;
constexpr ll mod = 1000000007;
constexpr ll MOD = 998244353;
constexpr ld eps = 1e-10;
constexpr ld pi = 3.1415926535897932;
template<class T>inline bool chmax(T& a, const T& b) { if (a < b) { a = b; return true; }return false; }
template<class T>inline bool chmin(T& a, const T& b) { if (a > b) { a = b; return true; }return false; }
inline void init() { cin.tie(nullptr); cout.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(15); }
template<class T>inline istream& operator>>(istream& is, vector<T>& v) { for (auto& a : v)is >> a; return is; }
template<class T, class U>inline istream& operator>>(istream& is, pair<T, U>& p) { is >> p.first >> p.second; return is; }
template<class T>inline vector<T> vec(size_t a) { return vector<T>(a); }
template<class T>inline vector<T> defvec(T def, size_t a) { return vector<T>(a, def); }
template<class T, class... Ts>inline auto vec(size_t a, Ts... ts) { return vector<decltype(vec<T>(ts...))>(a, vec<T>(ts...)); }
template<class T, class... Ts>inline auto defvec(T def, size_t a, Ts... ts) { return vector<decltype(defvec<T>(def, ts...))>(a, defvec<T>(def, ts...)); }
template<class T>inline void print(const T& a) { cout << a << "\n"; }
template<class T, class... Ts>inline void print(const T& a, const Ts&... ts) { cout << a << " "; print(ts...); }
template<class T>inline void print(const vector<T>& v) { for (int i = 0; i < v.size(); ++i)cout << v[i] << (i == v.size() - 1 ? "\n" : " "); }
template<class T>inline void print(const vector<vector<T>>& v) { for (auto& a : v)print(a); }
inline string reversed(const string& s) { string t = s; reverse(all(t)); return t; }
template<class T>inline T sum(const vector<T>& a, int l, int r) { return a[r] - (l == 0 ? 0 : a[l - 1]); }
template<class T>inline void END(T s) { print(s); exit(0); }
void END() { exit(0); }

VI dep, kind, cnt;
vector<VI> nxt, g;
int k = 0;
void dfs1(int cur, int par = -1) {
    kind[cur] = k;
    for (const int& to : g[cur]) {
        if (to != par) {
            dep[to] = dep[cur] + 1;
            nxt[0][to] = cur;
            dfs1(to, cur);
        }
    }
}
int lca(int a, int b) {
    if (dep[a] > dep[b])swap(a, b);
    int d = dep[b] - dep[a];
    FOR(j, 0, 18)if (d & (1 << j))b = nxt[j][b];
    if (a == b)return a;
    rFOR(j, 0, 18)if (nxt[j][a] != nxt[j][b])
        a = nxt[j][a], b = nxt[j][b];
    return nxt[0][a];
}
int dis(int a, int b) {
    return dep[a] + dep[b] - dep[lca(a, b)] * 2;
}

int dfs2(int cur, int par = -1) {
    for (const int& to : g[cur]) {
        if (to != par)cnt[cur] += dfs2(to, cur);
    }
    return cnt[cur];
}

vector<bool> yet;
vector<VI> ver;
void dfs3(ll& res, ll val, int cur, int par = -1) {
    yet[cur] = false;
    for (const int& to : g[cur]) {
        if (to != par) {
            ll backup = val;
            chmin(res, val += ver[kind[cur]].size() - cnt[to] * 2);
            dfs3(res, val, to, cur);
            val -= ver[kind[cur]].size() - cnt[to] * 2;
        }
    }
}


#include <algorithm>
#include <cassert>
#include <vector>

namespace atcoder {

struct dsu {
  public:
    dsu() : _n(0) {}
    dsu(int n) : _n(n), parent_or_size(n, -1) {}

    int merge(int a, int b) {
        assert(0 <= a && a < _n);
        assert(0 <= b && b < _n);
        int x = leader(a), y = leader(b);
        if (x == y) return x;
        if (-parent_or_size[x] < -parent_or_size[y]) std::swap(x, y);
        parent_or_size[x] += parent_or_size[y];
        parent_or_size[y] = x;
        return x;
    }

    bool same(int a, int b) {
        assert(0 <= a && a < _n);
        assert(0 <= b && b < _n);
        return leader(a) == leader(b);
    }

    int leader(int a) {
        assert(0 <= a && a < _n);
        if (parent_or_size[a] < 0) return a;
        return parent_or_size[a] = leader(parent_or_size[a]);
    }

    int size(int a) {
        assert(0 <= a && a < _n);
        return -parent_or_size[leader(a)];
    }

    std::vector<std::vector<int>> groups() {
        std::vector<int> leader_buf(_n), group_size(_n);
        for (int i = 0; i < _n; i++) {
            leader_buf[i] = leader(i);
            group_size[leader_buf[i]]++;
        }
        std::vector<std::vector<int>> result(_n);
        for (int i = 0; i < _n; i++) {
            result[i].reserve(group_size[i]);
        }
        for (int i = 0; i < _n; i++) {
            result[leader_buf[i]].push_back(i);
        }
        result.erase(
            std::remove_if(result.begin(), result.end(),
                           [&](const std::vector<int>& v) { return v.empty(); }),
            result.end());
        return result;
    }

  private:
    int _n;
    std::vector<int> parent_or_size;
};

}  // namespace atcoder

int main() {
    init();

    //input-begin
    int n, m, q; cin >> n >> m >> q;
    atcoder::dsu uf(n);
    g.resize(n);
    FOR(i, 0, m) {
        int u, v; cin >> u >> v;
        uf.merge(--u, --v);
        g[u].push_back(v);
        g[v].push_back(u);
    }
    //input-end

    //lca-init-begin
    dep.resize(n, -1), kind.resize(n);
    nxt = defvec<int>(-1, 18, n);
    FOR(i, 0, n)if (dep[i] == -1) {
        dep[uf.leader(i)] = 0;
        dfs1(uf.leader(i));
        ++k;
    }
    FOR(j, 0, 17)FOR(i, 0, n)if (nxt[j][i] != -1)
        nxt[j + 1][i] = nxt[j][nxt[j][i]];
    //lca-init-end

    //kueri
    cnt.resize(n), ver.resize(k);
    ll ans = 0;
    while (q--) {
        int a, b; cin >> a >> b;
        if (uf.same(--a, --b))ans += dis(a, b);
        else {
            ver[kind[a]].push_back(a);
            ver[kind[b]].push_back(b);
            ++cnt[a], ++cnt[b];
        }
    }

    yet.resize(n, true);
    FOR(i, 0, n) {
        if (!yet[i])continue;
        int rt = uf.leader(i), idx = kind[rt];
        ll val = 0;
        for (const int& v : ver[idx]) {
            val += dis(rt, v);
        }
        ll res = val;
        dfs2(rt);
        dfs3(res, val, rt);
        ans += res;
    }
    print(ans);

    return 0;
}
0