結果

問題 No.1647 Travel in Mitaru city 2
ユーザー kotatsugamekotatsugame
提出日時 2021-08-13 21:53:15
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 169 ms / 2,500 ms
コード長 1,032 bytes
コンパイル時間 839 ms
コンパイル使用メモリ 75,932 KB
実行使用メモリ 35,672 KB
最終ジャッジ日時 2024-04-21 04:48:38
合計ジャッジ時間 10,224 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 9 ms
12,800 KB
testcase_01 AC 9 ms
12,672 KB
testcase_02 AC 8 ms
12,800 KB
testcase_03 AC 8 ms
12,672 KB
testcase_04 AC 9 ms
12,800 KB
testcase_05 AC 8 ms
12,672 KB
testcase_06 AC 7 ms
12,672 KB
testcase_07 AC 9 ms
13,056 KB
testcase_08 AC 152 ms
33,112 KB
testcase_09 AC 131 ms
30,468 KB
testcase_10 AC 140 ms
33,312 KB
testcase_11 AC 132 ms
30,776 KB
testcase_12 AC 112 ms
24,448 KB
testcase_13 AC 138 ms
33,064 KB
testcase_14 AC 153 ms
34,016 KB
testcase_15 AC 161 ms
33,532 KB
testcase_16 AC 143 ms
32,148 KB
testcase_17 AC 105 ms
22,912 KB
testcase_18 AC 129 ms
27,100 KB
testcase_19 AC 114 ms
24,704 KB
testcase_20 AC 132 ms
26,996 KB
testcase_21 AC 122 ms
26,708 KB
testcase_22 AC 168 ms
34,264 KB
testcase_23 AC 156 ms
34,520 KB
testcase_24 AC 134 ms
27,996 KB
testcase_25 AC 146 ms
30,812 KB
testcase_26 AC 160 ms
34,516 KB
testcase_27 AC 165 ms
34,904 KB
testcase_28 AC 165 ms
35,672 KB
testcase_29 AC 158 ms
35,416 KB
testcase_30 AC 132 ms
27,048 KB
testcase_31 AC 164 ms
35,540 KB
testcase_32 AC 151 ms
31,064 KB
testcase_33 AC 158 ms
33,116 KB
testcase_34 AC 159 ms
35,672 KB
testcase_35 AC 156 ms
32,216 KB
testcase_36 AC 168 ms
35,672 KB
testcase_37 AC 169 ms
35,668 KB
testcase_38 AC 147 ms
29,416 KB
testcase_39 AC 129 ms
25,668 KB
testcase_40 AC 146 ms
29,164 KB
testcase_41 AC 137 ms
27,124 KB
testcase_42 AC 156 ms
29,292 KB
testcase_43 AC 9 ms
12,672 KB
testcase_44 AC 9 ms
13,568 KB
testcase_45 AC 110 ms
21,760 KB
testcase_46 AC 137 ms
21,760 KB
testcase_47 AC 105 ms
21,760 KB
testcase_48 AC 123 ms
21,888 KB
testcase_49 AC 108 ms
21,760 KB
testcase_50 AC 109 ms
21,760 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp:30:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
   30 | main()
      | ^~~~

ソースコード

diff #

#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
int H,W,N;
int X[1<<17],Y[1<<17];
vector<int>G[3<<17];
vector<int>tmp;
vector<int>ans;
int vis[3<<17];
bool dfs(int u,int p)
{
	vis[u]=tmp.size();
	tmp.push_back(u);
	for(int v:G[u])if(v!=p)
	{
		if(vis[v]==-1)
		{
			if(dfs(v,u))return true;
		}
		else
		{
			ans=vector<int>(tmp.begin()+vis[v],tmp.end());
			return true;
		}
	}
	tmp.pop_back();
	return false;
}
main()
{
	cin>>H>>W>>N;
	for(int i=0;i<N;i++)
	{
		cin>>X[i]>>Y[i];
		X[i]--,Y[i]--;
		G[i].push_back(N+X[i]);
		G[i].push_back(N+H+Y[i]);
		G[N+X[i]].push_back(i);
		G[N+H+Y[i]].push_back(i);
	}
	for(int i=0;i<N+H+W;i++)vis[i]=-1;
	for(int i=0;i<N;i++)if(vis[i]==-1)
	{
		if(dfs(i,-1))
		{
			vector<int>ret;
			for(int id:ans)if(id<N)ret.push_back(id);
			if(X[ret[0]]==X[ret[1]])
			{
				ret.push_back(ret[0]);
				ret.erase(ret.begin());
			}
			cout<<ret.size()<<endl;
			for(int i=0;i<ret.size();i++)cout<<ret[i]+1<<(i+1==ret.size()?"\n":" ");
			return 0;
		}
	}
	cout<<-1<<endl;
}
0