結果

問題 No.812 Change of Class
ユーザー TiramisterTiramister
提出日時 2019-04-12 23:14:35
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 604 ms / 4,000 ms
コード長 2,889 bytes
コンパイル時間 1,186 ms
コンパイル使用メモリ 89,744 KB
実行使用メモリ 12,160 KB
最終ジャッジ日時 2023-09-03 14:11:39
合計ジャッジ時間 14,232 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 93 ms
8,648 KB
testcase_01 AC 15 ms
4,380 KB
testcase_02 AC 51 ms
5,964 KB
testcase_03 AC 109 ms
9,460 KB
testcase_04 AC 96 ms
8,572 KB
testcase_05 AC 585 ms
10,432 KB
testcase_06 AC 427 ms
9,640 KB
testcase_07 AC 5 ms
4,384 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 516 ms
11,000 KB
testcase_10 AC 531 ms
10,832 KB
testcase_11 AC 21 ms
7,432 KB
testcase_12 AC 310 ms
9,940 KB
testcase_13 AC 2 ms
4,384 KB
testcase_14 AC 2 ms
4,380 KB
testcase_15 AC 342 ms
8,136 KB
testcase_16 AC 20 ms
4,376 KB
testcase_17 AC 331 ms
8,652 KB
testcase_18 AC 219 ms
7,184 KB
testcase_19 AC 257 ms
7,872 KB
testcase_20 AC 604 ms
10,764 KB
testcase_21 AC 211 ms
6,992 KB
testcase_22 AC 92 ms
4,864 KB
testcase_23 AC 15 ms
4,380 KB
testcase_24 AC 24 ms
4,380 KB
testcase_25 AC 72 ms
4,648 KB
testcase_26 AC 454 ms
9,368 KB
testcase_27 AC 379 ms
8,744 KB
testcase_28 AC 243 ms
7,872 KB
testcase_29 AC 54 ms
4,424 KB
testcase_30 AC 333 ms
8,460 KB
testcase_31 AC 280 ms
7,904 KB
testcase_32 AC 114 ms
5,588 KB
testcase_33 AC 358 ms
9,248 KB
testcase_34 AC 22 ms
4,376 KB
testcase_35 AC 55 ms
4,504 KB
testcase_36 AC 25 ms
4,376 KB
testcase_37 AC 159 ms
5,836 KB
testcase_38 AC 8 ms
4,808 KB
testcase_39 AC 162 ms
5,712 KB
testcase_40 AC 35 ms
4,380 KB
testcase_41 AC 7 ms
4,648 KB
testcase_42 AC 373 ms
8,156 KB
testcase_43 AC 45 ms
6,836 KB
testcase_44 AC 240 ms
6,840 KB
testcase_45 AC 27 ms
4,376 KB
testcase_46 AC 40 ms
6,560 KB
testcase_47 AC 238 ms
6,692 KB
testcase_48 AC 244 ms
7,776 KB
testcase_49 AC 71 ms
4,412 KB
testcase_50 AC 5 ms
4,380 KB
testcase_51 AC 57 ms
4,540 KB
testcase_52 AC 111 ms
7,088 KB
testcase_53 AC 45 ms
4,380 KB
testcase_54 AC 41 ms
4,376 KB
testcase_55 AC 240 ms
9,184 KB
testcase_56 AC 294 ms
10,716 KB
testcase_57 AC 312 ms
11,200 KB
testcase_58 AC 155 ms
7,148 KB
testcase_59 AC 355 ms
12,160 KB
testcase_60 AC 1 ms
4,376 KB
testcase_61 AC 1 ms
4,380 KB
testcase_62 AC 1 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <numeric>
#include <vector>
#include <queue>
#include <tuple>
#include <limits>

template <class Cost = int>
struct Edge {
    int from, to;
    Cost cost;
    Edge(int from = -1, int to = -1, Cost cost = 1)
        : from(from), to(to), cost(cost){};

    bool operator<(const Edge<Cost>& e) const { return this->cost < e.cost; }
    bool operator>(const Edge<Cost>& e) const { return this->cost > e.cost; }
};

template <class Cost = int>
class Graph {
public:
    int size;
    std::vector<std::vector<Edge<Cost>>> path;

    explicit Graph(int N = 0) : size(N), path(size) {}
    void span(int from, int to, Cost cost = 1) {
        path[from].push_back(Edge<Cost>(from, to, cost));
    }
    std::vector<Edge<Cost>>& operator[](int v) { return path[v]; }
};

const int INF = std::numeric_limits<int>::max() / 3;

class UnionFind {
private:
    std::vector<int> par, num;

    int find(int v) {
        return (par[v] == v) ? v : (par[v] = find(par[v]));
    }

public:
    explicit UnionFind(int N) : par(N), num(N, 1) {
        std::iota(par.begin(), par.end(), 0);
    }

    void unite(int u, int v) {
        u = find(u), v = find(v);
        if (u == v) return;

        if (num[u] < num[v]) std::swap(u, v);
        num[u] += num[v];
        par[v] = u;
    }

    bool same(int u, int v) { return find(u) == find(v); }
    bool ispar(int v) { return v == find(v); }
    int size(int v) { return num[find(v)]; }
};

template <class Cost>
std::vector<Cost> dijkstra(Graph<Cost>& graph, std::vector<int> ss) {
    std::vector<Cost> dist(graph.size, INF);
    std::priority_queue<std::pair<int, Cost>, std::vector<std::pair<int, Cost>>, std::greater<std::pair<int, Cost>>> que;

    for (auto s : ss) {
        dist[s] = 0;
        que.emplace(0, s);
    }

    while (!que.empty()) {
        int v;
        Cost d;
        std::tie(d, v) = que.top();
        que.pop();
        if (d > dist[v]) continue;

        for (auto e : graph[v]) {
            if (dist[e.to] <= dist[v] + e.cost) continue;
            dist[e.to] = dist[v] + e.cost;
            que.emplace(dist[e.to], e.to);
        }
    }
    return dist;
}

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

    Graph<> graph(N);
    UnionFind uf(N);
    for (int i = 0; i < M; ++i) {
        int p, q;
        std::cin >> p >> q;
        --p, --q;

        graph.span(p, q);
        graph.span(q, p);
        uf.unite(p, q);
    }

    int Q;
    std::cin >> Q;
    for (int q = 0; q < Q; ++q) {
        int a;
        std::cin >> a;
        --a;

        std::cout << uf.size(a) - 1 << " ";
        auto dist = dijkstra<>(graph, {a});

        int max = 1;
        for (auto d : dist) {
            if (0 < d && d < INF) max = std::max(max, d);
        }

        int day;
        for (day = 0; (1 << day) < max; ++day) {}

        std::cout << day << std::endl;
    }
    return 0;
}
0