結果

問題 No.416 旅行会社
ユーザー 193s193s
提出日時 2017-11-13 22:19:27
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 398 ms / 4,000 ms
コード長 1,850 bytes
コンパイル時間 1,482 ms
コンパイル使用メモリ 107,908 KB
実行使用メモリ 20,768 KB
最終ジャッジ日時 2023-08-21 10:13:34
合計ジャッジ時間 6,108 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 117 ms
12,120 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 4 ms
4,376 KB
testcase_09 AC 16 ms
4,428 KB
testcase_10 AC 135 ms
12,240 KB
testcase_11 AC 132 ms
12,244 KB
testcase_12 AC 135 ms
12,316 KB
testcase_13 AC 115 ms
12,264 KB
testcase_14 AC 398 ms
19,812 KB
testcase_15 AC 387 ms
19,772 KB
testcase_16 AC 384 ms
20,768 KB
testcase_17 AC 390 ms
19,912 KB
testcase_18 AC 392 ms
19,916 KB
testcase_19 AC 248 ms
17,872 KB
testcase_20 AC 251 ms
16,652 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

typedef pair<int, int> P;
#define rep(i, n) for (int i=0; i<(n); i++)
#define all(c) (c).begin(), (c).end()
#define uniq(c) c.erase(unique(all(c)), (c).end())
#define index(xs, x) (int)(lower_bound(all(xs), x) - xs.begin())
#define _1 first
#define _2 second
#define pb push_back
#define INF 1145141919
#define MOD 1000000007

int N, M, Q;
int U[100000], R[100000], T[100000];
int find(int x) {
  if (U[x] == x) return x;
  return find(U[x]);
}

void unite(int x, int y, int t) {
  x = find(x), y = find(y);
  if (x == y) return;
  if (R[x] > R[y]) swap(x, y);
  U[x] = y;
  T[x] = min(T[x], t);
  if (R[x] == R[y]) R[y]++;
}

int find(int x, int t) {
  if (T[x] > t) return x;
  return find(U[x], t);
}

signed main() {
  ios::sync_with_stdio(false); cin.tie(0);
  cin >> N >> M >> Q;
  map<P, int> mp;
  rep(i, M) {
    int a, b;
    cin >> a >> b;
    a--, b--;
    mp[P(a, b)] = 0;
  }
  rep(i, Q) {
    int a, b;
    cin >> a >> b;
    a--, b--;
    if (mp.find(P(a, b)) == mp.end()) swap(a, b);
    mp[P(a, b)] = Q-i;
  }
  rep(i, N) U[i] = i, R[i] = 1, T[i] = INF;
  vector<pair<int, P> > pp;
  for (auto p : mp) pp.pb(make_pair(p._2, P(p._1._1, p._1._2)));
  sort(all(pp));
  for (auto p : pp) unite(p._2._1, p._2._2, p._1);

  for (int i=1; i<N; i++) {
    if (find(0, Q) != find(i, Q)) {
      cout << 0 << "\n";
      continue;
    }
    int lo = -1, hi = Q+1;
    while (hi - lo > 1) {
      int mid = (lo + hi) / 2;
      if (find(0, mid) == find(i, mid)) hi = mid;
      else lo = mid;
    }
    if (hi == 0) cout << -1 << "\n";
    else cout << Q+1-hi << "\n";
  }
  return 0;
}
0