結果

問題 No.1647 Travel in Mitaru city 2
ユーザー 沙耶花沙耶花
提出日時 2021-08-13 21:59:43
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 131 ms / 2,500 ms
コード長 1,759 bytes
コンパイル時間 5,007 ms
コンパイル使用メモリ 263,464 KB
実行使用メモリ 20,704 KB
最終ジャッジ日時 2024-04-21 04:49:22
合計ジャッジ時間 12,782 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 3 ms
5,376 KB
testcase_05 AC 3 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 4 ms
5,376 KB
testcase_08 AC 117 ms
19,088 KB
testcase_09 AC 110 ms
17,484 KB
testcase_10 AC 108 ms
19,272 KB
testcase_11 AC 90 ms
14,872 KB
testcase_12 AC 105 ms
18,812 KB
testcase_13 AC 43 ms
7,988 KB
testcase_14 AC 114 ms
19,760 KB
testcase_15 AC 112 ms
19,436 KB
testcase_16 AC 100 ms
18,608 KB
testcase_17 AC 104 ms
14,960 KB
testcase_18 AC 55 ms
9,448 KB
testcase_19 AC 91 ms
13,296 KB
testcase_20 AC 60 ms
10,856 KB
testcase_21 AC 63 ms
11,236 KB
testcase_22 AC 124 ms
19,864 KB
testcase_23 AC 124 ms
19,972 KB
testcase_24 AC 113 ms
17,912 KB
testcase_25 AC 120 ms
19,232 KB
testcase_26 AC 123 ms
19,972 KB
testcase_27 AC 131 ms
20,216 KB
testcase_28 AC 123 ms
20,700 KB
testcase_29 AC 119 ms
20,520 KB
testcase_30 AC 56 ms
9,956 KB
testcase_31 AC 122 ms
20,572 KB
testcase_32 AC 117 ms
18,796 KB
testcase_33 AC 110 ms
18,200 KB
testcase_34 AC 125 ms
20,576 KB
testcase_35 AC 114 ms
14,960 KB
testcase_36 AC 124 ms
20,704 KB
testcase_37 AC 129 ms
20,700 KB
testcase_38 AC 118 ms
17,520 KB
testcase_39 AC 103 ms
15,728 KB
testcase_40 AC 112 ms
17,132 KB
testcase_41 AC 110 ms
16,240 KB
testcase_42 AC 117 ms
17,388 KB
testcase_43 AC 2 ms
5,376 KB
testcase_44 AC 4 ms
5,376 KB
testcase_45 AC 45 ms
5,632 KB
testcase_46 AC 29 ms
5,376 KB
testcase_47 AC 35 ms
5,988 KB
testcase_48 AC 48 ms
5,504 KB
testcase_49 AC 36 ms
6,112 KB
testcase_50 AC 43 ms
5,728 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <stdio.h>
#include <bits/stdc++.h>
#include <atcoder/all>
using namespace atcoder;
using mint = modint998244353;
using namespace std;
#define rep(i,n) for (int i = 0; i < (n); ++i)
#define Inf 1000000001

vector<int> ans;
vector<bool> f;
bool dfs(vector<vector<pair<int,int>>> &E,int cur,int p){
	f[cur] = true;
	
	rep(i,E[cur].size()){
		int to = E[cur][i].first;
		if(to==p)continue;
		if(f[to]){
			ans.push_back(E[cur][i].second);
			return true;
		}
		if(dfs(E,to,cur)){
			ans.push_back(E[cur][i].second);
			return true;
		}
	}
	return false;
}

int main(){
	
	int H,W;
	cin>>H>>W;
	
	int N;
	cin>>N;
	
	dsu D(H+W);
	vector<int> x,y;
	rep(i,N){
		int a,b;
		scanf("%d %d",&a,&b);
		a--;b--;
		a = a,b = H+b;
		x.push_back(a);
		y.push_back(b);
		
		if(D.same(a,b)){
			for(int j=i+1;j<N;j++){
				cin>>a>>b;
			}
			break;
		}
		D.merge(a,b);
		if(i==N-1){
			cout<<-1<<endl;
			return 0;
		}
	}
	N = x.size();
	
	
	vector<int> ind;
	rep(i,H+W){
		if(D.same(i,x.back()))ind.push_back(i);
	}
	//cout<<ind.size()<<endl;
	vector<vector<pair<int,int>>> E(ind.size());
	rep(i,N){
		if(D.same(x[i],x.back())){
			int a = distance(ind.begin(),lower_bound(ind.begin(),ind.end(),x[i]));
			int b = distance(ind.begin(),lower_bound(ind.begin(),ind.end(),y[i]));
			E[a].emplace_back(b,i);
			E[b].emplace_back(a,i);
		}
	}
	f.resize(ind.size(),false);
	
	dfs(E,0,-1);
	
	dsu DD(H+W);
	vector<int> Ans;
	rep(i,ans.size()){
		Ans.push_back(ans[i]);
		int a = x[ans[i]],b = y[ans[i]];
		if(DD.same(a,b)){
			break;
		}
		DD.merge(a,b);
	}
	
	if(x[Ans[0]]==x[Ans[1]]){
		int t = Ans.back();
		Ans.pop_back();
		Ans.insert(Ans.begin(),t);
	}
	
	cout<<Ans.size()<<endl;
	rep(i,Ans.size()){
		if(i!=0)cout<<' ';
		cout<<Ans[i]+1;
	}
	cout<<endl;
	
	return 0;
}
0