結果

問題 No.812 Change of Class
ユーザー to10to10to10toto10to10to10to
提出日時 2019-04-12 22:37:06
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,255 bytes
コンパイル時間 1,566 ms
コンパイル使用メモリ 170,200 KB
実行使用メモリ 4,504 KB
最終ジャッジ日時 2023-09-03 13:30:57
合計ジャッジ時間 6,181 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 AC 3 ms
4,380 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 AC 2 ms
4,380 KB
testcase_14 AC 2 ms
4,376 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 WA -
testcase_39 WA -
testcase_40 WA -
testcase_41 AC 6 ms
4,376 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 WA -
testcase_56 WA -
testcase_57 WA -
testcase_58 WA -
testcase_59 WA -
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 <bits/stdc++.h>
using namespace std;
typedef long long ll;

#define reps(i,a,b) for(int i=(a);i<(b);++i)
#define rep(i,n)   reps(i,0,n)
#define all(x) (x).begin(),(x).end()
#define INF (1000000000)
#define MOD (1000000007)
#define PI (acos(-1))

class UnionFind{
  public:
  //親の番号を格納する 親だった場合-1*集合のサイズ
  vector<int> Parent;
  vector<int> Weight;

  //コンストラクタ 初めはバラバラの状態
  UnionFind(int N){
    Parent = vector<int>(N,-1);//要素数N,要素の値-1で初期化
    Weight = vector<int>(N,0);//親への距離
  }

  //Aがどのグループに属しているか調べる
  int root(int A){
    if(Parent[A] < 0){
      return A;//Aが親だったらA自身を返す
    }else{
      int tmp = root(Parent[A]);
      Weight[A] += Weight[Parent[A]];
      return Parent[A] = tmp;//親に繋ぎなおす
    }
  }

  int weight(int A){
    root(A);
    return Weight[A];
  }

  //頂点数を調べる
  int size(int A){
    return -Parent[root(A)];//Aの根×-1=集合のサイズ
  }

  //AとBをくっつける
  bool connect(int A, int B, int W){
    W += Weight[A]; W -= Weight[B];
    //Aの根とBの根をくっつける
    A = root(A);
    B = root(B);
    if(A==B)return false;//既に同じグループならそのままでfalseを返す

    //Aのサイズ>=Bのサイズとする  
    if(size(A)<size(B))swap(A,B);

    Parent[A] += Parent[B];//AのサイズにBのサイズを足す
    Parent[B] = A;//Bの親をAに変更する

    Weight[B] = W;
    return true;
  }

  //AとBが同じグループかどうか調べる
  bool same(int A,int B){
    if(root(A)==root(B))return true;
    else return false;
  }

};

int main(){
  int N,M;
  cin >> N >> M;

  UnionFind Uni(N);

  rep(i,M){
    int p,q;
    cin >> p >> q;
    p--;q--;
    Uni.connect(p,q,1);
  }

  int Q;
  cin >> Q;

  /*
  rep(i,N){
    cout << Uni.weight(i) << " ";
  }
  cout << endl;
  */

  rep(i,Q){
    int A;
    cin >> A;
    A--;
    cout << Uni.size(A)-1 << " ";

    int ma = 0;
    rep(j,N){
      if(Uni.same(A,j)){
        ma = max(ma,abs(Uni.weight(A)-Uni.weight(j)));
      }
    }
    if(ma == 1)cout << 0 << endl;
    else cout << max(0,(ma+1)/2) << endl;
  }
}
0