結果

問題 No.922 東北きりきざむたん
ユーザー haruki_Kharuki_K
提出日時 2019-11-10 19:57:58
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 480 ms / 2,000 ms
コード長 8,974 bytes
コンパイル時間 2,445 ms
コンパイル使用メモリ 198,652 KB
実行使用メモリ 49,592 KB
最終ジャッジ日時 2024-09-15 05:04:33
合計ジャッジ時間 7,346 ms
ジャッジサーバーID
(参考情報)
judge2 / judge6
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 3 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 3 ms
5,376 KB
testcase_09 AC 70 ms
18,240 KB
testcase_10 AC 32 ms
8,028 KB
testcase_11 AC 56 ms
14,208 KB
testcase_12 AC 66 ms
27,116 KB
testcase_13 AC 24 ms
9,532 KB
testcase_14 AC 121 ms
29,908 KB
testcase_15 AC 65 ms
29,740 KB
testcase_16 AC 142 ms
26,008 KB
testcase_17 AC 151 ms
26,620 KB
testcase_18 AC 153 ms
25,372 KB
testcase_19 AC 146 ms
26,252 KB
testcase_20 AC 147 ms
26,124 KB
testcase_21 AC 132 ms
21,832 KB
testcase_22 AC 125 ms
21,452 KB
testcase_23 AC 210 ms
32,464 KB
testcase_24 AC 213 ms
32,836 KB
testcase_25 AC 477 ms
35,012 KB
testcase_26 AC 480 ms
34,976 KB
testcase_27 AC 478 ms
35,016 KB
testcase_28 AC 133 ms
38,740 KB
testcase_29 AC 203 ms
49,592 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

// {{{ TEMPLATES
#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for (int i = 0; i < int(n); i++)
#define rep1(i,n) for (int i = 1; i <= int(n); i++)
#define repR(i,n) for (int i = int(n)-1; i >= 0; i--)
#define rep1R(i,n) for (int i = int(n); i >= 1; i--)
#define loop(i,a,B) for (int i = a; i B; i++)
#define loopR(i,a,B) for (int i = a; i B; i--)
#define all(x) (x).begin(), (x).end()
#define allR(x) (x).rbegin(), (x).rend()
#define eb emplace_back
#define mp make_pair
#define fst first
#define snd second
#ifdef LOCAL
#define dump(...) cerr << "[" << __LINE__ << ":" << __FUNCTION__ << "] ", my_dmp(#__VA_ARGS__, __VA_ARGS__)
#define say(x) cerr << "[" << __LINE__ << ":" << __FUNCTION__ << "] " << x << endl;
void my_dmp(const char*) { cerr << endl; }
template <class T, class... U> void my_dmp(const char *s, T const& x, U const& ...y) { const char *o = "({[", *e = "]})"; for (int i = 0; *s != '\0'; cerr << *s++) { if (count(o,o+3,*s)) i++; if (count(e,e+3,*s)) i--; if (!i && *s == ',') break; } cerr << " = " << x; if (*s == ',') cerr << ", ", s++; my_dmp(s, y...); }
#else
#define dump(...)
#define say(x)
#endif
using ll = long long;
using ld = long double;
#define int ll
#define double ld
template <class T> using pque_max = priority_queue<T>;
template <class T> using pque_min = priority_queue<T, vector<T>, greater<T> >;
template <class T, class = typename T::iterator, class = typename enable_if<!is_same<T, string>::value>::type>
ostream& operator<<(ostream& os, T const& v) { os << "{"; for (auto const& x : v) os << " " << x; return os << " }"; }
template <class T> istream& operator>>(istream& is, vector<T>& v) { for (auto& x : v) is >> x; return is; }
template <class T, class S> ostream& operator<<(ostream& os, pair<T,S> const& p) { return os << "(" << p.first << ", " << p.second << ")"; }
template <class T, class S> istream& operator>>(istream& is, pair<T,S>& p) { return is >> p.first >> p.second; }
template <size_t i, class T> typename enable_if<i >= tuple_size<T>::value>::type output_tuple(ostream&, T const&) { }
template <size_t i = 0, class T> typename enable_if<i < tuple_size<T>::value>::type
output_tuple(ostream& os, T const& t) { os << (i ? " " : "") << get<i>(t); output_tuple<i+1,T>(os,t); }
template <class... T> ostream& operator<<(ostream& os, tuple<T...> const& t) { return output_tuple(os,t), os; }
struct my_Init { my_Init() { cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(15); } } my_init;
template <class T, size_t d> struct vec_impl {
    using type = vector<typename vec_impl<T,d-1>::type>;
    template <class... U> static type make_v(size_t n, U&&... x) { return type(n, vec_impl<T,d-1>::make_v(forward<U>(x)...)); }
};
template <class T> struct vec_impl<T,0> { using type = T; static type make_v(T const& x = {}) { return x; } };
template <class T, size_t d> using vec = typename vec_impl<T,d>::type;
template <class T, size_t d, class... Args> auto make_v(Args&&... args) { return vec_impl<T,d>::make_v(forward<Args>(args)...); }
template <class T> void quit(T const& x) { cout << x << endl; exit(0); }
template <class T> constexpr bool chmin(T& x, T const& y) { if (x > y) { x = y; return true; } return false; }
template <class T> constexpr bool chmax(T& x, T const& y) { if (x < y) { x = y; return true; } return false; }
template <class It> constexpr auto sumof(It b, It e) { return accumulate(b,e,typename iterator_traits<It>::value_type{}); }
const ll INF = (1LL<<62)-1; // ~ 4.6e18
// }}}
///////////////////////////////
//
struct UnionFind {
    int n, sz; // id : 0...n-1
    vector<int> par;

