Halc's Library

This documentation is automatically generated by online-judge-tools/verification-helper


Project maintained by halc-git Hosted on GitHub Pages — Theme by mattgraham

:heavy_check_mark: Verify/verify-yuki/901.test.cpp

Depends on

Code

#define PROBLEM "https://yukicoder.me/problems/no/901"
#include "../../Graph/Graph.hpp"
#include "../../Template/Template.hpp"
#include "../../Tree/StaticTopTree.hpp"
void solve() {
    LL(N);
    static vector<bool> mark(N, 0);
    static vector<ll> weight(N, -1);
    struct steiner {
        using point = array<ll, 5>;
        using path = array<ll, 6>;

        static path vertex(int v) {
            if (mark[v]) {
                return {1, 0, 0, 0, 0, weight[v]};
            }
            return {0, 0, 0, 0, 0, weight[v]};
        }

        static path compress(path p, path c) {
            if (p[0] == 0 && c[0] == 0) {
                return {0, 0, 0, p[3] + c[3] + c[5], p[3] + c[3] + c[5], p[5]};
            }
            if (p[0] == 1 && c[0] == 0) {
                return {1, p[1], p[2], p[3], p[4] + c[3] + c[5], p[5]};
            }
            if (p[0] == 0 && c[0] == 1) {
                return {1, c[1], c[2], p[3] + c[3] + c[5], c[4], p[5]};
            }
            return {1,
                    p[2] + c[2] + p[4] + c[3] + c[5],
                    p[2] + c[2] + p[4] + c[3] + c[5],
                    p[3],
                    c[4],
                    p[5]};
        }

        static path add_vertex(point t, int v) {
            if (t[0] == 0) {
                if (mark[v]) {
                    return {1, 0, 0, 0, 0, weight[v]};
                }
                return {0, 0, 0, 0, 0, weight[v]};
            }
            if (t[3] == 0) {
                if (mark[v]) {
                    return {1, t[2] + t[4], t[2] + t[4], 0, 0, weight[v]};
                }
                return {1, t[1], t[2] + t[4], 0, 0, weight[v]};
            }
            return {1, t[2] + t[4], t[2] + t[4], 0, 0, weight[v]};
        }

        static point rake(point x, point y) {
            if (x[0] < y[0]) swap(x, y);
            if (x[0] == 0 && y[0] == 0) {
                return {0, 0, 0, 0, 0};
            }
            if (x[0] == 1 && y[0] == 0) {
                return x;
            }
            return {1, x[2] + y[2], x[2] + y[2], 1, x[4] + y[4]};
        }

        static point add_edge(path t) {
            if (t[0] == 0) {
                return {0, 0, 0, 0, 0};
            }
            return {1, t[1], t[2] + t[3], 0, t[5]};
        }
    };
    Graph<> gr(N);
    rep(i, N - 1) {
        LL(u, v, w);
        gr.add_edge(u, v, w);
    }
    weight[0] = 0;
    stack<ll> vert;
    vert.push(0);
    while (!vert.empty()) {
        ll pos = vert.top();
        vert.pop();
        each(e, gr[pos]) {
            if (weight[e] == -1) {
                weight[e] = e.cost;
                vert.push(e);
            }
        }
    }
    StaticTopTree<steiner> tree(gr, 0);
    LL(Q);
    rep(i, Q) {
        LL(k);
        VEC(ll, x, k);
        each(j, x) {
            mark[j] = 1;
            tree.calc(j);
        }
        out(tree.root_value()[1]);
        each(j, x) {
            mark[j] = 0;
            tree.calc(j);
        }
    }
}
int main() { solve(); }
#line 1 "Verify/verify-yuki/901.test.cpp"
#define PROBLEM "https://yukicoder.me/problems/no/901"
#line 2 "Graph/Graph.hpp"
#include <cstdint>
#include <vector>
template <class T = int32_t>
struct Edge {
    int32_t from, to;
    T cost;
    int32_t idx;
    Edge() = default;
    Edge(int32_t from, int32_t to, T cost = 1, int32_t idx = -1)
        : from(from), to(to), cost(cost), idx(idx) {}
    operator int32_t() { return to; }
    void reverse() { std::swap(from, to); }
};
template <class T = int32_t>
struct Graph {
    std::vector<std::vector<Edge<T>>> gr;
    int32_t eds = 0;
    Graph() = default;
    Graph(int32_t n) { gr.resize(n); }
    void add_edge(int32_t from, int32_t to, T cost = 1, bool directed = false) {
        gr[from].emplace_back(from, to, cost, eds);
        if (!directed) {
            gr[to].emplace_back(to, from, cost, eds);
        }
        eds++;
    }
    void add_directed_edge(int32_t from, int32_t to, T cost = 1) {
        gr[from].emplace_back(from, to, cost, eds);
        eds++;
    }
    inline std::vector<Edge<T>> &operator[](const int32_t &p) { return gr[p]; }
    int32_t size() { return gr.size(); }
};
template <class T>
Graph<T> reverse_edges(Graph<T> &gr) {
    Graph<T> ret(gr.size());
    for (int32_t i = 0; i < gr.size(); i++) {
        for (Edge<T> j : gr[i]) {
            ret[j].emplace_back(j);
            ret[j].back().reverse();
        }
    }
    return ret;
}
#line 2 "Template/Template.hpp"
#include <bits/stdc++.h>
using namespace std;

