結果

問題 No.1600 Many Shortest Path Problems
ユーザー tokusakuraitokusakurai
提出日時 2022-09-29 10:35:26
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 14,092 bytes
コンパイル時間 3,358 ms
コンパイル使用メモリ 234,632 KB
実行使用メモリ 48,956 KB
最終ジャッジ日時 2024-06-02 01:59:23
合計ジャッジ時間 17,122 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 1 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 371 ms
41,216 KB
testcase_05 AC 375 ms
41,344 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 WA -
testcase_09 AC 2 ms
5,376 KB
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 AC 2 ms
5,376 KB
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 AC 2 ms
5,376 KB
testcase_24 WA -
testcase_25 AC 1 ms
5,376 KB
testcase_26 AC 2 ms
5,376 KB
testcase_27 AC 2 ms
5,376 KB
testcase_28 AC 1 ms
5,376 KB
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
testcase_33 AC 2 ms
5,376 KB
testcase_34 AC 2 ms
5,376 KB
testcase_35 AC 230 ms
42,816 KB
testcase_36 AC 168 ms
42,244 KB
testcase_37 WA -
testcase_38 WA -
testcase_39 WA -
testcase_40 WA -
testcase_41 WA -
testcase_42 WA -
testcase_43 WA -
testcase_44 AC 259 ms
36,092 KB
testcase_45 WA -
testcase_46 WA -
testcase_47 WA -
testcase_48 WA -
testcase_49 AC 2 ms
5,376 KB
testcase_50 AC 2 ms
5,376 KB
testcase_51 AC 2 ms
5,376 KB
testcase_52 AC 2 ms
5,376 KB
testcase_53 AC 1 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define per(i, n) for (int i = (n)-1; i >= 0; i--)
#define rep2(i, l, r) for (int i = (l); i < (r); i++)
#define per2(i, l, r) for (int i = (r)-1; i >= (l); i--)
#define each(e, v) for (auto &e : v)
#define MM << " " <<
#define pb push_back
#define eb emplace_back
#define all(x) begin(x), end(x)
#define rall(x) rbegin(x), rend(x)
#define sz(x) (int)x.size()
using ll = long long;
using pii = pair<int, int>;
using pil = pair<int, ll>;
using pli = pair<ll, int>;
using pll = pair<ll, ll>;

template <typename T>
using minheap = priority_queue<T, vector<T>, greater<T>>;

template <typename T>
using maxheap = priority_queue<T>;

template <typename T>
bool chmax(T &x, const T &y) {
    return (x < y) ? (x = y, true) : false;
}

template <typename T>
bool chmin(T &x, const T &y) {
    return (x > y) ? (x = y, true) : false;
}

template <typename T>
int flg(T x, int i) {
    return (x >> i) & 1;
}

template <typename T>
void print(const vector<T> &v, T x = 0) {
    int n = v.size();
    for (int i = 0; i < n; i++) cout << v[i] + x << (i == n - 1 ? '\n' : ' ');
    if (v.empty()) cout << '\n';
}

template <typename T>
void printn(const vector<T> &v, T x = 0) {
    int n = v.size();
    for (int i = 0; i < n; i++) cout << v[i] + x << '\n';
}

template <typename T>
int lb(const vector<T> &v, T x) {
    return lower_bound(begin(v), end(v), x) - begin(v);
}

template <typename T>
int ub(const vector<T> &v, T x) {
    return upper_bound(begin(v), end(v), x) - begin(v);
}

template <typename T>
void rearrange(vector<T> &v) {
    sort(begin(v), end(v));
    v.erase(unique(begin(v), end(v)), end(v));
}

template <typename T>
vector<int> id_sort(const vector<T> &v, bool greater = false) {
    int n = v.size();
    vector<int> ret(n);
    iota(begin(ret), end(ret), 0);
    sort(begin(ret), end(ret), [&](int i, int j) { return greater ? v[i] > v[j] : v[i] < v[j]; });
    return ret;
}

template <typename S, typename T>
pair<S, T> operator+(const pair<S, T> &p, const pair<S, T> &q) {
    return make_pair(p.first + q.first, p.second + q.second);
}

template <typename S, typename T>
pair<S, T> operator-(const pair<S, T> &p, const pair<S, T> &q) {
    return make_pair(p.first - q.first, p.second - q.second);
}

template <typename S, typename T>
istream &operator>>(istream &is, pair<S, T> &p) {
    S a;
    T b;
    is >> a >> b;
    p = make_pair(a, b);
    return is;
}

