結果

問題 No.416 旅行会社
ユーザー tossytossy
提出日時 2016-08-26 23:12:54
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 487 ms / 4,000 ms
コード長 2,092 bytes
コンパイル時間 1,063 ms
コンパイル使用メモリ 97,648 KB
実行使用メモリ 19,124 KB
最終ジャッジ日時 2023-08-21 09:40:58
合計ジャッジ時間 6,854 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 135 ms
11,620 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 3 ms
4,376 KB
testcase_08 AC 6 ms
4,376 KB
testcase_09 AC 25 ms
4,808 KB
testcase_10 AC 156 ms
11,808 KB
testcase_11 AC 135 ms
11,016 KB
testcase_12 AC 138 ms
11,088 KB
testcase_13 AC 113 ms
11,064 KB
testcase_14 AC 487 ms
18,744 KB
testcase_15 AC 473 ms
18,616 KB
testcase_16 AC 470 ms
19,124 KB
testcase_17 AC 473 ms
13,928 KB
testcase_18 AC 478 ms
18,632 KB
testcase_19 AC 470 ms
17,408 KB
testcase_20 AC 476 ms
17,528 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <cassert>
#include <iostream>
#include <algorithm>
#include <stack>
#include <queue>
#include <vector>
#include <set>
#include <map>
#include <bitset>
using namespace std;

#define repl(i,a,b) for(int i=(int)(a);i<(int)(b);i++)
#define rep(i,n) repl(i,0,n)
#define mp(a,b) make_pair(a,b)
#define pb(a) push_back(a)
#define all(x) (x).begin(),(x).end()
#define dbg(x) cout<<#x"="<<x<<endl
#define fi first
#define se second

#define INF 2147483600

class UnionFind {
public:
  vector<int> par, rank, sizes; // parent, depth, size(only for root)
  UnionFind(int sz) : par(sz), rank(sz, 0), sizes(sz, 1){
    rep(i,sz) par[i]=i; // initial tree is independent each other
  }
  int find(int x){
    if(par[x]==x) return x;
    else return par[x] = find(par[x]);
  }
  void unite(int x, int y){
    x=find(x);
    y=find(y);
    if(x==y) return;  // already belong to same tree
    if(rank[x] < rank[y]){  // y becomes parent node
      par[x] = y;
      sizes[y] += sizes[x];
    } else {  // x becomes parent node
      par[y]=x;
      if(rank[x]==rank[y]) rank[x]++;
      sizes[x] += sizes[y];
    }
  }
  bool same(int x, int y){
    return find(x) == find(y);
  }
  int size(int x){
    return sizes[find(x)];
  }
}; // END class UnionFind

typedef pair<int, int> P;

int main(){
  int n,m,q;
  cin>>n>>m>>q;

  set<P> brg;
  rep(i,m){
    int a,b;
    scanf("%d %d", &a, &b);
    a--;b--;
    brg.insert(mp(a,b));
  }

  vector<int> a(q), b(q);

  rep(i,q){
    scanf("%d %d", &a[i], &b[i]);
    a[i]--;b[i]--;
    brg.erase(mp(a[i],b[i]));
  }

  vector<int> res(n);

  vector<int> l(n,0), r(n,q+1);

  rep(reps, 18){
    UnionFind uf(n);
    for(P p : brg){ uf.unite(p.fi, p.se); }

    vector<vector<int> > s(m+1);
    rep(i,n-1) s[(l[i]+r[i])/2].pb(i);

    rep(i,q+1){
      if(i!=0) uf.unite(a[q-i], b[q-i]);
      for(int j : s[i]){
        if(uf.same(0,j+1)) r[j]=i;
        else l[j]=i;
      }
    }
  }

  rep(i,n-1){ if(r[i] == 0) r[i]=q+2;}

  rep(i,n-1) printf("%d\n", q-r[i]+1);

  return 0;
}
0