結果

問題 No.1647 Travel in Mitaru city 2
ユーザー chocoruskchocorusk
提出日時 2021-08-13 22:37:51
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 141 ms / 2,500 ms
コード長 2,403 bytes
コンパイル時間 3,916 ms
コンパイル使用メモリ 190,640 KB
実行使用メモリ 18,512 KB
最終ジャッジ日時 2024-04-21 04:54:08
合計ジャッジ時間 12,320 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 5 ms
9,424 KB
testcase_01 AC 5 ms
9,672 KB
testcase_02 AC 5 ms
9,928 KB
testcase_03 AC 5 ms
10,188 KB
testcase_04 AC 5 ms
9,616 KB
testcase_05 AC 6 ms
10,708 KB
testcase_06 AC 5 ms
10,056 KB
testcase_07 AC 5 ms
10,828 KB
testcase_08 AC 112 ms
17,868 KB
testcase_09 AC 103 ms
17,228 KB
testcase_10 AC 112 ms
18,000 KB
testcase_11 AC 100 ms
16,588 KB
testcase_12 AC 106 ms
17,276 KB
testcase_13 AC 99 ms
14,868 KB
testcase_14 AC 124 ms
18,156 KB
testcase_15 AC 127 ms
18,248 KB
testcase_16 AC 114 ms
17,736 KB
testcase_17 AC 96 ms
16,844 KB
testcase_18 AC 101 ms
15,388 KB
testcase_19 AC 113 ms
16,456 KB
testcase_20 AC 102 ms
15,656 KB
testcase_21 AC 102 ms
15,572 KB
testcase_22 AC 123 ms
18,256 KB
testcase_23 AC 117 ms
18,272 KB
testcase_24 AC 119 ms
17,480 KB
testcase_25 AC 130 ms
17,484 KB
testcase_26 AC 123 ms
18,256 KB
testcase_27 AC 141 ms
18,380 KB
testcase_28 AC 130 ms
18,384 KB
testcase_29 AC 134 ms
18,376 KB
testcase_30 AC 115 ms
15,184 KB
testcase_31 AC 127 ms
18,384 KB
testcase_32 AC 125 ms
16,848 KB
testcase_33 AC 114 ms
16,636 KB
testcase_34 AC 122 ms
18,376 KB
testcase_35 AC 133 ms
16,468 KB
testcase_36 AC 140 ms
18,508 KB
testcase_37 AC 134 ms
18,512 KB
testcase_38 AC 122 ms
17,224 KB
testcase_39 AC 122 ms
16,336 KB
testcase_40 AC 127 ms
17,096 KB
testcase_41 AC 123 ms
16,848 KB
testcase_42 AC 132 ms
17,108 KB
testcase_43 AC 5 ms
9,572 KB
testcase_44 AC 6 ms
10,192 KB
testcase_45 AC 57 ms
12,540 KB
testcase_46 AC 104 ms
15,312 KB
testcase_47 AC 104 ms
15,304 KB
testcase_48 AC 61 ms
13,136 KB
testcase_49 AC 95 ms
14,824 KB
testcase_50 AC 68 ms
14,032 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
#include <cmath>
#include <bitset>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <algorithm>
#include <complex>
#include <unordered_map>
#include <unordered_set>
#include <random>
#include <cassert>
#include <fstream>
#include <utility>
#include <functional>
#include <time.h>
#include <stack>
#include <array>
#include <list>
#include <atcoder/all>
#define popcount __builtin_popcount
using namespace std;
using namespace atcoder;
typedef long long ll;
typedef pair<int, int> P;
struct unionfind{
    vector<int> par, sz;
    unionfind() {}
    unionfind(int n):par(n), sz(n, 1){
        for(int i=0; i<n; i++) par[i]=i;
    }
    int find(int x){
        if(par[x]==x) return x;
        return par[x]=find(par[x]);
    }
    void unite(int x, int y){
        x=find(x); y=find(y);
        if(x==y) return;
        if(sz[x]>sz[y]) swap(x, y);
        par[x]=y;
        sz[y]+=sz[x];
    }
    bool same(int x, int y){
        return find(x)==find(y);
    }
    int size(int x){
        return sz[find(x)];
    }
};
int main()
{
    int h, w, n;
    cin>>h>>w>>n;
    int x[100010], y[100010];
    int n1=100000;
    vector<P> g[200020];
    unionfind uf(2*n1);
    vector<int> ans;
    int par[200020];
    auto dfs=[&](auto dfs, int x, int p)->void{
        for(auto q:g[x]){
            if(q.first==p){
                par[x]=q.second;
            }else{
                dfs(dfs, q.first, x);
            }
        }
    };
    for(int i=0; i<n; i++){
        cin>>x[i]>>y[i];
        x[i]--; y[i]--;
        if(uf.same(x[i], y[i]+n1)){
            dfs(dfs, y[i]+n1, -1);
            int v=x[i];
            while(v!=y[i]+n1){
                ans.push_back(par[v]);
                if(v<n1){
                    v=y[par[v]]+n1;
                }else{
                    v=x[par[v]];
                }
            }
            ans.push_back(i);
            reverse(ans.begin(), ans.end());
            cout<<ans.size()<<endl;
            for(int i=0; i<ans.size(); i++){
                cout<<ans[i]+1;
                if(i<(int)ans.size()-1) cout<<" ";
            }
            cout<<endl;
            return 0;
        }
        uf.unite(x[i], y[i]+n1);
        g[x[i]].push_back({y[i]+n1,i});
        g[y[i]+n1].push_back({x[i],i});

    }
    cout<<-1<<endl;
    return 0;
}
0