結果

問題 No.416 旅行会社
ユーザー pionepione
提出日時 2020-04-06 00:34:27
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 511 ms / 4,000 ms
コード長 2,864 bytes
コンパイル時間 2,220 ms
コンパイル使用メモリ 179,576 KB
実行使用メモリ 22,924 KB
最終ジャッジ日時 2023-08-21 10:32:00
合計ジャッジ時間 8,998 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 222 ms
14,100 KB
testcase_01 AC 3 ms
5,708 KB
testcase_02 AC 3 ms
5,704 KB
testcase_03 AC 4 ms
5,776 KB
testcase_04 AC 3 ms
5,724 KB
testcase_05 AC 3 ms
5,688 KB
testcase_06 AC 3 ms
5,732 KB
testcase_07 AC 5 ms
5,708 KB
testcase_08 AC 9 ms
5,948 KB
testcase_09 AC 28 ms
6,568 KB
testcase_10 AC 240 ms
13,980 KB
testcase_11 AC 244 ms
17,228 KB
testcase_12 AC 253 ms
17,236 KB
testcase_13 AC 221 ms
17,252 KB
testcase_14 AC 499 ms
21,820 KB
testcase_15 AC 511 ms
22,608 KB
testcase_16 AC 499 ms
22,924 KB
testcase_17 AC 501 ms
22,472 KB
testcase_18 AC 504 ms
22,264 KB
testcase_19 AC 392 ms
18,212 KB
testcase_20 AC 385 ms
17,764 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

// #define int long long
#define rep(i, n) for (int i = (int)(0); i < (int)(n); ++i)
#define reps(i, n) for (int i = (int)(1); i <= (int)(n); ++i)
#define rrep(i, n) for (int i = ((int)(n)-1); i >= 0; i--)
#define rreps(i, n) for (int i = ((int)(n)); i > 0; i--)
#define irep(i, m, n) for (int i = (int)(m); i < (int)(n); ++i)
#define ireps(i, m, n) for (int i = (int)(m); i <= (int)(n); ++i)
#define SORT(v, n) sort(v, v + n);
#define REVERSE(v, n) reverse(v, v+n);
#define vsort(v) sort(v.begin(), v.end());
#define all(v) v.begin(), v.end()
#define mp(n, m) make_pair(n, m);
#define cout(d) cout<<d<<endl;
#define coutd(d) cout<<std::setprecision(10)<<d<<endl;
#define cinline(n) getline(cin,n);
#define replace_all(s, b, a) replace(s.begin(),s.end(), b, a);
#define PI (acos(-1))
#define FILL(v, n, x) fill(v, v + n, x);
#define sz(x) int(x.size())

using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vll = vector<ll>;
using vvll = vector<vll>;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using vs = vector<string>;
using vpll = vector<pair<ll, ll>>;

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

const ll INF = 1e15;
const int MOD = 1e9+7;
const ll LINF = 1e18;

class UnionFind{
private:
  vector<ll> Parent; 
 public:
  UnionFind(ll N){
    Parent = vector<ll>(N, -1);
  }
  
  ll root(ll A){
    if(Parent[A] < 0) return A;
    return Parent[A] = root(Parent[A]);
  }
  
  ll size(ll A){
    return -Parent[root(A)]; 
  }
  
  bool connect(ll A, ll B){
    A = root(A);
    B = root(B);
    if(A == B){
      return false;
    }
    if(size(A) < size(B)) swap(A, B);
    Parent[A] += Parent[B];
    Parent[B] = A;
    return true;
  }
  
  bool isSame(ll A, ll B){
    return root(A) == root(B);
  }
};

ll n,m,q; 
vll G[100005];
bool visited[100005];
ll ans[100005];
ll cnt;

void dfs(ll v){
  if(visited[v]) return;
  visited[v]=true;
  ans[v]=cnt;
  for(auto nv: G[v]){
    dfs(nv);
  }
}

signed main()
{
  cin.tie( 0 ); ios::sync_with_stdio( false );
  cin>>n>>m>>q;
  set<pll> s;
  rep(i,m){
    ll a,b; cin>>a>>b;
    a--,b--;
    s.insert({a,b});
  }
  vpll cd(q);
  rep(i,q){
    ll c,d; cin>>c>>d;
    c--,d--;
    s.erase({c,d});
    cd[i]={c,d};
  }
  UnionFind uf(n);
  for(auto e: s){
    ll a=e.first, b=e.second;
    uf.connect(a,b);
    G[a].push_back(b);
    G[b].push_back(a);
  }
  
  rep(i,n){
    if(uf.isSame(0,i)) ans[i]=-1;
  }
  rrep(i,q){
    cnt=i+1;
    ll c=cd[i].first, d=cd[i].second;
    if(uf.isSame(0,d)) swap(c,d);
    if(uf.isSame(0,c)&&!uf.isSame(0,d)){
      dfs(d);
    }
    
    uf.connect(c,d);
    G[c].push_back(d);
    G[d].push_back(c);
  }
  irep(i,1,n){
    cout<<ans[i]<<endl;
  }
}
0