結果

問題 No.812 Change of Class
ユーザー TiramisterTiramister
提出日時 2019-04-12 21:46:04
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 636 ms / 4,000 ms
コード長 7,277 bytes
コンパイル時間 1,347 ms
コンパイル使用メモリ 126,008 KB
実行使用メモリ 12,176 KB
最終ジャッジ日時 2024-06-12 06:47:21
合計ジャッジ時間 14,562 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 96 ms
8,504 KB
testcase_01 AC 15 ms
6,940 KB
testcase_02 AC 52 ms
6,944 KB
testcase_03 AC 111 ms
9,472 KB
testcase_04 AC 97 ms
8,704 KB
testcase_05 AC 595 ms
10,552 KB
testcase_06 AC 471 ms
9,728 KB
testcase_07 AC 5 ms
6,940 KB
testcase_08 AC 3 ms
6,940 KB
testcase_09 AC 585 ms
11,056 KB
testcase_10 AC 612 ms
11,056 KB
testcase_11 AC 22 ms
7,712 KB
testcase_12 AC 352 ms
10,144 KB
testcase_13 AC 2 ms
6,944 KB
testcase_14 AC 2 ms
6,944 KB
testcase_15 AC 375 ms
8,192 KB
testcase_16 AC 19 ms
6,944 KB
testcase_17 AC 335 ms
8,704 KB
testcase_18 AC 226 ms
7,296 KB
testcase_19 AC 275 ms
7,936 KB
testcase_20 AC 636 ms
10,760 KB
testcase_21 AC 216 ms
7,040 KB
testcase_22 AC 88 ms
6,944 KB
testcase_23 AC 16 ms
6,940 KB
testcase_24 AC 24 ms
6,940 KB
testcase_25 AC 69 ms
6,944 KB
testcase_26 AC 479 ms
9,768 KB
testcase_27 AC 386 ms
8,832 KB
testcase_28 AC 255 ms
7,876 KB
testcase_29 AC 52 ms
6,940 KB
testcase_30 AC 351 ms
8,576 KB
testcase_31 AC 294 ms
7,712 KB
testcase_32 AC 111 ms
6,948 KB
testcase_33 AC 368 ms
9,416 KB
testcase_34 AC 22 ms
6,944 KB
testcase_35 AC 54 ms
6,940 KB
testcase_36 AC 26 ms
6,940 KB
testcase_37 AC 162 ms
6,944 KB
testcase_38 AC 8 ms
6,940 KB
testcase_39 AC 162 ms
6,944 KB
testcase_40 AC 34 ms
6,940 KB
testcase_41 AC 8 ms
6,944 KB
testcase_42 AC 410 ms
8,208 KB
testcase_43 AC 47 ms
6,940 KB
testcase_44 AC 260 ms
6,940 KB
testcase_45 AC 26 ms
6,944 KB
testcase_46 AC 43 ms
6,944 KB
testcase_47 AC 264 ms
6,940 KB
testcase_48 AC 281 ms
7,808 KB
testcase_49 AC 69 ms
6,944 KB
testcase_50 AC 5 ms
6,940 KB
testcase_51 AC 57 ms
6,944 KB
testcase_52 AC 126 ms
7,040 KB
testcase_53 AC 42 ms
6,940 KB
testcase_54 AC 39 ms
6,940 KB
testcase_55 AC 225 ms
9,276 KB
testcase_56 AC 276 ms
10,916 KB
testcase_57 AC 293 ms
11,196 KB
testcase_58 AC 148 ms
7,420 KB
testcase_59 AC 331 ms
12,176 KB
testcase_60 AC 2 ms
6,940 KB
testcase_61 AC 2 ms
6,940 KB
testcase_62 AC 2 ms
6,944 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

// IO
#include <cstdio>
#include <iomanip>
#include <ios>
#include <iostream>

// algorithm
#include <algorithm>
#include <cmath>
#include <numeric>

// container
#include <vector>
#include <string>
#include <tuple>
#include <set>
#include <map>
#include <unordered_map>
#include <stack>
#include <queue>
#include <deque>

// others
#include <random>
#include <limits>
#include <functional>
#include <ctime>
#include <cassert>

// type alias
using lint = long long;
using ldouble = long double;
template <class T>
using greater_priority_queue = std::priority_queue<T, std::vector<T>, std::greater<T>>;

/* ----- class ----- */

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>
using Edges = std::vector<Edge<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]; }
};

/* ----- Output Functions for Debugging ----- */