#line 8 "Template/InOut.hpp"
inline void scan() {}
inline void scan(int32_t &a) { std::cin >> a; }
inline void scan(uint32_t &a) { std::cin >> a; }
inline void scan(int64_t &a) { std::cin >> a; }
inline void scan(uint64_t &a) { std::cin >> a; }
inline void scan(char &a) { std::cin >> a; }
inline void scan(float &a) { std::cin >> a; }
inline void scan(double &a) { std::cin >> a; }
inline void scan(long double &a) { std::cin >> a; }
inline void scan(std::vector<bool> &vec) {
    for (int32_t i = 0; i < vec.size(); i++) {
        int a;
        scan(a);
        vec[i] = a;
    }
}
inline void scan(std::string &a) { std::cin >> a; }
template <class T>
inline void scan(std::vector<T> &vec);
template <class T, size_t size>
inline void scan(std::array<T, size> &vec);
template <class T, class L>
inline void scan(std::pair<T, L> &p);
template <class T, size_t size>
inline void scan(T (&vec)[size]);
template <class T>
inline void scan(std::vector<T> &vec) {
    for (auto &i : vec) scan(i);
}
template <class T>
inline void scan(std::deque<T> &vec) {
    for (auto &i : vec) scan(i);
}
template <class T, size_t size>
inline void scan(std::array<T, size> &vec) {
    for (auto &i : vec) scan(i);
}
template <class T, class L>
inline void scan(std::pair<T, L> &p) {
    scan(p.first);
    scan(p.second);
}
template <class T, size_t size>
inline void scan(T (&vec)[size]) {
    for (auto &i : vec) scan(i);
}
template <class T>
inline void scan(T &a) {
    std::cin >> a;
}
inline void in() {}
template <class Head, class... Tail>
inline void in(Head &head, Tail &...tail) {
    scan(head);
    in(tail...);
}
inline void print() { std::cout << ' '; }
inline void print(const bool &a) { std::cout << a; }
inline void print(const int32_t &a) { std::cout << a; }
inline void print(const uint32_t &a) { std::cout << a; }
inline void print(const int64_t &a) { std::cout << a; }
inline void print(const uint64_t &a) { std::cout << a; }
inline void print(const char &a) { std::cout << a; }
inline void print(const char a[]) { std::cout << a; }
inline void print(const float &a) { std::cout << a; }
inline void print(const double &a) { std::cout << a; }
inline void print(const long double &a) { std::cout << a; }
inline void print(const std::string &a) {
    for (auto &&i : a) print(i);
}
template <class T>
inline void print(const std::vector<T> &vec);
template <class T, size_t size>
inline void print(const std::array<T, size> &vec);
template <class T, class L>
inline void print(const std::pair<T, L> &p);
template <class T, size_t size>
inline void print(const T (&vec)[size]);
template <class T>
inline void print(const std::vector<T> &vec) {
    if (vec.empty()) return;
    print(vec[0]);
    for (auto i = vec.begin(); ++i != vec.end();) {
        std::cout << ' ';
        print(*i);
    }
}
template <class T>
inline void print(const std::deque<T> &vec) {
    if (vec.empty()) return;
    print(vec[0]);
    for (auto i = vec.begin(); ++i != vec.end();) {
        std::cout << ' ';
        print(*i);
    }
}
template <class T, size_t size>
inline void print(const std::array<T, size> &vec) {
    print(vec[0]);
    for (auto i = vec.begin(); ++i != vec.end();) {
        std::cout << ' ';
        print(*i);
    }
}
template <class T, class L>
inline void print(const std::pair<T, L> &p) {
    print(p.first);
    std::cout << ' ';
    print(p.second);
}
template <class T, size_t size>
inline void print(const T (&vec)[size]) {
    print(vec[0]);
    for (auto i = vec; ++i != end(vec);) {
        std::cout << ' ';
        print(*i);
    }
}
template <class T>
inline void print(const T &a) {
    std::cout << a;
}
inline void out() { std::cout << '\n'; }
template <class T>
inline void out(const T &t) {
    print(t);
    std::cout << '\n';
}
template <class Head, class... Tail>
inline void out(const Head &head, const Tail &...tail) {
    print(head);
    std::cout << ' ';
    out(tail...);
}
inline void Yes(bool i = true) { out(i ? "Yes" : "No"); }
inline void No(bool i = true) { out(i ? "No" : "Yes"); }
inline void Takahashi(bool i = true) { out(i ? "Takahashi" : "Aoki"); }
inline void Aoki(bool i = true) { out(i ? "Aoki" : "Takahashi"); }
inline void Alice(bool i = true) { out(i ? "Alice" : "Bob"); }
inline void Bob(bool i = true) { out(i ? "Bob" : "Alice"); }
inline void First(bool i = true) { out(i ? "First" : "Second"); }
inline void Second(bool i = true) { out(i ? "Second" : "First"); }
inline void Possible(bool i = true) { out(i ? "Possible" : "Impossible"); }
inline void Impossible(bool i = true) { out(i ? "Impossible" : "Possible"); }
inline void fls() { std::flush(std::cout); }
struct IOsetup {
    IOsetup() {
        std::ios::sync_with_stdio(false);
        std::cin.tie(nullptr);
        std::cout << std::fixed << std::setprecision(16);
    }
} iosetup;
#line 9 "Template/Util.hpp"
using ll = int64_t;
using ld = long double;
using ull = uint64_t;
using uint = uint32_t;
using pll = std::pair<ll, ll>;
using pii = std::pair<int32_t, int32_t>;
using vl = std::vector<ll>;
using vvl = std::vector<std::vector<ll>>;
using pdd = std::pair<ld, ld>;
using tuplis = std::array<ll, 3>;
template <class T>
using pq = std::priority_queue<T, std::vector<T>, std::greater<T>>;
constexpr ll LINF = (1LL << 62) - (1LL << 31);
constexpr int32_t INF = INT_MAX >> 1;
constexpr ll MINF = 1LL << 40;
constexpr ld DINF = std::numeric_limits<ld>::infinity();
constexpr int32_t MODD = 1000000007;
constexpr int32_t MOD = 998244353;
constexpr ld EPS = 1e-9;
constexpr ld PI = 3.1415926535897932;
const ll four[] = {0, 1, 0, -1, 0};
const ll eight[] = {0, 1, 1, 0, -1, -1, 1, -1, 0};
template <class T>
bool chmin(T &a, const T &b) {
    if (a > b) {
        a = b;
        return true;
    } else
        return false;
}
template <class T>
bool chmax(T &a, const T &b) {
    if (a < b) {
        a = b;
        return true;
    } else
        return false;
}
template <class T>
ll sum(const T &a) {
    return accumulate(std::begin(a), std::end(a), 0LL);
}
template <class T>
ld dsum(const T &a) {
    return accumulate(std::begin(a), std::end(a), 0.0L);
}
template <class T>
auto min(const T &a) {
    return *min_element(std::begin(a), std::end(a));
}
template <class T>
auto max(const T &a) {
    return *max_element(std::begin(a), std::end(a));
}
#line 1 "Template/Macro.hpp"
#define _overload3(_1, _2, _3, name, ...) name
#define _overload4(_1, _2, _3, _4, name, ...) name
#define _rep1(i, n) for (int64_t i = 0; i < (n); i++)
#define _rep2(i, a, b) for (int64_t i = (a); i < (b); i++)
#define _rep3(i, a, b, c) for (int64_t i = (a); i < (b); i += (c))
#define rep(...) _overload4(__VA_ARGS__, _rep3, _rep2, _rep1)(__VA_ARGS__)
#define _rrep1(i, n) for (int64_t i = (n) - 1; i >= 0; i--)
#define _rrep2(i, a, b) for (int64_t i = (b) - 1; i >= (a); i--)
#define rrep(...) _overload3(__VA_ARGS__, _rrep2, _rrep1)(__VA_ARGS__)
#define each(i, ...) for (auto&& i : __VA_ARGS__)
#define all(i) std::begin(i), std::end(i)
#define rall(i) std::rbegin(i), std::rend(i)
#define len(x) ((int64_t)(x).size())
#define fi first
#define se second
#define uniq(x) x.erase(unique(all(x)), std::end(x))
#define vec(type, name, ...) vector<type> name(__VA_ARGS__);
#define vv(type, name, h, ...) std::vector<std::vector<type>> name(h, std::vector<type>(__VA_ARGS__));
#define INT(...) int32_t __VA_ARGS__; in(__VA_ARGS__)
#define LL(...) int64_t __VA_ARGS__; in(__VA_ARGS__)
#define ULL(...) uint64_t __VA_ARGS__; in(__VA_ARGS__)
#define STR(...) std::string __VA_ARGS__; in(__VA_ARGS__)
#define CHR(...) char __VA_ARGS__; in(__VA_ARGS__)
#define LD(...) long double __VA_ARGS__; in(__VA_ARGS__)
#define VEC(type, name, size) std::vector<type> name(size); in(name)
#define VV(type, name, h, w) std::vector<std::vector<type>> name(h, std::vector<type>(w)); in(name)
#line 5 "Tree/StaticTopTree.hpp"