template <typename S, typename T>
ostream &operator<<(ostream &os, const pair<S, T> &p) {
    return os << p.first << ' ' << p.second;
}

struct io_setup {
    io_setup() {
        ios_base::sync_with_stdio(false);
        cin.tie(NULL);
        cout << fixed << setprecision(15);
    }
} io_setup;

const int inf = (1 << 30) - 1;
const ll INF = (1LL << 60) - 1;
const int MOD = 1000000007;
// const int MOD = 998244353;

template <int mod>
struct Mod_Int {
    int x;

    Mod_Int() : x(0) {}

    Mod_Int(long long y) : x(y >= 0 ? y % mod : (mod - (-y) % mod) % mod) {}

    static int get_mod() { return mod; }

    Mod_Int &operator+=(const Mod_Int &p) {
        if ((x += p.x) >= mod) x -= mod;
        return *this;
    }

    Mod_Int &operator-=(const Mod_Int &p) {
        if ((x += mod - p.x) >= mod) x -= mod;
        return *this;
    }

    Mod_Int &operator*=(const Mod_Int &p) {
        x = (int)(1LL * x * p.x % mod);
        return *this;
    }

    Mod_Int &operator/=(const Mod_Int &p) {
        *this *= p.inverse();
        return *this;
    }

    Mod_Int &operator++() { return *this += Mod_Int(1); }

    Mod_Int operator++(int) {
        Mod_Int tmp = *this;
        ++*this;
        return tmp;
    }

    Mod_Int &operator--() { return *this -= Mod_Int(1); }

    Mod_Int operator--(int) {
        Mod_Int tmp = *this;
        --*this;
        return tmp;
    }

    Mod_Int operator-() const { return Mod_Int(-x); }

    Mod_Int operator+(const Mod_Int &p) const { return Mod_Int(*this) += p; }

    Mod_Int operator-(const Mod_Int &p) const { return Mod_Int(*this) -= p; }

    Mod_Int operator*(const Mod_Int &p) const { return Mod_Int(*this) *= p; }

    Mod_Int operator/(const Mod_Int &p) const { return Mod_Int(*this) /= p; }

    bool operator==(const Mod_Int &p) const { return x == p.x; }

    bool operator!=(const Mod_Int &p) const { return x != p.x; }

    Mod_Int inverse() const {
        assert(*this != Mod_Int(0));
        return pow(mod - 2);
    }

    Mod_Int pow(long long k) const {
        Mod_Int now = *this, ret = 1;
        for (; k > 0; k >>= 1, now *= now) {
            if (k & 1) ret *= now;
        }
        return ret;
    }

    friend ostream &operator<<(ostream &os, const Mod_Int &p) { return os << p.x; }

    friend istream &operator>>(istream &is, Mod_Int &p) {
        long long a;
        is >> a;
        p = Mod_Int<mod>(a);
        return is;
    }
};

using mint = Mod_Int<MOD>;

struct Union_Find_Tree {
    vector<int> data;
    const int n;
    int cnt;

    Union_Find_Tree(int n) : data(n, -1), n(n), cnt(n) {}

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

    int operator[](int i) { return root(i); }

    bool unite(int x, int y) {
        x = root(x), y = root(y);
        if (x == y) return false;
        if (data[x] > data[y]) swap(x, y);
        data[x] += data[y], data[y] = x;
        cnt--;
        return true;
    }

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

    int count() { return cnt; };

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

    void clear() {
        cnt = n;
        fill(begin(data), end(data), -1);
    }
};

template <bool directed = false>
struct Euler_Tour_Subtree {
    struct edge {
        int to, id;
        edge(int to, int id) : to(to), id(id) {}
    };

    vector<vector<edge>> es;
    vector<int> l, r; // 部分木 i は区間 [l[i],r[i]) に対応する。また、頂点 i は l[i] に対応する。
    const int n;
    int m;

    Euler_Tour_Subtree(int n) : es(n), l(n), r(n), n(n), m(0) {}

    void add_edge(int from, int to) {
        es[from].emplace_back(to, m);
        if (!directed) es[to].emplace_back(from, m);
        m++;
    }

    void _dfs(int now, int pre, int &cnt) {
        l[now] = cnt++;
        for (auto &e : es[now]) {
            if (e.to != pre) _dfs(e.to, now, cnt);
        }
        r[now] = cnt;
    }

    void build(int root = 0) {
        int cnt = 0;
        _dfs(root, -1, cnt);
    }
};

