結果

問題 No.812 Change of Class
ユーザー eQeeQe
提出日時 2019-04-12 22:54:33
言語 C++11
(gcc 13.3.0)
結果
AC  
実行時間 477 ms / 4,000 ms
コード長 1,894 bytes
コンパイル時間 1,397 ms
コンパイル使用メモリ 168,304 KB
実行使用メモリ 15,060 KB
最終ジャッジ日時 2024-06-12 19:46:31
合計ジャッジ時間 12,124 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 60
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define ft first
#define sc second
#define lb lower_bound
#define ub upper_bound
#define pb push_back
#define pt(sth) cout << sth << "\n"
#define chmax(a, b) {if(a<b) a=b;}
#define chmin(a, b) {if(a>b) a=b;}
#define moC(a, s, b) (a)=((a)s(b)+MOD)%MOD
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
static const ll INF=1e18;
static const ll MAX=1e5+7;
static const ll MOD=1e9+7;

class unionFind {
public:
  vector<ll> data;
  
  unionFind(ll size) {
    data.assign(size, -1);
  }
  
  bool same(ll x, ll y) {
    return find(x)==find(y);
  }
  
  void unite(ll x, ll y) {
    x=find(x);
    y=find(y);
    if(x==y) return;
    if(data[x]>data[y]) swap(x, y);
    data[x]+=data[y];
    data[y]=x;
  }
  
  ll find(ll k) {
    if(data[k]<0) return k;
    return data[k]=find(data[k]);
  }
  
  ll size(ll k) {
    return -data[find(k)];
  }
  
};

ll N, M;
vector<ll> g[MAX];
ll d[MAX];

void dijkstra(ll s) {
  ll i, u;
  ll clr[MAX];
  for(i=0; i<N; i++) {
    d[i]=INF;
    clr[i]=0;
  }
  priority_queue<P> q;
  q.push(P(0, s));
  d[s]=0;
  
  while(q.size()) {
    P f=q.top(); q.pop();
    u=f.sc;
    
    clr[u]=1;
    if(d[u]<-f.ft) continue;
    
    for(i=0; i<g[u].size(); i++) {
      ll v=g[u][i];
      if(clr[v]==0 && d[v]>d[u]+1) {
        d[v]=d[u]+1;
        q.push(P(-d[v], v));
      }
    }
  }
}

int main(void) {
  cin >> N >> M;
  ll i, j;
  
  unionFind uf(N);
  
  for(i=0; i<M; i++) {
    ll a, b;
    cin >> a >> b;
    a--; b--;
    g[a].pb(b);
    g[b].pb(a);
    
    uf.unite(a, b);
    
  }
  
  ll Q;
  cin >> Q;
  for(i=0; i<Q; i++) {
    ll a;
    cin >> a;
    a--;
    
    dijkstra(a);
    ll max=1;
    for(j=0; j<N; j++) {
      if(j==a) continue;
      if(d[j]==INF) continue;
      chmax(max, d[j]);
    }
    
    ll t=0;
    while(max>pow(2, t)) t++;
    pt(uf.size(a)-1 << " " << t);
  }
  
    
}





0