#line 7 "Tree/StaticTopTree.hpp"
template <class M>
struct StaticTopTree {
    using point = typename M::point;
    using path = typename M::path;
    struct Node {
        bool is_path;
        point point_val;
        path path_val;
        int32_t pos;
        int32_t left;
        int32_t right;
        int32_t parent;
        Node(bool pat, int32_t po = -1, int32_t lf = -1, int32_t ri = -1) {
            is_path = pat;
            pos = po;
            left = lf;
            right = ri;
            parent = -1;
        }
    };
    int32_t sz;
    std::vector<int32_t> node_pos;
    std::vector<Node> nodes;
    int32_t rt;
    template <class T>
    StaticTopTree(Graph<T> gr, int32_t root) {
        sz = gr.size();
        node_pos.resize(sz);
        _build(root, gr);
    }
    template <class T>
    int32_t _path_cluster(int32_t pos, std::vector<int32_t> &tree_sz,
                          Graph<T> &tree) {
        if (tree[pos].empty()) {
            node_pos[pos] = nodes.size();
            nodes.emplace_back(Node(1, pos));
            _calc_val(nodes.size() - 1);
            return nodes.size() - 1;
        }
        std::vector<int32_t> address;
        std::vector<int32_t> sizes;
        while (!tree[pos].empty()) {
            int32_t max_size = -1;
            int32_t next_pos = -1;
            for (int i = 0; i < tree[pos].size(); i++) {
                if (tree_sz[tree[pos][i]] > max_size) {
                    max_size = tree_sz[tree[pos][i]];
                    next_pos = i;
                }
            }
            std::swap(tree[pos][next_pos], tree[pos].back());
            next_pos = tree[pos].back();
            tree[pos].pop_back();
            tree_sz[pos] -= tree_sz[next_pos];
            sizes.emplace_back(tree_sz[pos]);
            address.emplace_back(_point_cluster(pos, tree_sz, tree));
            pos = next_pos;
        }
        address.emplace_back(_point_cluster(pos, tree_sz, tree));
        sizes.emplace_back(tree_sz[pos]);
        return _merge(address, sizes, 0, address.size(), 1);
    }
    template <class T>
    int32_t _point_cluster(int32_t pos, std::vector<int32_t> &tree_sz,
                           Graph<T> &tree) {
        if (tree[pos].empty()) {
            node_pos[pos] = nodes.size();
            nodes.emplace_back(Node(1, pos));
            _calc_val(nodes.size() - 1);
            return nodes.size() - 1;
        }
        std::vector<int32_t> address;
        std::vector<int32_t> sizes;
        for (int32_t i : tree[pos]) {
            sizes.emplace_back(tree_sz[i]);
            int32_t vert = _path_cluster(i, tree_sz, tree);
            nodes.emplace_back(Node(0, -1, vert));
            nodes[vert].parent = nodes.size() - 1;
            address.emplace_back(nodes.size() - 1);
            _calc_val(nodes.size() - 1);
        }
        int32_t vert = _merge(address, sizes, 0, address.size(), 0);
        node_pos[pos] = nodes.size();
        nodes.emplace_back(Node(1, pos, vert));
        nodes[vert].parent = nodes.size() - 1;
        _calc_val(nodes.size() - 1);
        return nodes.size() - 1;
    }
    int32_t _merge(std::vector<int32_t> &address, std::vector<int32_t> &sizes,
                   int32_t lf, int32_t ri, bool pat) {
        if (lf + 1 == ri) return address[lf];
        int32_t add = 0;
        for (int32_t i = lf; i < ri; i++) {
            add += sizes[i];
        }
        int32_t now = 0;
        int32_t bef = add + 1;
        for (int32_t i = lf; i < ri; i++) {
            now += sizes[i];
            if (now > add - now) {
                if (now + now - add > bef) i--;
                int32_t left = _merge(address, sizes, lf, i + 1, pat);
                int32_t right = _merge(address, sizes, i + 1, ri, pat);
                nodes.emplace_back(Node(pat, -1, left, right));
                nodes[left].parent = nodes.size() - 1;
                nodes[right].parent = nodes.size() - 1;
                _calc_val(nodes.size() - 1);
                return nodes.size() - 1;
            }
            bef = add - now - now;
        }
        assert(false);
    }
    void _calc_val(int32_t pos) {
        if (nodes[pos].is_path) {
            if ((nodes[pos].left == -1) && (nodes[pos].right == -1)) {
                nodes[pos].path_val = M::vertex(nodes[pos].pos);
            } else if ((nodes[pos].left != -1) && (nodes[pos].right != -1)) {
                nodes[pos].path_val =
                    M::compress(nodes[nodes[pos].left].path_val,
                                nodes[nodes[pos].right].path_val);
            } else {
                nodes[pos].path_val = M::add_vertex(
                    nodes[nodes[pos].left].point_val, nodes[pos].pos);
            }
        } else {
            if ((nodes[pos].left != -1) && (nodes[pos].right != -1)) {
                nodes[pos].point_val =
                    M::rake(nodes[nodes[pos].left].point_val,
                            nodes[nodes[pos].right].point_val);
            } else {
                nodes[pos].point_val =
                    M::add_edge(nodes[nodes[pos].left].path_val);
            }
        }
    }
    template <class T>
    void _build(int32_t root, Graph<T> &tree) {
        std::vector<int32_t> vert(sz);
        std::vector<int32_t> tree_sz(sz, -1);
        vert[0] = root;
        tree_sz[root] = 0;
        int32_t cnt = 1;
        for (int32_t i = 0; i < sz; i++) {
            for (int32_t j : tree[vert[i]]) {
                if (tree_sz[j]) {
                    tree_sz[j] = 0;
                    vert[cnt] = j;
                    cnt++;
                }
            }
        }
        for (int32_t i = sz - 1; i >= 0; i--) {
            int32_t parent = 0;
            for (int32_t j : tree[vert[i]]) {
                if (tree_sz[j] == 0) {
                    parent = -parent - 1;
                }
                if (parent >= 0) parent++;
                tree_sz[vert[i]] += tree_sz[j];
            }
            if (parent < 0) {
                std::swap(tree[vert[i]][-parent - 1], tree[vert[i]].back());
                tree[vert[i]].pop_back();
            }
            tree_sz[vert[i]]++;
        }
        rt = _path_cluster(root, tree_sz, tree);
    }
    path root_value() { return nodes[rt].path_val; }
    void calc(int32_t pos) {
        int32_t change = node_pos[pos];
        while (nodes[change].parent != -1) {
            _calc_val(change);
            change = nodes[change].parent;
        }
        _calc_val(change);
    }
    int32_t size() { return sz; }
};
#line 5 "Verify/verify-yuki/901.test.cpp"
void solve() {
    LL(N);
    static vector<bool> mark(N, 0);
    static vector<ll> weight(N, -1);
    struct steiner {
        using point = array<ll, 5>;
        using path = array<ll, 6>;

        static path vertex(int v) {
            if (mark[v]) {
                return {1, 0, 0, 0, 0, weight[v]};
            }
            return {0, 0, 0, 0, 0, weight[v]};
        }

        static path compress(path p, path c) {
            if (p[0] == 0 && c[0] == 0) {
                return {0, 0, 0, p[3] + c[3] + c[5], p[3] + c[3] + c[5], p[5]};
            }
            if (p[0] == 1 && c[0] == 0) {
                return {1, p[1], p[2], p[3], p[4] + c[3] + c[5], p[5]};
            }
            if (p[0] == 0 && c[0] == 1) {
                return {1, c[1], c[2], p[3] + c[3] + c[5], c[4], p[5]};
            }
            return {1,
                    p[2] + c[2] + p[4] + c[3] + c[5],
                    p[2] + c[2] + p[4] + c[3] + c[5],
                    p[3],
                    c[4],
                    p[5]};
        }

        static path add_vertex(point t, int v) {
            if (t[0] == 0) {
                if (mark[v]) {
                    return {1, 0, 0, 0, 0, weight[v]};
                }
                return {0, 0, 0, 0, 0, weight[v]};
            }
            if (t[3] == 0) {
                if (mark[v]) {
                    return {1, t[2] + t[4], t[2] + t[4], 0, 0, weight[v]};
                }
                return {1, t[1], t[2] + t[4], 0, 0, weight[v]};
            }
            return {1, t[2] + t[4], t[2] + t[4], 0, 0, weight[v]};
        }

        static point rake(point x, point y) {
            if (x[0] < y[0]) swap(x, y);
            if (x[0] == 0 && y[0] == 0) {
                return {0, 0, 0, 0, 0};
            }
            if (x[0] == 1 && y[0] == 0) {
                return x;
            }
            return {1, x[2] + y[2], x[2] + y[2], 1, x[4] + y[4]};
        }

        static point add_edge(path t) {
            if (t[0] == 0) {
                return {0, 0, 0, 0, 0};
            }
            return {1, t[1], t[2] + t[3], 0, t[5]};
        }
    };
    Graph<> gr(N);
    rep(i, N - 1) {
        LL(u, v, w);
        gr.add_edge(u, v, w);
    }
    weight[0] = 0;
    stack<ll> vert;
    vert.push(0);
    while (!vert.empty()) {
        ll pos = vert.top();
        vert.pop();
        each(e, gr[pos]) {
            if (weight[e] == -1) {
                weight[e] = e.cost;
                vert.push(e);
            }
        }
    }
    StaticTopTree<steiner> tree(gr, 0);
    LL(Q);
    rep(i, Q) {
        LL(k);
        VEC(ll, x, k);
        each(j, x) {
            mark[j] = 1;
            tree.calc(j);
        }
        out(tree.root_value()[1]);
        each(j, x) {
            mark[j] = 0;
            tree.calc(j);
        }
    }
}
int main() { solve(); }
Back to top page