結果

問題 No.416 旅行会社
ユーザー 👑 jupirojupiro
提出日時 2020-04-15 02:13:05
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 177 ms / 4,000 ms
コード長 2,983 bytes
コンパイル時間 2,004 ms
コンパイル使用メモリ 132,816 KB
実行使用メモリ 15,552 KB
最終ジャッジ日時 2023-08-21 10:31:49
合計ジャッジ時間 5,488 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 70 ms
11,040 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 4 ms
4,376 KB
testcase_09 AC 10 ms
4,380 KB
testcase_10 AC 76 ms
11,028 KB
testcase_11 AC 81 ms
15,504 KB
testcase_12 AC 82 ms
15,552 KB
testcase_13 AC 73 ms
15,544 KB
testcase_14 AC 177 ms
12,636 KB
testcase_15 AC 174 ms
12,664 KB
testcase_16 AC 175 ms
12,660 KB
testcase_17 AC 175 ms
12,552 KB
testcase_18 AC 174 ms
12,488 KB
testcase_19 AC 116 ms
11,272 KB
testcase_20 AC 114 ms
11,020 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cstdio>
#include <iostream>
#include <string>
#include <sstream>
#include <stack>
#include <algorithm>
#include <cmath>
#include <queue>
#include <map>
#include <set>
#include <cstdlib>
#include <bitset>
#include <tuple>
#include <assert.h>
#include <deque>
#include <bitset>
#include <iomanip>
#include <limits>
#include <chrono>
#include <random>
#include <array>
#include <unordered_map>
#include <functional>
#include <complex>
#include <numeric>

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; }

constexpr long long MAX = 5100000;
constexpr long long INF = 1LL << 60;
constexpr int inf = 1 << 28;
constexpr long long mod = 1000000007LL;
//constexpr long long mod = 998244353LL;

using namespace std;
typedef unsigned long long ull;
typedef long long ll;

class UnionFind {
public:
	vector<int> Parent;
	UnionFind(int N) {
		Parent = vector<int>(N, -1);
	}
	int root(int A) {
		if (Parent[A] < 0) {
			return A;
		}
		return Parent[A] = root(Parent[A]);
	}

	long long size(int A) {
		return -(long long)Parent[root(A)];
	}

	bool connect(int A, int 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;
	}

};
vector<vector<int>> g;

void dfs(int cur, vector<int>& res, int idx) {
	if (res[cur] != 0) return;
	res[cur] = idx;
	for (auto next : g[cur]) {
		dfs(next, res, idx);
	}
}
int main()
{
	/*
	cin.tie(nullptr);
	ios::sync_with_stdio(false);
	*/

	int n, m, Q; scanf("%d %d %d", &n, &m, &Q);
	g.resize(n);
	vector<pair<int, int>> vp(m);
	for (int i = 0; i < m; i++) {
		int u, v; scanf("%d %d", &u, &v);
		u--; v--;
		if (u > v) swap(u, v);
		vp[i] = { u,v };
	}
	sort(vp.begin(), vp.end());
	vector<bool> used(m, false);
	vector<pair<int, int>> q(Q);
	for (int i = 0; i < Q; i++) {
		int u, v; scanf("%d %d", &u, &v);
		u--; v--;
		if (u > v) swap(u, v);
		q[i] = make_pair(u, v);
		int idx = lower_bound(vp.begin(), vp.end(), q[i]) - vp.begin();
		used[idx] = true;
	}
	UnionFind uni(n);
	for (int i = 0; i < m; i++) {
		if (!used[i]) {
			uni.connect(vp[i].first, vp[i].second);
			g[vp[i].first].push_back(vp[i].second);
			g[vp[i].second].push_back(vp[i].first);
		}
	}

	vector<int> res(n);
	for (int i = 1; i < n; i++) {
		if (uni.root(0) == uni.root(i)) {
			res[i] = -1;
		}
	}
	for (int i = Q - 1; i >= 0; i--) {
		if (uni.root(q[i].first) == uni.root(q[i].second)) continue;
		bool f = (uni.root(0) == uni.root(q[i].first));
		bool s = (uni.root(0) == uni.root(q[i].second));
		if (!f and s) {
			dfs(q[i].first, res, i + 1);
		}
		else if (f and !s) {
			dfs(q[i].second, res, i + 1);
		}
		uni.connect(q[i].first, q[i].second);
		g[q[i].first].push_back(q[i].second);
		g[q[i].second].push_back(q[i].first);
	}
	for (int i = 1; i < n; i++) {
		cout << res[i] << "\n";
	}
    return 0;
} 
0