結果

問題 No.922 東北きりきざむたん
ユーザー first_vilfirst_vil
提出日時 2020-12-28 16:07:32
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 176 ms / 2,000 ms
コード長 5,106 bytes
コンパイル時間 2,724 ms
コンパイル使用メモリ 216,140 KB
実行使用メモリ 25,056 KB
最終ジャッジ日時 2024-04-14 09:16:51
合計ジャッジ時間 7,366 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 2 ms
6,816 KB
testcase_02 AC 2 ms
6,944 KB
testcase_03 AC 2 ms
6,944 KB
testcase_04 AC 2 ms
6,944 KB
testcase_05 AC 2 ms
6,940 KB
testcase_06 AC 2 ms
6,944 KB
testcase_07 AC 2 ms
6,944 KB
testcase_08 AC 2 ms
6,940 KB
testcase_09 AC 52 ms
12,712 KB
testcase_10 AC 32 ms
6,940 KB
testcase_11 AC 47 ms
10,700 KB
testcase_12 AC 25 ms
13,812 KB
testcase_13 AC 16 ms
6,940 KB
testcase_14 AC 92 ms
18,828 KB
testcase_15 AC 15 ms
14,352 KB
testcase_16 AC 138 ms
18,448 KB
testcase_17 AC 142 ms
18,584 KB
testcase_18 AC 142 ms
18,704 KB
testcase_19 AC 148 ms
18,572 KB
testcase_20 AC 145 ms
18,448 KB
testcase_21 AC 166 ms
18,916 KB
testcase_22 AC 161 ms
18,784 KB
testcase_23 AC 176 ms
17,508 KB
testcase_24 AC 170 ms
17,384 KB
testcase_25 AC 124 ms
18,048 KB
testcase_26 AC 124 ms
18,048 KB
testcase_27 AC 124 ms
18,040 KB
testcase_28 AC 94 ms
19,376 KB
testcase_29 AC 132 ms
25,056 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 <atcoder/dsu>
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