結果

問題 No.812 Change of Class
ユーザー MisterMister
提出日時 2020-08-01 22:38:49
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 223 ms / 4,000 ms
コード長 2,194 bytes
コンパイル時間 851 ms
コンパイル使用メモリ 88,244 KB
実行使用メモリ 11,232 KB
最終ジャッジ日時 2023-09-22 18:42:31
合計ジャッジ時間 9,594 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 99 ms
7,800 KB
testcase_01 AC 16 ms
4,376 KB
testcase_02 AC 54 ms
5,640 KB
testcase_03 AC 115 ms
8,636 KB
testcase_04 AC 107 ms
7,948 KB
testcase_05 AC 223 ms
9,404 KB
testcase_06 AC 177 ms
8,924 KB
testcase_07 AC 4 ms
4,376 KB
testcase_08 AC 3 ms
4,376 KB
testcase_09 AC 188 ms
9,872 KB
testcase_10 AC 198 ms
9,868 KB
testcase_11 AC 15 ms
6,584 KB
testcase_12 AC 124 ms
9,172 KB
testcase_13 AC 2 ms
4,376 KB
testcase_14 AC 2 ms
4,380 KB
testcase_15 AC 139 ms
7,596 KB
testcase_16 AC 9 ms
4,376 KB
testcase_17 AC 123 ms
8,064 KB
testcase_18 AC 94 ms
6,592 KB
testcase_19 AC 106 ms
7,012 KB
testcase_20 AC 206 ms
9,644 KB
testcase_21 AC 90 ms
6,628 KB
testcase_22 AC 41 ms
4,612 KB
testcase_23 AC 7 ms
4,376 KB
testcase_24 AC 11 ms
4,380 KB
testcase_25 AC 32 ms
4,376 KB
testcase_26 AC 166 ms
8,716 KB
testcase_27 AC 148 ms
8,032 KB
testcase_28 AC 101 ms
7,108 KB
testcase_29 AC 23 ms
4,376 KB
testcase_30 AC 133 ms
7,876 KB
testcase_31 AC 114 ms
7,016 KB
testcase_32 AC 51 ms
5,220 KB
testcase_33 AC 134 ms
8,392 KB
testcase_34 AC 10 ms
4,380 KB
testcase_35 AC 25 ms
4,376 KB
testcase_36 AC 12 ms
4,380 KB
testcase_37 AC 72 ms
5,476 KB
testcase_38 AC 7 ms
4,684 KB
testcase_39 AC 74 ms
5,572 KB
testcase_40 AC 17 ms
4,376 KB
testcase_41 AC 5 ms
4,592 KB
testcase_42 AC 152 ms
7,536 KB
testcase_43 AC 23 ms
6,164 KB
testcase_44 AC 107 ms
6,256 KB
testcase_45 AC 12 ms
4,376 KB
testcase_46 AC 20 ms
5,968 KB
testcase_47 AC 105 ms
6,280 KB
testcase_48 AC 106 ms
6,952 KB
testcase_49 AC 32 ms
4,380 KB
testcase_50 AC 3 ms
4,380 KB
testcase_51 AC 28 ms
4,568 KB
testcase_52 AC 53 ms
6,480 KB
testcase_53 AC 19 ms
4,380 KB
testcase_54 AC 17 ms
4,376 KB
testcase_55 AC 80 ms
8,700 KB
testcase_56 AC 93 ms
9,792 KB
testcase_57 AC 99 ms
10,020 KB
testcase_58 AC 53 ms
6,708 KB
testcase_59 AC 113 ms
11,232 KB
testcase_60 AC 1 ms
4,380 KB
testcase_61 AC 1 ms
4,380 KB
testcase_62 AC 2 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>

template <class Cost = int>
struct Edge {
    int src, dst;
    Cost cost;
    Edge(int src = -1, int dst = -1, Cost cost = 1)
        : src(src), dst(dst), 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>
struct Graph {
    std::vector<std::vector<Edge<Cost>>> graph;

    Graph(int n = 0) : graph(n) {}

    void span(bool direct, int src, int dst, Cost cost = 1) {
        graph[src].emplace_back(src, dst, cost);
        if (!direct) graph[dst].emplace_back(dst, src, cost);
    }

    int size() const { return graph.size(); }
    void clear() { graph.clear(); }
    void resize(int n) { graph.resize(n); }

    std::vector<Edge<Cost>>& operator[](int v) { return graph[v]; }
    std::vector<Edge<Cost>> operator[](int v) const { return graph[v]; }
};

template <class Cost>
std::vector<int> bfs(const Graph<Cost>& graph, int s) {
    std::vector<Cost> dist(graph.size(), -1);
    dist[s] = 0;
    std::queue<int> que;
    que.push(s);

    while (!que.empty()) {
        int v = que.front();
        que.pop();

        for (const auto& e : graph[v]) {
            if (dist[e.dst] != -1) continue;
            dist[e.dst] = dist[v] + e.cost;
            que.push(e.dst);
        }
    }

    return dist;
}

void solve() {
    int n, m;
    std::cin >> n >> m;

    Graph<> graph(n);
    while (m--) {
        int u, v;
        std::cin >> u >> v;
        graph.span(false, --u, --v);
    }

    int q;
    std::cin >> q;
    while (q--) {
        int r;
        std::cin >> r;
        --r;

        auto ds = bfs(graph, r);

        int num = std::count_if(ds.begin(), ds.end(),
                                [](auto x) { return x != -1; }) -
                  1;
        int dmax = *std::max_element(ds.begin(), ds.end());

        int day = 0;
        while (dmax > (1 << day)) ++day;

        std::cout << num << " " << day << "\n";
    }
}

int main() {
    std::cin.tie(nullptr);
    std::ios::sync_with_stdio(false);

    solve();

    return 0;
}
0