結果

問題 No.416 旅行会社
ユーザー pekempeypekempey
提出日時 2016-08-26 22:36:05
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 1,440 bytes
コンパイル時間 1,638 ms
コンパイル使用メモリ 174,520 KB
実行使用メモリ 18,924 KB
最終ジャッジ日時 2024-04-25 21:33:56
合計ジャッジ時間 5,748 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 AC 2 ms
6,944 KB
testcase_02 AC 1 ms
6,944 KB
testcase_03 AC 1 ms
6,940 KB
testcase_04 AC 1 ms
6,940 KB
testcase_05 AC 2 ms
6,940 KB
testcase_06 AC 1 ms
6,944 KB
testcase_07 AC 2 ms
6,940 KB
testcase_08 AC 4 ms
6,944 KB
testcase_09 AC 14 ms
6,940 KB
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 AC 318 ms
18,816 KB
testcase_15 AC 319 ms
18,796 KB
testcase_16 AC 323 ms
18,924 KB
testcase_17 AC 323 ms
18,116 KB
testcase_18 AC 328 ms
18,916 KB
testcase_19 AC 214 ms
17,312 KB
testcase_20 AC 215 ms
17,376 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:40:22: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   40 |                 scanf("%d %d", &a, &b);
      |                 ~~~~~^~~~~~~~~~~~~~~~~
main.cpp:47:22: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   47 |                 scanf("%d %d", &c[i], &d[i]);
      |                 ~~~~~^~~~~~~~~~~~~~~~~~~~~~~

ソースコード

diff #

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

struct UnionFind {
	vector<int> parent;
	vector<vector<int>> group;

	UnionFind(int n) : parent(n), group(n) {
		for (int i = 0; i < n; i++) {
			parent[i] = i;
			group[i].push_back(i);
		}
	}

	int operator[](int x) {
		if (parent[x] == x) return x;
		return parent[x] = operator[](parent[x]);
	}

	bool merge(int x, int y) {
		x = operator[](x);
		y = operator[](y);
		if (x == y) return false;
		if (x > y) swap(x, y);
		if (group[x].size() < group[y].size()) swap(group[x], group[y]);
		for (int e : group[y]) group[x].push_back(e);
		parent[y] = x;
		return true;
	}
};

int main() {
	int n, m, q;
	cin >> n >> m >> q;

	set<pair<int, int>> g;

	for (int i = 0; i < m; i++) {
		int a, b;
		scanf("%d %d", &a, &b);
		a--; b--;
		g.emplace(minmax(a, b));
	}

	vector<int> c(q), d(q);
	for (int i = 0; i < q; i++) {
		scanf("%d %d", &c[i], &d[i]);
		c[i]--; d[i]--;
		g.erase(minmax(c[i], d[i]));
	}

	UnionFind uf(m);
	for (auto e : g) {
		uf.merge(e.first, e.second);
	}

	vector<int> ans(n);

	for (int i = 0; i < n; i++) {
		if (uf[0] == uf[i]) {
			ans[i] = -1;
		}
	}

	for (int i = q - 1; i >= 0; i--) {
		c[i] = uf[c[i]];
		d[i] = uf[d[i]];
		if (c[i] > d[i]) swap(c[i], d[i]);
		if (c[i] != d[i]) {
			if (c[i] == 0) {
				for (int e : uf.group[d[i]]) {
					ans[e] = i + 1;
				}
			}
			uf.merge(c[i], d[i]);
		}
	}

	for (int i = 1; i < n; i++) {
		printf("%d\n", ans[i]);
	}
}
0