結果

問題 No.812 Change of Class
ユーザー dsm4up2cdsm4up2c
提出日時 2019-04-12 22:26:56
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 1,985 bytes
コンパイル時間 680 ms
コンパイル使用メモリ 80,828 KB
実行使用メモリ 12,568 KB
最終ジャッジ日時 2023-09-03 13:19:10
合計ジャッジ時間 7,880 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 AC 4 ms
4,620 KB
testcase_08 AC 3 ms
4,376 KB
testcase_09 WA -
testcase_10 WA -
testcase_11 AC 19 ms
7,800 KB
testcase_12 WA -
testcase_13 AC 1 ms
4,380 KB
testcase_14 AC 2 ms
4,380 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 8 ms
5,180 KB
testcase_39 WA -
testcase_40 WA -
testcase_41 AC 6 ms
4,736 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 49 ms
8,004 KB
testcase_56 AC 57 ms
8,656 KB
testcase_57 AC 61 ms
9,268 KB
testcase_58 AC 32 ms
6,184 KB
testcase_59 AC 69 ms
9,924 KB
testcase_60 AC 1 ms
4,376 KB
testcase_61 AC 1 ms
4,376 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;

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;
        if(max_dist>1){
            while(max_dist-cnt>0){
                cnt++;
                max_dist -= cnt;
            }
        }

        cout << uf.size[uf.find(a)]-1 << " " << cnt <<endl;
    }

    return 0;
}
0