    UnionFind(int n = 0) : n(n), sz(n), par(n,-1) { }
    int root(int x) {
        assert(0 <= x && x < n);
        return par[x] < 0 ? x : par[x] = root(par[x]);
    }
    bool unite(int x, int y) {
        x = root(x), y = root(y);
        if (x == y) return false;
        sz--;
        if (par[x] < par[y]) swap(x,y);
        par[y] += par[x];
        par[x] = y;
        return true;
    }
    bool same(int x, int y) { return root(x) == root(y); }
    int size(int x) { return -par[root(x)]; }
    int size() const { return sz; }
    struct groups_t {
        vector<vector<int> > grp;
        vector<pair<int,int> > id;
    };
    groups_t groups() {
        vector<vector<int> > g(n);
        rep (i,n) if (par[i] < 0) g[i].reserve(-par[i]);
        rep (i,n) g[root(i)].push_back(i);
        vector<vector<int> > grp; grp.reserve(size());
        rep (i,n) if (g[i].size()) grp.emplace_back(move(g[i]));
        vector<pair<int,int> > id(n);
        rep (i,grp.size()) rep (j,grp[i].size()) {
            id[grp[i][j]] = make_pair(i,j);
        }
        return { grp, id };
    }
};
//
//
constexpr int dst(int v) { return v; }
template <class E, class = decltype(E().to)>
constexpr int dst(E const& e) { return e.to; }

template <class E> struct LCA {
    vector<vector<E> > const* g;
    int32_t n,lg,root;
    vector<int32_t> dep;
    vector<vector<int32_t> > anc;
    LCA() {}
    LCA(vector<vector<E> > const* g, int root = 0)
        : g(g), n(g->size()), root(root), dep(n) {
        lg = 1; while ((1<<lg) < n) lg++; // lg == 1 when n <= 1
        anc.assign(lg, vector<int32_t>(n,-1));
        dfs(root);
        rep (i,lg-1) rep (j,n) if (anc[i][j] >= 0) {
            anc[i+1][j] = anc[i][anc[i][j]];
        }
    }
    void dfs(int x, int p = -1) {
        anc[0][x] = p;
        dep[x] = (p == -1 ? 0 : dep[p]+1);
        for (auto const& e : (*g)[x]) if (dst(e) != p) dfs(dst(e),x);
    }
    int operator()(int a, int b) const { return lca(a,b); }
    int lca(int a, int b) const {
        if (dep[a] > dep[b]) swap(a,b);
        for (int dd = dep[b]-dep[a], i = 0; dd; dd >>= 1, i++) {
            if (dd&1) b = anc[i][b];
        }
        if (a == b) return a;
        for (int i = lg-1; i >= 0; i--) {
            if (anc[i][a] != anc[i][b]) a = anc[i][a], b = anc[i][b];
        }
        return anc[0][a];
    }
    int dist(int a, int b) const {
        return dep[a] + dep[b] - 2*dep[lca(a,b)];
    }
    int climb(int v, int dist) const {
        for (int i = 0; dist && v >= 0; dist >>= 1, i++) {
            if (dist&1) v = anc[i][v];
        }
        return v;
    }
};
template <class E>
auto get_lca(vector<vector<E> > const& g) { return LCA<E>(&g); }
//

