結果

問題 No.812 Change of Class
ユーザー dsm4up2cdsm4up2c
提出日時 2019-04-13 04:44:38
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 195 ms / 4,000 ms
コード長 2,173 bytes
コンパイル時間 902 ms
コンパイル使用メモリ 83,856 KB
実行使用メモリ 10,384 KB
最終ジャッジ日時 2023-09-03 14:39:52
合計ジャッジ時間 7,626 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 77 ms
8,636 KB
testcase_01 AC 13 ms
4,376 KB
testcase_02 AC 43 ms
6,212 KB
testcase_03 AC 91 ms
9,788 KB
testcase_04 AC 81 ms
8,800 KB
testcase_05 AC 195 ms
9,000 KB
testcase_06 AC 175 ms
7,612 KB
testcase_07 AC 5 ms
4,740 KB
testcase_08 AC 3 ms
4,376 KB
testcase_09 AC 184 ms
9,952 KB
testcase_10 AC 184 ms
10,116 KB
testcase_11 AC 20 ms
7,904 KB
testcase_12 AC 128 ms
9,400 KB
testcase_13 AC 2 ms
4,380 KB
testcase_14 AC 2 ms
4,376 KB
testcase_15 AC 125 ms
7,644 KB
testcase_16 AC 9 ms
4,380 KB
testcase_17 AC 118 ms
7,840 KB
testcase_18 AC 89 ms
6,540 KB
testcase_19 AC 101 ms
7,060 KB
testcase_20 AC 186 ms
9,748 KB
testcase_21 AC 84 ms
6,276 KB
testcase_22 AC 39 ms
4,632 KB
testcase_23 AC 8 ms
4,380 KB
testcase_24 AC 12 ms
4,376 KB
testcase_25 AC 31 ms
4,380 KB
testcase_26 AC 155 ms
8,648 KB
testcase_27 AC 136 ms
8,084 KB
testcase_28 AC 98 ms
7,060 KB
testcase_29 AC 23 ms
4,380 KB
testcase_30 AC 125 ms
7,580 KB
testcase_31 AC 106 ms
6,712 KB
testcase_32 AC 48 ms
5,212 KB
testcase_33 AC 134 ms
8,368 KB
testcase_34 AC 10 ms
4,380 KB
testcase_35 AC 25 ms
4,380 KB
testcase_36 AC 10 ms
4,376 KB
testcase_37 AC 58 ms
5,436 KB
testcase_38 AC 8 ms
5,264 KB
testcase_39 AC 60 ms
5,432 KB
testcase_40 AC 16 ms
4,376 KB
testcase_41 AC 6 ms
4,776 KB
testcase_42 AC 122 ms
7,528 KB
testcase_43 AC 36 ms
6,792 KB
testcase_44 AC 84 ms
6,276 KB
testcase_45 AC 10 ms
4,376 KB
testcase_46 AC 32 ms
6,460 KB
testcase_47 AC 85 ms
6,536 KB
testcase_48 AC 96 ms
7,284 KB
testcase_49 AC 24 ms
4,376 KB
testcase_50 AC 2 ms
4,380 KB
testcase_51 AC 29 ms
4,568 KB
testcase_52 AC 59 ms
6,996 KB
testcase_53 AC 16 ms
4,376 KB
testcase_54 AC 14 ms
4,380 KB
testcase_55 AC 55 ms
8,300 KB
testcase_56 AC 64 ms
8,968 KB
testcase_57 AC 67 ms
9,436 KB
testcase_58 AC 36 ms
6,256 KB
testcase_59 AC 76 ms
10,384 KB
testcase_60 AC 1 ms
4,376 KB
testcase_61 AC 2 ms
4,380 KB
testcase_62 AC 1 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <string>
#include <algorithm>
#include <cstdio>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <numeric>
#include <cmath>

using namespace std;

typedef long long int ll;
typedef pair<int,int> P;

#define all(x) x.begin(),x.end()

const ll mod = 1e9+7;
const ll INF = 1e9;
const ll MAXN = 1e9;

struct union_find{
  vector<int> par,r,size;

  union_find(int n) : par(n),r(n),size(n){init(n);}

  void init(int n){
    for(int i = 0; i < n; i++) par[i] = i;
    for(int i = 0; i < n; i++) r[i] = 0;
    for(int i = 0; i < n; i++) size[i] = 1;
  }

  int find(int x){
    if(par[x] == x) return x;
    else return find(par[x]);
  }

  void unite(int x,int y){
    x = find(x);
    y = find(y);
    if(x == y) return;
    if(r[x] < r[y]){
      par[x] = y;
      size[y] += size[x];
    }else{
      par[y] = x;
      size[x] += size[y];
      if(r[x] == r[y]) r[x]++;
    }
  }

  bool same(int x,int y){
    return find(x) == find(y);
  }
};

vector<vector<int> > g;
vector<int> dist;


int main()
{
    int n,m;
    cin>>n>>m;
    g.resize(n);
    dist.resize(n);

    union_find uf(n);
    for(int i = 0; i < m; i++){
        int a,b;
        cin>>a>>b;
        a--;b--;
        g[a].push_back(b);
        g[b].push_back(a);
        uf.unite(a,b);
    }

    int q;
    cin>>q;
    for(int i = 0; i < q; i++){
        int a;
        cin>>a;
        a--;
        dist.assign(dist.size(),INF);
        queue<int> que;
        que.push(a);
        dist[a] = 0;
        while(!que.empty()){
            int x = que.front();que.pop();
            for(int i = 0; i < g[x].size(); i++){
                if(dist[x]+1<dist[g[x][i]]){
                    dist[g[x][i]] = dist[x]+1;
                    que.push(g[x][i]);
                }
            }
        }

        int max_dist = 0;
        for(int i = 0; i < n; i++){
            if(max_dist<dist[i] && dist[i]<INF) max_dist = dist[i];
        }
    
        int l = 0;
        max_dist--;
        while(max_dist>0){
            max_dist /= 2;
            l++;
        }
        cout << uf.size[uf.find(a)]-1 << " " << l <<endl;
    }

    return 0;
}
0