結果

問題 No.1647 Travel in Mitaru city 2
ユーザー startcppstartcpp
提出日時 2021-08-13 23:26:21
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,674 bytes
コンパイル時間 881 ms
コンパイル使用メモリ 74,044 KB
実行使用メモリ 28,708 KB
最終ジャッジ日時 2024-04-14 20:43:21
合計ジャッジ時間 12,167 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 6 ms
13,852 KB
testcase_01 AC 6 ms
15,184 KB
testcase_02 AC 4 ms
13,684 KB
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 AC 151 ms
25,788 KB
testcase_16 AC 142 ms
25,448 KB
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 AC 151 ms
26,376 KB
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 AC 154 ms
28,708 KB
testcase_29 WA -
testcase_30 AC 129 ms
23,152 KB
testcase_31 WA -
testcase_32 AC 147 ms
24,960 KB
testcase_33 WA -
testcase_34 AC 158 ms
25,744 KB
testcase_35 WA -
testcase_36 WA -
testcase_37 AC 155 ms
27,028 KB
testcase_38 AC 155 ms
23,980 KB
testcase_39 WA -
testcase_40 AC 149 ms
24,432 KB
testcase_41 WA -
testcase_42 WA -
testcase_43 AC 6 ms
13,912 KB
testcase_44 AC 5 ms
13,924 KB
testcase_45 WA -
testcase_46 AC 126 ms
21,724 KB
testcase_47 WA -
testcase_48 WA -
testcase_49 WA -
testcase_50 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

//O(H + W + N)じゃないので負けた気がするけど、union find解を実装します。
#include <iostream>
#include <vector>
#define rep(i, n) for(i = 0; i < n; i++)
using namespace std;

struct UF {
	int par[200000];
	UF() { for (int i = 0; i < 200000; i++) par[i] = i; }
	int root(int x) { if (par[x] == x) return x; return par[x] = root(par[x]); }
	bool same(int x, int y) { return root(x) == root(y); }
	void merge(int x, int y) { x = root(x); y = root(y); par[x] = y; }
};

int h, w, n;
vector<int> et[200000];
vector<int> ec[200000];
UF uf;

int parent[200000];
int color[200000];
void dfs(int p, int v) {
	parent[v] = p;
	for (int i = 0; i < et[v].size(); i++) {
		int nv = et[v][i];
		if (nv == p) continue;
		color[nv] = ec[v][i];
		dfs(v, nv);
	}
}

vector<int> get_path(int v) {
	vector<int> path;
	while (v != 0) {
		path.push_back(color[v]);
		v = parent[v];
	}
	return path;
}

void print(vector<int> ans) {
	cout << ans.size() << endl;
	for (int i = 0; i < ans.size(); i++) {
		cout << ans[i] + 1;
		if (i + 1 < ans.size()) cout << " ";
	}
	cout << endl;
}

signed main() {
	int i;
	
	cin >> h >> w >> n;
	rep(i, n) {
		int r, c;
		cin >> r >> c;
		r--; c--;
		if (uf.same(r, c + h)) {
			dfs(-1, 0);
			vector<int> p1 = get_path(r);
			vector<int> p2 = get_path(c + h);
			vector<int> ans;
			for (int j = 0; j < p1.size(); j++) ans.push_back(p1[j]);
			for (int j = p2.size() - 1; j >= 0; j--) ans.push_back(p2[j]);
			ans.push_back(i);
			print(ans);
			return 0;
		}
		else {
			uf.merge(r, c + h);
			et[r].push_back(c + h);
			et[c + h].push_back(r);
			ec[r].push_back(i);
			ec[c + h].push_back(i);
		}
	}
	
	cout << -1 << endl;
	return 0;
}
0