結果

問題 No.1647 Travel in Mitaru city 2
ユーザー chocoruskchocorusk
提出日時 2021-08-13 22:37:51
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 147 ms / 2,500 ms
コード長 2,403 bytes
コンパイル時間 3,598 ms
コンパイル使用メモリ 191,628 KB
実行使用メモリ 18,692 KB
最終ジャッジ日時 2023-08-03 05:40:45
合計ジャッジ時間 12,723 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 5 ms
9,312 KB
testcase_01 AC 5 ms
10,004 KB
testcase_02 AC 5 ms
10,612 KB
testcase_03 AC 5 ms
9,408 KB
testcase_04 AC 5 ms
10,644 KB
testcase_05 AC 5 ms
10,028 KB
testcase_06 AC 5 ms
9,772 KB
testcase_07 AC 5 ms
9,580 KB
testcase_08 AC 131 ms
17,816 KB
testcase_09 AC 118 ms
17,280 KB
testcase_10 AC 123 ms
17,924 KB
testcase_11 AC 114 ms
16,772 KB
testcase_12 AC 118 ms
17,368 KB
testcase_13 AC 103 ms
14,860 KB
testcase_14 AC 134 ms
18,264 KB
testcase_15 AC 134 ms
17,964 KB
testcase_16 AC 119 ms
17,520 KB
testcase_17 AC 111 ms
16,616 KB
testcase_18 AC 113 ms
15,580 KB
testcase_19 AC 123 ms
16,592 KB
testcase_20 AC 113 ms
15,476 KB
testcase_21 AC 115 ms
15,572 KB
testcase_22 AC 136 ms
18,212 KB
testcase_23 AC 140 ms
18,368 KB
testcase_24 AC 137 ms
17,592 KB
testcase_25 AC 132 ms
17,296 KB
testcase_26 AC 139 ms
18,368 KB
testcase_27 AC 140 ms
18,296 KB
testcase_28 AC 145 ms
18,692 KB
testcase_29 AC 147 ms
18,424 KB
testcase_30 AC 117 ms
15,144 KB
testcase_31 AC 143 ms
18,440 KB
testcase_32 AC 136 ms
16,892 KB
testcase_33 AC 134 ms
16,680 KB
testcase_34 AC 143 ms
18,540 KB
testcase_35 AC 125 ms
16,312 KB
testcase_36 AC 133 ms
18,436 KB
testcase_37 AC 129 ms
18,428 KB
testcase_38 AC 123 ms
17,276 KB
testcase_39 AC 119 ms
16,236 KB
testcase_40 AC 125 ms
16,952 KB
testcase_41 AC 123 ms
16,752 KB
testcase_42 AC 129 ms
17,252 KB
testcase_43 AC 4 ms
9,244 KB
testcase_44 AC 6 ms
9,504 KB
testcase_45 AC 57 ms
12,748 KB
testcase_46 AC 106 ms
14,300 KB
testcase_47 AC 101 ms
15,104 KB
testcase_48 AC 58 ms
13,168 KB
testcase_49 AC 101 ms
14,692 KB
testcase_50 AC 75 ms
13,720 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