結果

問題 No.416 旅行会社
ユーザー hokutohokuto
提出日時 2021-09-05 01:36:43
言語 C++17(clang)
(17.0.6 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,076 bytes
コンパイル時間 1,507 ms
コンパイル使用メモリ 110,904 KB
実行使用メモリ 16,940 KB
最終ジャッジ日時 2023-08-23 13:54:02
合計ジャッジ時間 8,432 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 285 ms
12,488 KB
testcase_01 AC 4 ms
7,356 KB
testcase_02 AC 5 ms
7,656 KB
testcase_03 AC 5 ms
7,552 KB
testcase_04 AC 6 ms
7,628 KB
testcase_05 AC 5 ms
7,588 KB
testcase_06 AC 4 ms
7,252 KB
testcase_07 AC 6 ms
7,540 KB
testcase_08 AC 11 ms
7,584 KB
testcase_09 AC 37 ms
8,276 KB
testcase_10 AC 313 ms
12,096 KB
testcase_11 AC 316 ms
12,300 KB
testcase_12 AC 312 ms
12,312 KB
testcase_13 AC 281 ms
12,620 KB
testcase_14 AC 660 ms
16,844 KB
testcase_15 AC 651 ms
16,940 KB
testcase_16 AC 640 ms
16,924 KB
testcase_17 AC 632 ms
16,816 KB
testcase_18 AC 652 ms
16,840 KB
testcase_19 AC 446 ms
15,956 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;
  }
};
vector<int> answer (101010,0);
vector<int> X(202020),Y(202020);
vector<int> conec[101010];
int main(int argc, char const *argv[])
{
  int N,M,Q; cin >> N >> M >> Q;
  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();
      if(uf.root(x)==y) swap(conec[x],conec[y]);
    }
  }
  for(int i=1;i<N;++i)
  {
    std::cout << answer[i] << std::endl;
  }
}
0