結果

問題 No.1647 Travel in Mitaru city 2
ユーザー むかでむかで
提出日時 2021-08-14 00:44:41
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
(最新)
AC  
(最初)
実行時間 -
コード長 6,303 bytes
コンパイル時間 2,817 ms
コンパイル使用メモリ 216,920 KB
実行使用メモリ 65,580 KB
最終ジャッジ日時 2024-04-19 04:58:05
合計ジャッジ時間 20,841 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 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 4 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 6 ms
5,376 KB
testcase_08 AC 286 ms
60,832 KB
testcase_09 AC 245 ms
45,440 KB
testcase_10 AC 322 ms
61,200 KB
testcase_11 AC 270 ms
55,528 KB
testcase_12 AC 316 ms
62,472 KB
testcase_13 AC 316 ms
59,852 KB
testcase_14 AC 322 ms
62,664 KB
testcase_15 AC 313 ms
62,032 KB
testcase_16 AC 289 ms
58,156 KB
testcase_17 AC 300 ms
56,792 KB
testcase_18 AC 302 ms
55,936 KB
testcase_19 AC 319 ms
61,120 KB
testcase_20 AC 315 ms
59,904 KB
testcase_21 AC 312 ms
61,572 KB
testcase_22 AC 335 ms
63,752 KB
testcase_23 AC 336 ms
63,956 KB
testcase_24 AC 333 ms
64,212 KB
testcase_25 AC 315 ms
63,964 KB
testcase_26 AC 342 ms
64,084 KB
testcase_27 AC 339 ms
64,312 KB
testcase_28 AC 344 ms
65,576 KB
testcase_29 AC 342 ms
65,568 KB
testcase_30 AC 326 ms
60,264 KB
testcase_31 AC 349 ms
65,452 KB
testcase_32 AC 320 ms
57,984 KB
testcase_33 AC 349 ms
65,580 KB
testcase_34 AC 349 ms
65,452 KB
testcase_35 AC 324 ms
61,088 KB
testcase_36 AC 328 ms
65,576 KB
testcase_37 AC 342 ms
65,448 KB
testcase_38 AC 325 ms
56,972 KB
testcase_39 AC 302 ms
55,620 KB
testcase_40 AC 317 ms
56,384 KB
testcase_41 AC 267 ms
46,936 KB
testcase_42 AC 299 ms
56,756 KB
testcase_43 AC 2 ms
5,376 KB
testcase_44 AC 6 ms
7,936 KB
testcase_45 AC 247 ms
43,296 KB
testcase_46 AC 313 ms
43,332 KB
testcase_47 AC 229 ms
43,296 KB
testcase_48 WA -
testcase_49 AC 244 ms
43,392 KB
testcase_50 AC 251 ms
43,392 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define FOR(i,n,m) for(int i=(int)(n); i<=(int)(m); i++)
#define RFOR(i,n,m) for(int i=(int)(n); i>=(int)(m); i--)
#define ITR(x,c) for(__typeof(c.begin()) x=c.begin();x!=c.end();x++)
#define RITR(x,c) for(__typeof(c.rbegin()) x=c.rbegin();x!=c.rend();x++)
#define setp(n) fixed << setprecision(n)

template<class T> bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T> bool chmin(T &a, const T &b) { if (a>b) { a=b; return 1; } return 0; }

#define ll long long
#define vll vector<ll>
#define vi vector<int>
#define pll pair<ll,ll>
#define pi pair<int,int>

#define all(a) (a.begin()),(a.end())
#define rall(a) (a.rbegin()),(a.rend())
#define fi first
#define se second
#define pb push_back
#define ins insert

#define debug(a) cerr<<(a)<<endl
#define dbrep(a,n) rep(_i,n) cerr<<(a[_i])<<" "; cerr<<endl
#define dbrep2(a,n,m) rep(_i,n){rep(_j,m) cerr<<(a[_i][_j])<<" "; cerr<<endl;}

using namespace std;

template<class A, class B>
ostream &operator<<(ostream &os, const pair<A,B> &p){return os<<"("<<p.fi<<","<<p.se<<")";}
template<class A, class B>
istream &operator>>(istream &is, pair<A,B> &p){return is>>p.fi>>p.se;}

template<class T>
vector<T> make_vec(size_t a){
	return vector<T>(a);
}
template<class T, class... Ts>
auto make_vec(size_t a, Ts... ts){
	return vector<decltype(make_vec<T>(ts...))>(a, make_vec<T>(ts...));
}

//-------------------------------------------------
//--Graph Template
//-------------------------------------------------
template<class E>
struct Graph
{
    vector<vector<E> > adj;
    Graph(int N):adj(N){}
    virtual void add_edge(int v, E e){adj[v].push_back(e);}
    vector<E>& operator[](int v){return adj[v];}
    inline int size(){return adj.size();}
};

namespace graph {
	struct SimpleEdge {
		int to;
		SimpleEdge(int to):to(to){}
	};
}

struct UWGraph : public Graph<graph::SimpleEdge>
{
    UWGraph(int N):Graph(N){}
    void add_edge(int v, int e){adj[v].push_back({e});}
};


int N;

//-------------------------------------------------
//--Get Cycle Any
//-------------------------------------------------
namespace Cycle
{
    stack<int> st;
    vector<bool> seen,finished;
    int pos;

