結果
問題 | No.812 Change of Class |
ユーザー |
![]() |
提出日時 | 2019-04-12 23:19:56 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
WA
|
実行時間 | - |
コード長 | 2,395 bytes |
コンパイル時間 | 1,660 ms |
コンパイル使用メモリ | 174,172 KB |
実行使用メモリ | 6,948 KB |
最終ジャッジ日時 | 2024-06-12 19:53:49 |
合計ジャッジ時間 | 5,748 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 11 WA * 49 |
ソースコード
#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 == 0 || ma == 1){ cout << 0 << endl; continue; } rep(j,30){ if(pow(2,j) < ma && ma <= pow(2,j+1)){ int ans = j+1; cout << ans << endl; break; } } } }