template <typename Operator_Monoid>
struct Dual_Segment_Tree {
    using H = function<Operator_Monoid(Operator_Monoid, Operator_Monoid)>;
    int n, height;
    vector<Operator_Monoid> lazy;
    const H h;
    const Operator_Monoid e2;

    Dual_Segment_Tree(int m, const H &h, const Operator_Monoid &e2) : h(h), e2(e2) {
        n = 1, height = 0;
        while (n < m) n <<= 1, height++;
        lazy.assign(2 * n, e2);
    }

    inline void eval(int i) {
        if (i < n && lazy[i] != e2) {
            lazy[2 * i] = h(lazy[2 * i], lazy[i]);
            lazy[2 * i + 1] = h(lazy[2 * i + 1], lazy[i]);
            lazy[i] = e2;
        }
    }

    inline void thrust(int i) {
        for (int j = height; j > 0; j--) eval(i >> j);
    }

    void apply(int l, int r, const Operator_Monoid &x) {
        l = max(l, 0), r = min(r, n);
        if (l >= r) return;
        l += n, r += n;
        thrust(l), thrust(r - 1);
        while (l < r) {
            if (l & 1) lazy[l] = h(lazy[l], x), l++;
            if (r & 1) r--, lazy[r] = h(lazy[r], x);
            l >>= 1, r >>= 1;
        }
    }

    Operator_Monoid get(int i) {
        thrust(i + n);
        return lazy[i + n];
    }

    Operator_Monoid operator[](int i) { return get(i); }
};

template <typename T, bool directed = false>
struct Heavy_Light_Decomposition {
    struct edge {
        int to, id;
        T cost;
        edge(int to, int id, T cost) : to(to), id(id), cost(cost) {}
    };

    vector<vector<edge>> es;
    vector<int> par, si, depth;
    vector<int> root;       // 属する連結成分の根
    vector<int> id_v, id_e; // 各頂点、各辺が一列に並べたときに何番目に相当するか (辺の番号は 1,2,...,n-1 となることに注意)
    vector<int> vs;
    const int n;
    int m;

    vector<T> d;

    Heavy_Light_Decomposition(int n) : es(n), par(n), si(n), depth(n), root(n), id_v(n), id_e(n - 1), vs(n), n(n), m(0), d(n, 0) {}

    void add_edge(int from, int to, T cost) {
        es[from].emplace_back(to, m, cost);
        if (!directed) es[to].emplace_back(from, m, cost);
        m++;
    }

    int _dfs1(int now, int pre = -1) {
        par[now] = pre;
        if (pre == -1) depth[now] = 0;
        si[now] = 1;
        for (auto &e : es[now]) {
            if (e.to != pre) {
                depth[e.to] = depth[now] + 1;
                d[e.to] = d[now] + e.cost;
                si[now] += _dfs1(e.to, now);
            }
        }
        return si[now];
    }

    void _dfs2(int now, bool st, int &s, int pre = -1) {
        root[now] = (st ? now : root[pre]);
        id_v[now] = s++;
        vs[id_v[now]] = now;
        edge heavy = {-1, -1, 0};
        int M = 0;
        for (auto &e : es[now]) {
            if (e.to == pre) continue;
            if (M < si[e.to]) M = si[e.to], heavy = e;
        }
        if (heavy.id != -1) {
            id_e[heavy.id] = s;
            _dfs2(heavy.to, false, s, now);
        }
        for (auto &e : es[now]) {
            if (e.to != pre && e.id != heavy.id) {
                id_e[e.id] = s;
                _dfs2(e.to, true, s, now);
            }
        }
    }

    void decompose(int root = 0) {
        _dfs1(root);
        int s = 0;
        _dfs2(root, true, s);
    }

    int lca(int u, int v) {
        while (root[u] != root[v]) {
            if (depth[root[u]] > depth[root[v]]) swap(u, v);
            v = par[root[v]];
        }
        if (depth[u] > depth[v]) swap(u, v);
        return u;
    }

    T dist(int u, int v) { return d[u] + d[v] - d[lca(u, v)] * 2; }

    // u の k 個前の祖先
    int ancestor(int u, int k) {
        if (k > depth[u]) return -1;
        while (k > 0) {
            int r = root[u];
            int l = depth[u] - depth[r];
            if (k <= l) return vs[id_v[r] + l - k];
            u = par[r];
            k -= l + 1;
        }
        return u;
    }

