結果

問題 No.416 旅行会社
ユーザー beetbeet
提出日時 2018-11-06 17:26:34
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 576 ms / 4,000 ms
コード長 1,196 bytes
コンパイル時間 4,842 ms
コンパイル使用メモリ 211,336 KB
実行使用メモリ 23,720 KB
最終ジャッジ日時 2023-08-21 10:20:42
合計ジャッジ時間 10,084 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 283 ms
16,288 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 3 ms
4,380 KB
testcase_08 AC 7 ms
4,388 KB
testcase_09 AC 33 ms
4,656 KB
testcase_10 AC 309 ms
16,072 KB
testcase_11 AC 312 ms
16,460 KB
testcase_12 AC 303 ms
16,544 KB
testcase_13 AC 284 ms
16,648 KB
testcase_14 AC 563 ms
23,032 KB
testcase_15 AC 558 ms
23,720 KB
testcase_16 AC 548 ms
23,500 KB
testcase_17 AC 576 ms
23,176 KB
testcase_18 AC 553 ms
23,168 KB
testcase_19 AC 379 ms
16,736 KB
testcase_20 AC 375 ms
16,560 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
using Int = long long;
template<typename T1,typename T2> inline void chmin(T1 &a,T2 b){if(a>b) a=b;}
template<typename T1,typename T2> inline void chmax(T1 &a,T2 b){if(a<b) a=b;}

//INSERT ABOVE HERE
signed main(){
  int n,m,q;
  cin>>n>>m>>q;
  vector<int> a(m),b(m),c(q),d(q);
  for(int i=0;i<m;i++) cin>>a[i]>>b[i],a[i]--,b[i]--;
  for(int i=0;i<q;i++) cin>>c[i]>>d[i],c[i]--,d[i]--;
  
  vector<int> ans(n,0),p(n);
  vector<vector<int> > G(n);
  for(int i=0;i<n;i++) G[i].emplace_back(i),p[i]=i;

  auto unite=
    [&](int x,int y,int k){
      x=p[x];y=p[y];
      if(x==y) return;
      if(y==0) swap(x,y);
      if(x!=0&&G[x].size()<G[y].size()) swap(x,y);
      
      for(int z:G[y]){
        p[z]=x;
        G[x].emplace_back(z);
        if(x==0) ans[z]=k;
      }
      G[y].clear();
    };
  
  using P = pair<int, int>;
  set<P> sp;
  for(int i=0;i<q;i++) sp.emplace(c[i],d[i]);

  for(int i=0;i<m;i++){
    if(sp.count(P(a[i],b[i]))) continue;
    unite(a[i],b[i],-1);
  }

  reverse(c.begin(),c.end());
  reverse(d.begin(),d.end());
  for(int i=0;i<q;i++) unite(c[i],d[i],q-i);

  for(int i=1;i<n;i++) cout<<ans[i]<<endl;
  return 0;
}
0