結果

問題 No.812 Change of Class
ユーザー yuji9511yuji9511
提出日時 2019-04-13 00:12:03
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 2,364 bytes
コンパイル時間 1,481 ms
コンパイル使用メモリ 155,168 KB
実行使用メモリ 19,812 KB
最終ジャッジ日時 2023-09-03 14:21:44
合計ジャッジ時間 32,835 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 AC 133 ms
9,256 KB
testcase_08 AC 42 ms
8,224 KB
testcase_09 WA -
testcase_10 WA -
testcase_11 AC 428 ms
12,200 KB
testcase_12 WA -
testcase_13 AC 8 ms
7,692 KB
testcase_14 AC 7 ms
7,692 KB
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
testcase_33 WA -
testcase_34 WA -
testcase_35 WA -
testcase_36 WA -
testcase_37 WA -
testcase_38 AC 168 ms
9,884 KB
testcase_39 WA -
testcase_40 WA -
testcase_41 AC 146 ms
9,500 KB
testcase_42 WA -
testcase_43 WA -
testcase_44 WA -
testcase_45 WA -
testcase_46 WA -
testcase_47 WA -
testcase_48 WA -
testcase_49 WA -
testcase_50 WA -
testcase_51 WA -
testcase_52 WA -
testcase_53 WA -
testcase_54 WA -
testcase_55 AC 464 ms
15,292 KB
testcase_56 AC 511 ms
18,440 KB
testcase_57 AC 650 ms
18,248 KB
testcase_58 AC 328 ms
13,396 KB
testcase_59 AC 739 ms
19,812 KB
testcase_60 AC 6 ms
7,812 KB
testcase_61 AC 7 ms
7,724 KB
testcase_62 AC 7 ms
7,628 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> lpair;
const ll MOD = 1e9 + 7;
const ll INF = 1e18;
#define rep(i,m,n) for(ll i = (m); i < (n); i++)
#define rrep(i,m,n) for(ll i = (m); i >= (n); i--)
#define print(x) cout << (x) << endl;
#define print2(x,y) cout << (x) << " " << (y) << endl;
#define printa(x,n) for(ll i = 0; i < n; i++){ cout << (x[i]) << " ";} cout<<endl;

ll N,M;
vector<ll> tree[100010];

// Union-Find
struct UnionFind {
private:
    ll N;
    vector<ll> parent;
    vector<ll> num;

public:
    UnionFind(ll n){
        N = n;
        rep(i,0,N) parent.push_back(i);
        rep(i,0,N) num.push_back(1);
    }
    ll root(ll x){
        if(x == parent[x]) return x;
        else return parent[x] = root(parent[x]);
    }
    void unite(ll a, ll b){
        a = root(a); b = root(b);
        if(a == b) return;
        parent[a] = b;
        ll sum = num[a] + num[b];
        num[a] = sum;
        num[b] = sum;
    }
    bool same(ll a, ll b){ return root(a) == root(b);}
    ll sz(ll x){ return num[x];}
};

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    cin >> N >> M;
    UnionFind uf(100010);
    rep(i,0,M){
        ll p,q;
        cin >> p >> q;
        p--; q--;
        tree[p].push_back(q);
        tree[q].push_back(p);
        uf.unite(p,q);
    }
    ll Q;
    cin >> Q;
    while(Q--){
        ll A;
        cin >> A;
        A--;
        // print(uf.sz(uf.root(A)) - 1);
        priority_queue<lpair, vector<lpair> ,greater<lpair> > pq;
        ll dist[100010] = {};
        rep(i,0,N) dist[i] = INF;
        dist[A] = 0;
        rep(i,0,N) pq.push(make_pair(dist[i], i));
        while(!pq.empty()){
            lpair l1 = pq.top();
            pq.pop();
            for(auto &e: tree[l1.second]){
                if(dist[e] > dist[l1.second] + 1){
                    dist[e] = dist[l1.second] + 1;
                    pq.push(make_pair(dist[e], e));
                }
            }
        }
        // printa(dist, N);
        ll v = 0;
        rep(i,0,N){
            if(dist[i] != INF) v = max(v, dist[i]);
        }
        ll ans = 0;
        if(v == 0 || v == 1){
            ans = 0;
        }else if(v == 2){
            ans = 1;
        }else{
            ans = (v+1)/2;
        }
        cout << uf.sz(uf.root(A)) - 1 << " " << ans << endl;
    }






}
0