結果

問題 No.1647 Travel in Mitaru city 2
ユーザー 沙耶花沙耶花
提出日時 2021-08-13 21:58:03
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,759 bytes
コンパイル時間 5,104 ms
コンパイル使用メモリ 264,156 KB
実行使用メモリ 20,704 KB
最終ジャッジ日時 2024-04-14 17:47:52
合計ジャッジ時間 17,874 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 AC 2 ms
6,816 KB
testcase_02 WA -
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 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
testcase_33 WA -
testcase_34 WA -
testcase_35 WA -
testcase_36 WA -
testcase_37 WA -
testcase_38 WA -
testcase_39 WA -
testcase_40 WA -
testcase_41 WA -
testcase_42 WA -
testcase_43 AC 2 ms
6,944 KB
testcase_44 WA -
testcase_45 WA -
testcase_46 AC 26 ms
6,944 KB
testcase_47 WA -
testcase_48 WA -
testcase_49 WA -
testcase_50 WA -
権限があれば一括ダウンロードができます

ソースコード

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