結果

問題 No.812 Change of Class
ユーザー paruki
提出日時 2019-04-28 20:50:44
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 190 ms / 4,000 ms
コード長 2,162 bytes
コンパイル時間 1,938 ms
コンパイル使用メモリ 178,040 KB
実行使用メモリ 10,368 KB
最終ジャッジ日時 2024-06-12 20:58:19
合計ジャッジ時間 7,221 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 60
権限があれば一括ダウンロードができます

ソースコード

diff #

#define _USE_MATH_DEFINES
#include "bits/stdc++.h"
using namespace std;
#define FOR(i,j,k) for(int (i)=(j);(i)<(int)(k);++(i))
#define rep(i,j) FOR(i,0,j)
#define each(x,y) for(auto &(x):(y))
#define mp make_pair
#define MT make_tuple
#define all(x) (x).begin(),(x).end()
#define debug(x) cout<<#x<<": "<<(x)<<endl
#define smax(x,y) (x)=max((x),(y))
#define smin(x,y) (x)=min((x),(y))
#define MEM(x,y) memset((x),(y),sizeof (x))
#define sz(x) (int)(x).size()
#define RT return
using ll = long long;
using pii = pair<int, int>;
using vi = vector<int>;
using vll = vector<ll>;

typedef int Weight;
struct Edge {
    int from, to;
    Weight wei;
    Edge(int from_, int to_, Weight wei_) :from(from_), to(to_), wei(wei_) {}
};
typedef vector<Edge> Edges;
typedef vector<Edges> Graph;
const Weight INFW = numeric_limits<Weight>::max();

vector<Weight> bfs(Graph &G, int start) {
    vector<Weight> res(G.size(), -1);
    queue<pair<int, Weight> > Q;
    Q.push(make_pair(start, 0));
    while (Q.size()) {
        auto p = Q.front();
        Q.pop();
        if (res[p.first] != -1) continue;
        res[p.first] = p.second;
        for (auto &e : G[p.first])
            Q.push(make_pair(e.to, p.second + 1));
    }
    return res;
}

void solve() {
	// Q回BFS
    // 友達同士である距離は
    // 一日で2倍になる
    int N, M;
    cin >> N >> M;
    Graph G(N);
    rep(i, M) {
        int p, q;
        cin >> p >> q;
        --p; --q;
        G[p].emplace_back(p, q, 1);
        G[q].emplace_back(q, p, 1);
    }

    int Q;
    cin >> Q;
    rep(i, Q) {
        int A;
        cin >> A;
        --A;
        vi d = bfs(G, A);
        int friends = -1;
        int max_dis = 0;
        int day = 0;
        rep(j, N) {
            if (d[j] != -1) {
                friends++;
                smax(max_dis, d[j]);
            }
        }
        rep(j, 1 << 30) {
            if ((1 << j) >= max_dis) {
                day = j;
                break;
            }
        }
        cout << friends << ' ' << day << endl;
    }
}

int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout << fixed << setprecision(15);
	solve();
	return 0;
}
0