結果

問題 No.812 Change of Class
ユーザー dsm4up2cdsm4up2c
提出日時 2019-04-13 03:43:11
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 2,200 bytes
コンパイル時間 906 ms
コンパイル使用メモリ 80,604 KB
実行使用メモリ 12,536 KB
最終ジャッジ日時 2023-09-03 14:41:00
合計ジャッジ時間 8,500 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 100 ms
11,280 KB
testcase_01 AC 16 ms
4,424 KB
testcase_02 AC 54 ms
7,340 KB
testcase_03 AC 122 ms
12,536 KB
testcase_04 AC 105 ms
11,428 KB
testcase_05 WA -
testcase_06 WA -
testcase_07 AC 5 ms
4,612 KB
testcase_08 AC 3 ms
4,380 KB
testcase_09 WA -
testcase_10 WA -
testcase_11 AC 21 ms
7,816 KB
testcase_12 WA -
testcase_13 AC 1 ms
4,380 KB
testcase_14 AC 1 ms
4,384 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 AC 26 ms
4,416 KB
testcase_36 AC 9 ms
4,376 KB
testcase_37 AC 65 ms
5,360 KB
testcase_38 AC 8 ms
5,208 KB
testcase_39 AC 68 ms
5,496 KB
testcase_40 AC 17 ms
4,376 KB
testcase_41 AC 6 ms
4,768 KB
testcase_42 AC 151 ms
7,512 KB
testcase_43 AC 38 ms
6,888 KB
testcase_44 AC 99 ms
6,328 KB
testcase_45 AC 10 ms
4,380 KB
testcase_46 AC 36 ms
6,528 KB
testcase_47 AC 100 ms
6,552 KB
testcase_48 AC 118 ms
7,264 KB
testcase_49 AC 24 ms
4,380 KB
testcase_50 AC 2 ms
4,376 KB
testcase_51 AC 30 ms
4,736 KB
testcase_52 AC 68 ms
6,760 KB
testcase_53 AC 17 ms
4,380 KB
testcase_54 AC 13 ms
4,384 KB
testcase_55 AC 50 ms
8,032 KB
testcase_56 AC 58 ms
8,720 KB
testcase_57 AC 61 ms
9,292 KB
testcase_58 AC 33 ms
6,340 KB
testcase_59 AC 69 ms
9,820 KB
testcase_60 AC 1 ms
4,380 KB
testcase_61 AC 2 ms
4,380 KB
testcase_62 AC 1 ms
4,384 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;

void dfs(int x,int len){
    dist[x] = len;
    for(int i = 0; i < g[x].size(); i++){
        if(dist[g[x][i]]<0) dfs(g[x][i],len+1);
    }
}


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(),-1);
        dfs(a,0);
        int max_dist = 0;
        for(int j = 0; j < n; j++){
            max_dist = max(max_dist,dist[j]);
        }
        // int cnt=0,res=0;
        // if(max_dist>1){
        //     while(max_dist-cnt>0){
        //         res++;
        //         if(cnt==0) cnt++;
        //         else cnt *= 2;
        //         max_dist -= cnt;
        //     }
        // }

        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