template <class T>
std::ostream& operator<<(std::ostream& os, std::vector<T> v);
template <class T>
std::ostream& operator<<(std::ostream& os, std::set<T> v);
template <class L, class R>
std::ostream& operator<<(std::ostream& os, std::pair<L, R> p);
template <class K, class T>
std::ostream& operator<<(std::ostream& os, std::map<K, T> v);
template <class T>
std::ostream& operator<<(std::ostream& os, std::stack<T> s);
template <class T>
std::ostream& operator<<(std::ostream& os, std::queue<T> q);
template <class T>
std::ostream& operator<<(std::ostream& os, std::priority_queue<T> q);
template <class T>
std::ostream& operator<<(std::ostream& os, std::priority_queue<T, std::vector<T>, std::greater<T>> q);
template <class T>
std::ostream& operator<<(std::ostream& os, Edge<T> e);

template <class T>
std::ostream& operator<<(std::ostream& os, std::vector<T> v) {
    os << "[";
    for (auto vv : v) os << vv << ",";
    return os << "]";
}
template <class T>
std::ostream& operator<<(std::ostream& os, std::set<T> v) {
    os << "{";
    for (auto vv : v) os << vv << ",";
    return os << "}";
}
template <class L, class R>
std::ostream& operator<<(std::ostream& os, std::pair<L, R> p) {
    return os << "(" << p.first << "," << p.second << ")";
}
template <class K, class T>
std::ostream& operator<<(std::ostream& os, std::map<K, T> v) {
    os << "{";
    for (auto vv : v) os << vv << ",";
    return os << "}";
}
template <class T>
std::ostream& operator<<(std::ostream& os, std::stack<T> s) {
    os << "[";
    while (!s.empty()) {
        os << s.top() << ",";
        s.pop();
    }
    return os << "]";
}
template <class T>
std::ostream& operator<<(std::ostream& os, std::queue<T> q) {
    os << "[";
    while (!q.empty()) {
        os << q.front() << ",";
        q.pop();
    }
    return os << "]";
}
template <class T>
std::ostream& operator<<(std::ostream& os, std::priority_queue<T> q) {
    os << "{";
    while (!q.empty()) {
        os << q.top() << ",";
        q.pop();
    }
    return os << "}";
}
template <class T>
std::ostream& operator<<(std::ostream& os, std::priority_queue<T, std::vector<T>, std::greater<T>> q) {
    os << "{";
    while (!q.empty()) {
        os << q.top() << ",";
        q.pop();
    }
    return os << "}";
}
template <class T>
std::ostream& operator<<(std::ostream& os, Edge<T> e) {
    return os << "(" << e.from << "->" << e.to << ":" << e.cost << ")";
}

/* ----- Short Functions ----- */

template <class T>
inline T sq(T a) { return a * a; }

template <class T>
inline T iceil(T n, T d) { return (n + d - 1) / d; }

template <class T>
T gcd(T a, T b) {
    while (b > 0) {
        a %= b;
        std::swap(a, b);
    }
    return a;
}

template <class T, class U>
T ipow(T b, U n) {
    T ret = 1;
    while (n > 0) {
        if (n & 1) ret *= b;
        n >>= 1;
        b *= b;
    }
    return ret;
}

// 0-indexed
template <class T, class U>
inline T kthbit(T a, U k) { return (a >> k) & 1; }

template <class T, class U>
inline T mask(T a, U k) { return a & ((1 << k) - 1); }

template <class T>
std::map<T, int> compress(std::vector<T>& v) {
    std::sort(v.begin(), v.end());
    v.erase(std::unique(v.begin(), v.end()), v.end());

    std::map<T, int> rev;
    for (int i = 0; i < v.size(); ++i) rev[v[i]] = i;
    return rev;
}

template <class T>
T Vec(T v) { return v; }

template <class T, class... Ts>
auto Vec(size_t l, Ts... ts) {
    return std::vector<decltype(Vec<T>(ts...))>(l, Vec<T>(ts...));
}

/* ----- Constants ----- */

const int INF = std::numeric_limits<int>::max() / 3;
// const ll INF = std::numeric_limits<ll>::max() / 3;
// const ld PI = acos(-1);
// const ld EPS = 1e-10;
// std::mt19937 mt(int(std::time(nullptr)));

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});

        // std::cerr << dist << std::endl;

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

        int day;
        for (day = 0; day < 30; ++day) {
            if ((1 << day) >= max) break;
        }

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

    return 0;
}
0