結果

問題 No.812 Change of Class
ユーザー sinsincoscossinsincoscos
提出日時 2019-04-12 22:27:07
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 155 ms / 4,000 ms
コード長 1,563 bytes
コンパイル時間 1,148 ms
コンパイル使用メモリ 85,140 KB
実行使用メモリ 10,312 KB
最終ジャッジ日時 2024-06-12 19:12:47
合計ジャッジ時間 5,806 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 60
権限があれば一括ダウンロードができます

ソースコード

diff #
プレゼンテーションモードにする

#include <iostream>
#include <vector>
#include <map>
#include <queue>
using namespace std;
class UnionFind{
private:
vector<int> p,s;
public:
UnionFind(){}
UnionFind(int N){
p = s = vector<int>(N+1,0);
for(int i=1;i<=N;i++){
p[i] = i; s[i] = 1;
}
}
int find(int x){
if(p[x]==x) return x;
else return p[x] = find(p[x]);
}
void unite(int x,int y){
x = find(x); y = find(y);
if(x==y) return;
if(s[x]>s[y]){
p[y] = x;
s[x] += s[y];
}else{
p[x] = y;
s[y] += s[x];
}
}
bool is_same_set(int x,int y) {return find(x)==find(y);}
int size(int x) {return s[find(x)];}
};
int N,M,p,q,Q,a;
int dist[100010] = {};
vector<vector<int>> v(100010);
int bfs(int n){
for(int i=1;i<=N;i++) dist[i] = -1;
queue<int> Q;
dist[n] = 0;
Q.push(n);
int res = 0;
while(!Q.empty()){
int a = Q.front(); Q.pop();
for(auto x:v[a]){
if(dist[x]==-1){
dist[x] = dist[a]+1;
Q.push(x);
res = max(res,dist[x]);
}
}
}
return res;
}
int main(){
cin >> N >> M;
UnionFind uf(N);
for(int i=0;i<M;i++){
cin >> p >> q;
v[p].push_back(q);
v[q].push_back(p);
uf.unite(p,q);
}
cin >> Q;
for(int i=0;i<Q;i++){
cin >> a;
cout << uf.size(a)-1 << " ";
int d = bfs(a);
int ans = 0;
for(int i=0;i<20;i++){
if(d<=(1<<i)){
ans = i;
break;
}
}
cout << ans << endl;
}
}
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
0