結果

問題 No.416 旅行会社
ユーザー hokutohokuto
提出日時 2021-09-05 01:33:02
言語 C++17(clang)
(17.0.6 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,027 bytes
コンパイル時間 4,731 ms
コンパイル使用メモリ 112,208 KB
実行使用メモリ 16,964 KB
最終ジャッジ日時 2023-08-23 13:53:48
合計ジャッジ時間 8,523 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 283 ms
12,628 KB
testcase_01 AC 5 ms
7,152 KB
testcase_02 AC 4 ms
7,568 KB
testcase_03 AC 4 ms
7,132 KB
testcase_04 AC 4 ms
7,464 KB
testcase_05 AC 5 ms
7,592 KB
testcase_06 AC 5 ms
7,704 KB
testcase_07 AC 6 ms
7,384 KB
testcase_08 AC 11 ms
7,616 KB
testcase_09 AC 35 ms
8,360 KB
testcase_10 AC 304 ms
12,088 KB
testcase_11 AC 298 ms
12,204 KB
testcase_12 AC 301 ms
12,252 KB
testcase_13 AC 283 ms
12,608 KB
testcase_14 AC 574 ms
16,764 KB
testcase_15 AC 585 ms
16,860 KB
testcase_16 AC 583 ms
16,964 KB
testcase_17 AC 591 ms
16,856 KB
testcase_18 AC 582 ms
16,820 KB
testcase_19 AC 403 ms
15,936 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();
    }
  }
  for(int i=1;i<N;++i)
  {
    std::cout << answer[i] << std::endl;
  }
}
0