template <class F> struct FixPoint : private F {
    constexpr FixPoint(F&& f) : F(forward<F>(f)) {}
    template <class... Args>
    constexpr auto operator()(Args&&... args) const {
        return F::operator()(*this, forward<Args>(args)...);
    }
};
struct MakeFixPoint {
    template <class F> constexpr auto operator|(F&& f) const {
        return FixPoint<F>(forward<F>(f));
    }
};
#define MFP MakeFixPoint()|

int f(vec<int,2> const& g, vector<int> const& v) {
    //
    dump(g);
    dump(v);
    //
    vector<int> c(g.size());
    for (int x : v) c[x]++;
    auto dp_cnt = g;
    auto dp_sum = g;
    for (auto &x : dp_cnt) for (int &y : x) y = -1;
    for (auto &x : dp_sum) for (int &y : x) y = -1;
    auto cnt = MFP [&](auto cnt, int p, int i) -> int {
        int &res = dp_cnt[p][i];
        if (res >= 0) return res;
        int x = g[p][i];
        res = c[x];
        rep (j,g[x].size()) {
            if (g[x][j] == p) continue;
            res += cnt(x,j);
        }
        return res;
    };
    auto sum = MFP [&](auto sum, int p, int i) -> int {
        int &res = dp_sum[p][i];
        if (res >= 0) return res;
        int x = g[p][i];
        res = 0;
        rep (j,g[x].size()) {
            if (g[x][j] == p) continue;
            res += sum(x,j) + cnt(x,j);
        }
        return res;
    };

    int mi = INF;
    rep (i,g.size()) {
        int s = 0;
        rep (j,g[i].size()) s += sum(i,j) + cnt(i,j);
        chmin(mi,s);
    }
    return mi;
}

int32_t main() {
    int n,m,q; cin >> n >> m >> q;
    vector<pair<int,int> > es(m);
    UnionFind uf(n);
    rep (i,m) {
        int u,v; cin >> u >> v; u--,v--;
        es[i] = mp(u,v);
        uf.unite(u,v);
    }
    auto res = uf.groups();
    auto const& id = res.id;

    dump(id);

    using Graph = vec<int,2>;
    vector<Graph> g(uf.size());
    rep (k,uf.size()) g[k].resize(res.grp[k].size());
    rep (_,m) {
        int u,v; tie(u,v) = es[_];
        int k,i,j; tie(k,i) = id[u]; tie(k,j) = id[v];
        g[k][i].eb(j);
        g[k][j].eb(i);
    }

    say("graph constructed");

    int ans = 0;
    vector<vector<int> > v(uf.size());
    vector<LCA<int> > lca(uf.size());
    rep (i,uf.size()) lca[i] = get_lca(g[i]);
    rep (_,q) {
        int a,b; cin >> a >> b; a--,b--;
        if (uf.same(a,b)) {
            int k,i,j; tie(k,i) = id[a]; tie(k,j) = id[b];
            ans += lca[k].dist(i,j);
        } else {
            int k1,i; tie(k1,i) = id[a];
            int k2,j; tie(k2,j) = id[b];
            v[k1].eb(i);
            v[k2].eb(j);
        }
    }
    //
    dump(ans);

    rep (k,uf.size()) ans += f(g[k],v[k]);

    cout << ans << endl;

}
0