結果

問題 No.416 旅行会社
ユーザー nmnmnmnmnmnmnmnmnmnmnmnmnmnm
提出日時 2016-08-27 00:05:19
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 634 ms / 4,000 ms
コード長 1,996 bytes
コンパイル時間 1,139 ms
コンパイル使用メモリ 105,588 KB
実行使用メモリ 40,584 KB
最終ジャッジ日時 2023-08-21 09:43:39
合計ジャッジ時間 9,078 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 305 ms
26,440 KB
testcase_01 AC 9 ms
14,104 KB
testcase_02 AC 9 ms
13,876 KB
testcase_03 AC 9 ms
13,832 KB
testcase_04 AC 9 ms
14,000 KB
testcase_05 AC 8 ms
13,936 KB
testcase_06 AC 9 ms
13,964 KB
testcase_07 AC 10 ms
13,844 KB
testcase_08 AC 15 ms
14,204 KB
testcase_09 AC 42 ms
15,396 KB
testcase_10 AC 353 ms
26,396 KB
testcase_11 AC 350 ms
29,436 KB
testcase_12 AC 343 ms
29,420 KB
testcase_13 AC 313 ms
29,520 KB
testcase_14 AC 634 ms
39,992 KB
testcase_15 AC 634 ms
40,584 KB
testcase_16 AC 624 ms
39,860 KB
testcase_17 AC 633 ms
39,612 KB
testcase_18 AC 616 ms
39,076 KB
testcase_19 AC 437 ms
34,524 KB
testcase_20 AC 455 ms
33,048 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <algorithm>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <memory>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#include <list>

using namespace std;

typedef long long ll;

#define sz size()
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define all(c) (c).begin(), (c).end()
#define rep(i,a,b) for(ll i=(a);i<(b);++i)
#define clr(a, b) memset((a), (b) ,sizeof(a))
#define ctos(d) string(1,d)
#define print(x) cout<<#x<<" = "<<x<<endl;

#define MOD 1000000007

ll d[200005];
vector<vector<ll> > v;

void dfs(ll p, ll c) {
  d[p] = c;
  rep(i, 0, v[p].sz) {
    if (d[v[p][i]] != -1)continue;
    dfs(v[p][i], c);
  }
}

int main() {
  ll n, m, q;
  cin >> n >> m >> q;
  map<pair<ll, ll> , ll> ma;
  vector<pair<ll, ll> > vm;
  vector<pair<ll, ll> > vm1;
  vector<pair<ll, ll> > vq;
  vector<vector<ll> > v_(200005, vector<ll>());
  v = v_;
  rep(i, 0, m) {
    ll a, b;
    cin >> a >> b;
    a--; b--;
    vm.pb(mp(a, b));
  }
  rep(i, 0, q) {
    ll a, b;
    cin >> a >> b;
    a--; b--;
    vq.pb(mp(a, b));
    ma[mp(a, b)] = 1;
  }
  reverse(all(vq));
  rep(i, 0, m) {
    ll a = vm[i].fi;
    ll b = vm[i].se;
    if (ma[mp(a, b)] == 0) {
      vm1.pb(mp(a, b));
    }
  }
  rep(i, 0, vm1.sz) {
    v[vm1[i].fi].pb(vm1[i].se);
    v[vm1[i].se].pb(vm1[i].fi);
  }
  clr(d, -1);
  dfs(0, 0);
  rep(i, 0, q) {
    ll a = vq[i].fi;
    ll b = vq[i].se;
    v[a].pb(b);
    v[b].pb(a);
    if (d[a] != -1 && d[b] == -1) {
      dfs(b, i + 1);
    }
    if (d[a] == -1 && d[b] != -1) {
      dfs(a, i + 1);
    }
  }
  rep(i, 1, n) {
    if (q - d[i] + 1 == q + 1) {
      cout << -1 << endl;
    }
    else if (d[i] == -1) {
      cout << 0 << endl;
    }
    else {
      cout << q - d[i] + 1 << endl;
    }
  }
  return 0;
}
0