結果

問題 No.1647 Travel in Mitaru city 2
ユーザー 👑 potato167potato167
提出日時 2021-08-13 22:55:10
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 116 ms / 2,500 ms
コード長 2,355 bytes
コンパイル時間 2,712 ms
コンパイル使用メモリ 210,580 KB
実行使用メモリ 20,404 KB
最終ジャッジ日時 2024-04-21 04:56:07
合計ジャッジ時間 10,471 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,816 KB
testcase_01 AC 2 ms
6,944 KB
testcase_02 AC 2 ms
6,944 KB
testcase_03 AC 2 ms
6,940 KB
testcase_04 AC 3 ms
6,940 KB
testcase_05 AC 2 ms
6,940 KB
testcase_06 AC 2 ms
6,940 KB
testcase_07 AC 3 ms
6,940 KB
testcase_08 AC 107 ms
19,692 KB
testcase_09 AC 100 ms
18,972 KB
testcase_10 AC 111 ms
19,464 KB
testcase_11 AC 99 ms
19,100 KB
testcase_12 AC 107 ms
19,344 KB
testcase_13 AC 105 ms
19,492 KB
testcase_14 AC 112 ms
19,996 KB
testcase_15 AC 113 ms
19,836 KB
testcase_16 AC 104 ms
19,136 KB
testcase_17 AC 105 ms
19,104 KB
testcase_18 AC 108 ms
19,556 KB
testcase_19 AC 115 ms
20,184 KB
testcase_20 AC 99 ms
19,448 KB
testcase_21 AC 111 ms
19,548 KB
testcase_22 AC 111 ms
20,404 KB
testcase_23 AC 112 ms
20,292 KB
testcase_24 AC 93 ms
19,408 KB
testcase_25 AC 102 ms
20,084 KB
testcase_26 AC 108 ms
20,292 KB
testcase_27 AC 112 ms
20,300 KB
testcase_28 AC 116 ms
20,348 KB
testcase_29 AC 114 ms
20,348 KB
testcase_30 AC 104 ms
20,184 KB
testcase_31 AC 110 ms
20,348 KB
testcase_32 AC 107 ms
19,796 KB
testcase_33 AC 108 ms
19,704 KB
testcase_34 AC 111 ms
20,272 KB
testcase_35 AC 108 ms
20,172 KB
testcase_36 AC 108 ms
20,348 KB
testcase_37 AC 112 ms
20,348 KB
testcase_38 AC 104 ms
19,660 KB
testcase_39 AC 99 ms
19,084 KB
testcase_40 AC 94 ms
19,512 KB
testcase_41 AC 95 ms
19,476 KB
testcase_42 AC 98 ms
19,464 KB
testcase_43 AC 2 ms
6,948 KB
testcase_44 AC 5 ms
8,724 KB
testcase_45 AC 69 ms
19,396 KB
testcase_46 AC 100 ms
19,280 KB
testcase_47 AC 68 ms
19,420 KB
testcase_48 AC 90 ms
19,344 KB
testcase_49 AC 66 ms
19,188 KB
testcase_50 AC 70 ms
19,312 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#define _GLIBCXX_DEBUG
using namespace std;
using std::cout;
using std::cin;
using std::endl;
using ll=long long;
using ld=long double;
ll ILL=1167167167167167167;
const int INF=2100000000;
ll mod=998244353;
#define rep(i,a) for (ll i=0;i<a;i++)
template<class T> using _pq = priority_queue<T, vector<T>, greater<T>>;
template<class T> ll LB(vector<T> &v,T a){return lower_bound(v.begin(),v.end(),a)-v.begin();}
template<class T> ll UB(vector<T> &v,T a){return upper_bound(v.begin(),v.end(),a)-v.begin();}
template<class T> bool chmin(T &a,const T &b){if(a>b){a=b;return 1;}else return 0;}
template<class T> bool chmax(T &a,const T &b){if(a<b){a=b;return 1;}else return 0;}
template<class T> void So(vector<T> &v) {sort(v.begin(),v.end());}
template<class T> void Sore(vector<T> &v) {sort(v.begin(),v.end(),[](T x,T y){return x>y;});}
void yneos(bool a){if(a) cout<<"Yes\n"; else cout<<"No\n";}


//おちこんだりもしたけれど、私はげんきです。
int main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	
	int N,H,W;
	cin>>H>>W>>N;
	vector<vector<int>> G(N+H+W);
	vector<int> x(N),y(N);
	rep(i,N){
		cin>>x[i]>>y[i];
		x[i]--,y[i]--;
		G[x[i]+N].push_back(i);
		G[y[i]+H+N].push_back(i);
		G[i].push_back(x[i]+N);
		G[i].push_back(y[i]+N+H);
	}
	vector<int> par(N+H+W,-1);
	vector<int> ans1,ans2;
	int J=0,A,B;
	rep(i,N){
		if(par[i]!=-1) continue;
		par[i]=-2;
		queue<int> q;
		q.push(i);
		while(!q.empty()){
			int a=q.front();
			q.pop();
			for(auto x:G[a]){
				if(par[x]==-1){
					par[x]=a;
					q.push(x);
				}
				else if(x==par[a]) continue;
				else{
					A=x,B=a;
					J=1;
					break;
				}
			}
		}
		if(J) break;
	}
	if(J==0){
		cout<<"-1\n";
		return 0;
	}
	while(A>=0){
		ans1.push_back(A);
		A=par[A];
	}
	while(B>=0){
		ans2.push_back(B);
		B=par[B];
	}
	reverse(ans1.begin(),ans1.end());
	reverse(ans2.begin(),ans2.end());
	int ind=0;
	while(ans1[ind]==ans2[ind]) ind++;
	vector<ll> ans;
	for(int i=ind-1;i<ans1.size();i++){
		if(ans1[i]<N) ans.push_back(ans1[i]);
	}
	for(int i=ans2.size()-1;i>=ind;i--){
		if(ans2[i]<N) ans.push_back(ans2[i]);
	}
	int start=0;
	int M=ans.size();
	cout<<M<<endl;
	assert(M%2==0&&M>=4);
	while(y[ans[start]]!=y[ans[start+1]]) start++;
	rep(i,M){
		if(i!=0) cout<<" ";
		cout<<ans[(i+start)%M]+1;
	}
	cout<<endl<<endl;
}

0