    // u から v の方向へ k 回移動
    int move(int u, int v, int k) {
        int w = lca(u, v);
        int l = depth[u] + depth[v] - depth[w] * 2;
        if (k > l) return -1;
        if (k <= depth[u] - depth[w]) return ancestor(u, k);
        return ancestor(v, l - k);
    }

    // パスに対応する区間たちを列挙
    vector<pair<int, int>> get_path(int u, int v, bool use_edge = false) {
        vector<pair<int, int>> ret;
        while (root[u] != root[v]) {
            if (depth[root[u]] > depth[root[v]]) swap(u, v);
            ret.emplace_back(id_v[root[v]], id_v[v] + 1);
            v = par[root[v]];
        }
        if (depth[u] > depth[v]) swap(u, v);
        ret.emplace_back(id_v[u] + use_edge, id_v[v] + 1);
        return ret;
    }

    // クエリが非可換の場合
    vector<pair<int, int>> get_path_noncommutative(int u, int v, bool use_edge = false) {
        vector<pair<int, int>> l, r;
        while (root[u] != root[v]) {
            if (depth[root[u]] > depth[root[v]]) {
                l.emplace_back(id_v[u] + 1, id_v[root[u]]);
                u = par[root[u]];
            } else {
                r.emplace_back(id_v[root[v]], id_v[v] + 1);
                v = par[root[v]];
            }
        }
        if (depth[u] > depth[v]) {
            l.emplace_back(id_v[u] + 1, id_v[v] + use_edge);
        } else {
            r.emplace_back(id_v[u] + use_edge, id_v[v] + 1);
        }
        reverse(begin(r), end(r));
        for (auto &e : r) l.push_back(e);
        return l;
    }
};

int main() {
    int N, M;
    cin >> N >> M;

    vector<int> u(M), v(M);
    Union_Find_Tree uf(N);
    Heavy_Light_Decomposition<mint> G1(N);
    Euler_Tour_Subtree G2(N);
    vector<mint> pw(M + 1, 1);
    rep(i, M) pw[i + 1] = pw[i] * 2;
    vector<bool> used(M, false);
    vector<int> rem;

    rep(i, M) {
        cin >> u[i] >> v[i];
        u[i]--, v[i]--;
        if (uf.unite(u[i], v[i])) {
            G1.add_edge(u[i], v[i], pw[i]);
            G2.add_edge(u[i], v[i]);
            used[i] = true;
        } else {
            rem.eb(i);
        }
    }

    G1.decompose();
    G2.build();

    auto f = [](int x, int y) { return min(x, y); };
    Dual_Segment_Tree<int> seg(N, f, inf);

    each(e, rem) {
        int w = G1.lca(u[e], v[e]);
        if (u[e] != w) {
            int s = u[e], t = G1.ancestor(s, G1.depth[s] - G1.depth[w] - 1);
            auto ps = G1.get_path(s, t);
            for (auto [l, r] : ps) seg.apply(l, r, e);
        }
        if (v[e] != w) {
            int s = v[e], t = G1.ancestor(s, G1.depth[s] - G1.depth[w] - 1);
            auto ps = G1.get_path(s, t);
            for (auto [l, r] : ps) seg.apply(l, r, e);
        }
    }

    int Q;
    cin >> Q;

    while (Q--) {
        int s, t, ng;
        cin >> s >> t >> ng;
        s--, t--, ng--;

        int x = (G1.depth[u[ng]] > G1.depth[v[ng]] ? u[ng] : v[ng]);
        int c1 = 0, c2 = 0;
        if (G2.l[x] <= G2.l[s] && G2.l[s] < G2.r[x]) c1++;
        if (G2.l[x] <= G2.l[t] && G2.l[t] < G2.r[x]) c2++;

        if (!used[ng] || c1 + c2 != 1) {
            cout << G1.dist(s, t) * 2 << '\n';
        } else {
            int id = min(seg[G1.id_v[s]], seg[G1.id_v[t]]);
            if (id == inf) {
                cout << "-1\n";
            } else {
                if (c1 == 0) swap(s, t);
                if (G2.l[x] <= G2.l[u[id]] && G2.l[u[id]] < G2.r[x]) {
                    cout << (G1.dist(s, u[id]) + pw[id] + G1.dist(v[id], t)) * 2 << '\n';
                } else {
                    cout << (G1.dist(s, v[id]) + pw[id] + G1.dist(u[id], t)) * 2 << '\n';
                }
            }
        }
    }
}
0