    template<class E>
    void dfs(int v, int p, Graph<E> &G, bool dir=true){
        st.push(v);
        seen[v] = true;
		if (v<N) finished[v+3*N] = true;
		else if(3*N<=v && v<4*N) finished[v] = true;
        for(auto &e:G[v]){
            if (!dir && e.to==p) continue;
            if (finished[e.to]) continue;
            if (seen[e.to] && !finished[e.to]){
                pos = e.to;
                return;
            }
            dfs(e.to,v,G,dir);
            if (pos!=-1) return;
        }
        finished[v] = true;
        st.pop();
    }

    void init(int V){
        pos = -1; st=stack<int>();
        seen.resize(V,false);
        finished.resize(V,false);
    }

    vector<int> restore(){
        vector<int> ret;
        if (pos==-1) return ret;
        while(!st.empty()){
            int u = st.top(); st.pop();
            ret.push_back(u);
            if (u==pos) break;
        }
        reverse(ret.begin(),ret.end());
        return ret;
    }

    template<class E>
    vector<int> get(Graph<E> &G, bool dir=true){
        int V = G.size();
        init(V);
        for(int i=0;i<V;i++)if(!seen[i]){
            dfs(i,-1,G,dir);
            if (pos!=-1) break;
        }
        return restore();
    }

    template<class E>
    vector<int> get(int v, Graph<E> &G, bool dir=true){
        int V = G.size();
        init(V);
        dfs(v,-1,G,dir);
        return restore();
    }

    template<class E>
    vector<int> minimize(vector<int> &cycle, Graph<E> &G, bool dir=true){
        vector<int> ret;
        int L = cycle.size();
        if (L==0) return ret;
        unordered_map<int,int> Mp;
        for(int j=0;j<L;j++) Mp[cycle[j]]=j;
        int i=0;
        while(i<L){
            ret.push_back(cycle[i]);
            int ni=i;
            for(auto &e:G[cycle[i]]){
                if (!dir && i==0 && e.to==cycle[L-1]) continue;
                if (!dir && ret.size()<3 && e.to==cycle[0]) continue;
                if (e.to==cycle[0]){
                    ni=L;
                    break;
                }else{
                    chmax(ni,Mp[e.to]);
                }
            }
            i=ni;
        }
        return ret;
    }
}

//-------------------------------------------------

int main(void)
{
    cin.tie(0);
    ios::sync_with_stdio(false);
	int H,W; cin>>H>>W>>N;
	
	vi y(N),x(N);
	vector<vi> row(H), col(W);
	rep(i,N){
		cin>>y[i]>>x[i]; y[i]--; x[i]--;
		row[y[i]].pb(i);
		col[x[i]].pb(i);
	}

	UWGraph G(6*N);
	rep(i,H){
		auto &vec = row[i];
		int sz = vec.size();
		rep(j,sz){
			int from = vec[j] + N;
			int to = vec[j];
			G.add_edge(from,to);
			from = vec[j] + 2*N;
			G.add_edge(from,to);
		}
		rep(j,sz-1){
			int from = vec[j] + N;
			int to = vec[j+1] + N;
			G.add_edge(from,to);
		}
		rep(j,sz-1){
			int from = vec[j+1] + 2*N;
			int to = vec[j] + 2*N;
			G.add_edge(from,to);
		}
		rep(j,sz-1){
			int from = vec[j] + 3*N;
			int to = vec[j+1] + N;
			G.add_edge(from,to);
		}
		rep(j,sz-1){
			int from = vec[j+1] + 3*N;
			int to = vec[j] + 2*N;
			G.add_edge(from,to);
		}
	}

	rep(i,W){
		auto &vec = col[i];
		int sz = vec.size();
		rep(j,sz){
			int from = vec[j] + 4*N;
			int to = vec[j] + 3*N;
			G.add_edge(from,to);
			from = vec[j] + 5*N;
			G.add_edge(from,to);
		}
		rep(j,sz-1){
			int from = vec[j] + 4*N;
			int to = vec[j+1] + 4*N;
			G.add_edge(from,to);
		}
		rep(j,sz-1){
			int from = vec[j+1] + 5*N;
			int to = vec[j] + 5*N;
			G.add_edge(from,to);
		}
		rep(j,sz-1){
			int from = vec[j];
			int to = vec[j+1] + 4*N;
			G.add_edge(from,to);
		}
		rep(j,sz-1){
			int from = vec[j+1];
			int to = vec[j] + 5*N;
			G.add_edge(from,to);
		}
	}
	
	auto cycle = Cycle::get(G);

	if (cycle.empty()){
		cout<<"-1\n";
	}else{
		int sz = cycle.size();
		vi ans;
		rep(i,sz){
			int val = cycle[i]/N;
			if (val==0 || val==3){
				ans.pb(cycle[i]%N+1);
			}
		}
		int V = ans.size();

		cout<<V<<"\n";
		if (x[ans[0]-1]==x[ans[1]-1]){
			rep(i,V) cout<<ans[i]<<" \n"[i==V-1];
		}else{
			rep(i,V) cout<<ans[(i+1)%V]<<" \n"[i==V-1];
		}
	}

    return 0;
}
0