結果

問題 No.416 旅行会社
ユーザー hokutohokuto
提出日時 2021-09-05 01:30:17
言語 C++17(clang)
(17.0.6 + boost 1.83.0)
結果
RE  
実行時間 -
コード長 2,014 bytes
コンパイル時間 4,324 ms
コンパイル使用メモリ 111,940 KB
実行使用メモリ 16,188 KB
最終ジャッジ日時 2023-08-23 13:53:35
合計ジャッジ時間 13,421 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 282 ms
11,764 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 RE -
testcase_06 AC 2 ms
4,376 KB
testcase_07 RE -
testcase_08 RE -
testcase_09 AC 33 ms
4,664 KB
testcase_10 AC 319 ms
11,236 KB
testcase_11 AC 314 ms
11,388 KB
testcase_12 AC 315 ms
11,432 KB
testcase_13 AC 284 ms
11,808 KB
testcase_14 RE -
testcase_15 RE -
testcase_16 RE -
testcase_17 RE -
testcase_18 RE -
testcase_19 AC 452 ms
15,228 KB
testcase_20 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <utility>
#include <vector>
#include <string>
#include <cmath>
#include <algorithm>
#include <functional>
#include <iomanip>
#include <map>
#include <set>
#include <queue>
using namespace std;
using ll = long long int;
#define chmax(x,y)  (x) = ((x) > (y)) ? (x) : ((x) = (y));
#define chmin(x,y)  (x) = ((x) < (y)) ? (x) : ((x) = (y));

struct UnionFind
{
  vector<int> parent,rank;
  UnionFind(int n)
  {
    parent = rank = vector<int> (n);
    for(int i=0;i<n;++i)parent[i] = i;
  }  
  int root(int x)
  {
    if(x == parent[x])return x;
    else return parent[x] = root(parent[x]);
  }
  int connect(int x, int y)
  {
    x = root(x);y = root(y);
    if(x == y)return x;
    if(rank[x] < rank[y])swap(x,y);
    parent[y] = x;
    if(rank[x] == rank[y])rank[x]++;
    return x;
  }
};


int main(int argc, char const *argv[])
{
  int N,M,Q; cin >> N >> M >> Q;
  vector<int> answer(N);
  vector<int> X(N),Y(N);
  vector<int> conec[N+1];
  set<pair<int,int>> st;
  for(int i=0;i<M;++i)
  {
    int x,y; cin >> x >> y;
    st.insert({--x, --y});
  }
  for(int i=0;i<Q;++i)
  {
    cin >> X[i] >> Y[i];
    st.erase({--X[i],--Y[i]});
  }

  UnionFind uf(N+1);
  for(auto &e : st)
  {
    uf.connect(e.first,e.second);
  }
  for(int i=0;i<N;++i)
  {
    conec[uf.root(i)].push_back(i);
  }
  for(int i=0;i<N;++i)
  {
    if(uf.root(i) == 0)
    {
      answer[i] = -1;
    }
  }

  
  for(int i=Q-1;i>=0;--i)
  {
    if(uf.root(X[i]) != uf.root(Y[i]))
    {
      int x = uf.root(X[i]);
      int y = uf.root(Y[i]);
      if(x == uf.root(0))
      {
        for(auto &z : conec[y])
        {
          answer[z] = i+1;
        }
      }
      if(y == uf.root(0))
      {
        for(auto &z : conec[x])
        {
          answer[z] = i+1;
        }
      }
      if(uf.rank[x] < uf.rank[y])swap(x,y);
      uf.connect(x,y);
      for(auto &z : conec[y])conec[x].push_back(z);
      conec[y].clear();
    }
  }
  for(int i=1;i<N;++i)
  {
    std::cout << answer[i] << std::endl;
  }
}
0