結果

問題 No.416 旅行会社
ユーザー SHIJOUSHIJOU
提出日時 2020-08-21 16:36:40
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 314 ms / 4,000 ms
コード長 3,217 bytes
コンパイル時間 2,216 ms
コンパイル使用メモリ 213,108 KB
実行使用メモリ 18,592 KB
最終ジャッジ日時 2023-08-21 10:37:47
合計ジャッジ時間 6,476 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 84 ms
12,256 KB
testcase_01 AC 3 ms
7,336 KB
testcase_02 AC 4 ms
7,156 KB
testcase_03 AC 4 ms
7,172 KB
testcase_04 AC 4 ms
7,104 KB
testcase_05 AC 4 ms
7,136 KB
testcase_06 AC 4 ms
7,164 KB
testcase_07 AC 4 ms
7,336 KB
testcase_08 AC 6 ms
7,532 KB
testcase_09 AC 14 ms
7,952 KB
testcase_10 AC 101 ms
12,520 KB
testcase_11 AC 105 ms
18,564 KB
testcase_12 AC 107 ms
18,592 KB
testcase_13 AC 88 ms
18,412 KB
testcase_14 AC 306 ms
17,204 KB
testcase_15 AC 314 ms
17,180 KB
testcase_16 AC 306 ms
17,168 KB
testcase_17 AC 307 ms
17,172 KB
testcase_18 AC 308 ms
17,128 KB
testcase_19 AC 208 ms
15,120 KB
testcase_20 AC 209 ms
14,904 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

//#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
#define rep(i, n) for(int i=0; i<n; ++i)
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
using namespace std;
using ll = int64_t;
using ld = long double;
using P = pair<int, int>;
using vs = vector<string>;
using vi = vector<int>;
using vvi = vector<vi>;
template<class T> using PQ = priority_queue<T>;
template<class T> using PQG = priority_queue<T, vector<T>, greater<T> >;
const int INF = 0xccccccc;
const ll LINF = 922337203685477580LL;
template<typename T1, typename T2>
inline bool chmax(T1 &a, T2 b) {return a < b && (a = b, true);}
template<typename T1, typename T2>
inline bool chmin(T1 &a, T2 b) {return a > b && (a = b, true);}
template<typename T1, typename T2>
istream &operator>>(istream &is, pair<T1, T2> &p) { return is >> p.first >> p.second;}
template<typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &p) { return os << p.first << ' ' << p.second;}

struct UnionFind {
  vector<int> par; // 親を指すvector,-par[親]は木のサイズ
  UnionFind(int n):par(n, -1) {} // uniteで親を埋め込んでいく必要あり
  int root(int x) { // 親をたどる&データの整理
    if(par[x] < 0) return x;
    return par[x] = root(par[x]);
  }
  bool unite(int x, int y) { // データの結合
    x = root(x);
    y = root(y);
    if(x == y) return false;
    if(par[x] > par[y]) swap(x, y);
    par[x] += par[y];
    par[y] = x;
    return true;
  }
  bool same(int x, int y) {return root(x) == root(y);} // 所属判定
  int size(int x) {return -par[root(x)];} // 木のサイズ
};

const int N = 1e5+10;
const int M = N<<1;

//head

int n, m, q;
set<P> ab;
P cd[M];
vi G[N];
UnionFind uft(N);
int ans[N];

void dfs(int i, vi &u, int j = -1) {
  u.emplace_back(i);
  for(int ne:G[i]) if(ne != j) {
    dfs(ne, u, i);
  }
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> n >> m >> q;
  rep(i, m) {
    P g;
    cin >> g;
    g.first--;
    g.second--;
    ab.emplace(g);
  }
  rep(i, q) cin >> cd[i];
  reverse(cd, cd+q);
//cout << endl;
  rep(i, q) {
    cd[i].first--;
    cd[i].second--;
    ab.erase(cd[i]);
  }
  for(P edge:ab) {
    //cout << edge << endl;
    if(uft.unite(edge.first, edge.second)) {
      G[edge.first].emplace_back(edge.second);
      G[edge.second].emplace_back(edge.first);
    }
  }

  vi lst;
  dfs(0, lst);
  rep(i, lst.size()) ans[lst[i]] = -1;

  rep(i, q) {
    P edge = cd[i];
    //cout << edge << endl;
    if(uft.same(edge.first, edge.second)) continue;
    if(!uft.same(edge.first, 0) and !uft.same(edge.second, 0)) {
      uft.unite(edge.first, edge.second);
      G[edge.first].emplace_back(edge.second);
      G[edge.second].emplace_back(edge.first);
    }
    else {
      lst.clear();
      if(uft.same(edge.first, 0)) {
        dfs(edge.second, lst);
      }
      else {
        dfs(edge.first, lst);
      }
      //rep(i, lst.size()) cout << lst[i] << endl;
      uft.unite(edge.first, edge.second);
      rep(j, lst.size()) if(!ans[lst[j]]) ans[lst[j]] = q-i;
      G[edge.first].emplace_back(edge.second);
      G[edge.second].emplace_back(edge.first);
    }
  }

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