結果

問題 No.1647 Travel in Mitaru city 2
ユーザー rianoriano
提出日時 2021-08-13 22:13:57
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 94 ms / 2,500 ms
コード長 4,553 bytes
コンパイル時間 2,578 ms
コンパイル使用メモリ 176,056 KB
実行使用メモリ 26,728 KB
最終ジャッジ日時 2024-04-21 04:51:16
合計ジャッジ時間 8,895 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 1 ms
5,248 KB
testcase_02 AC 2 ms
5,248 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 79 ms
24,652 KB
testcase_09 AC 78 ms
23,304 KB
testcase_10 AC 82 ms
24,664 KB
testcase_11 AC 77 ms
23,464 KB
testcase_12 AC 77 ms
22,148 KB
testcase_13 AC 85 ms
23,672 KB
testcase_14 AC 87 ms
25,188 KB
testcase_15 AC 80 ms
24,876 KB
testcase_16 AC 76 ms
23,836 KB
testcase_17 AC 76 ms
22,764 KB
testcase_18 AC 82 ms
24,528 KB
testcase_19 AC 88 ms
24,352 KB
testcase_20 AC 82 ms
23,096 KB
testcase_21 AC 92 ms
25,620 KB
testcase_22 AC 87 ms
25,808 KB
testcase_23 AC 87 ms
25,920 KB
testcase_24 AC 76 ms
21,552 KB
testcase_25 AC 86 ms
23,736 KB
testcase_26 AC 94 ms
25,908 KB
testcase_27 AC 91 ms
25,996 KB
testcase_28 AC 94 ms
26,596 KB
testcase_29 AC 89 ms
26,604 KB
testcase_30 AC 80 ms
24,424 KB
testcase_31 AC 88 ms
26,600 KB
testcase_32 AC 88 ms
24,804 KB
testcase_33 AC 85 ms
25,836 KB
testcase_34 AC 90 ms
26,728 KB
testcase_35 AC 86 ms
24,168 KB
testcase_36 AC 94 ms
26,472 KB
testcase_37 AC 90 ms
26,476 KB
testcase_38 AC 79 ms
22,620 KB
testcase_39 AC 70 ms
19,616 KB
testcase_40 AC 81 ms
22,204 KB
testcase_41 AC 79 ms
20,252 KB
testcase_42 AC 87 ms
22,344 KB
testcase_43 AC 2 ms
5,376 KB
testcase_44 AC 8 ms
11,008 KB
testcase_45 AC 61 ms
16,384 KB
testcase_46 AC 56 ms
14,788 KB
testcase_47 AC 57 ms
16,284 KB
testcase_48 AC 54 ms
16,216 KB
testcase_49 AC 56 ms
16,464 KB
testcase_50 AC 56 ms
16,220 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define rep(i,n) for(int (i)=0;(i)<(n);(i)++)
#define Pr pair<ll,ll>
#define Tp tuple<int,int,int>
#define riano_ std::ios::sync_with_stdio(false);std::cin.tie(nullptr);typedef modint<mod> mint
using Graph = vector<vector<Pr>>;

const ll mod = 998244353;
template<uint64_t mod>
struct modint{
    uint64_t val;
    constexpr modint(const int64_t val_=0) noexcept:val((val_%int64_t(mod)+int64_t(mod))%int64_t(mod)){}
    constexpr modint operator-() const noexcept{
        return modint(*this)=mod-val;
    }
    constexpr modint operator+(const modint rhs) const noexcept{
        return modint(*this)+=rhs;
    }
    constexpr modint operator-(const modint rhs) const noexcept{
        return modint(*this)-=rhs;
    }
    constexpr modint operator*(const modint rhs) const noexcept{
        return modint(*this)*=rhs;
    }
    constexpr modint operator/(const modint rhs) const noexcept{
        return modint(*this)/=rhs;
    }
    constexpr modint &operator+=(const modint rhs) noexcept{
        val+=rhs.val;
        val-=((val>=mod)?mod:0);
        return (*this);
    }
    constexpr modint &operator-=(const modint rhs) noexcept{
        val+=((val<rhs.val)?mod:0);
        val-=rhs.val;
        return (*this);
    }
    constexpr modint &operator*=(const modint rhs) noexcept{
        val=val*rhs.val%mod;
        return (*this);
    }
    constexpr modint &operator/=(modint rhs) noexcept{
        uint64_t ex=mod-2;
        modint now=1;
        while(ex){
            now*=((ex&1)?rhs:1);
            rhs*=rhs,ex>>=1;
        }
        return (*this)*=now;
    }
    constexpr bool operator==(const modint rhs) noexcept{
        return val==rhs.val;
    }
    constexpr bool operator!=(const modint rhs) noexcept{
        return val!=rhs.val;
    }
    friend constexpr ostream &operator<<(ostream& os,const modint x) noexcept{
        return os<<(x.val);
    }
    friend constexpr istream &operator>>(istream& is,modint& x) noexcept{
        uint64_t t;
        is>>t,x=t;
        return is;
    }
};


//Unionfind
struct unionfind {
	//自身が親であれば、その集合に属する頂点数に-1を掛けたもの
	//そうでなければ親のid
	  vector<long long> r;
	  unionfind(long long N) {
		    r = vector<long long>(N, -1);
	  }
	  long long root(long long x) {
		    if (r[x] < 0) return x;
		    return r[x] = root(r[x]);
	  }
	  bool unite(long long x, long long y) {
		    x = root(x);
		    y = root(y);
		    if (x == y) return false;
		    if (r[x] > r[y]) swap(x, y);
		    r[x] += r[y];
		    r[y] = x;
		    return true;
	  }
	  long long size(long long x) {
		    return max(-r[root(x)],0LL);
	  }
  
  // 2つのデータx, yが属する木が同じならtrueを返す
    bool same(long long x, long long y) { 
        long long rx = root(x);
        long long ry = root(y);
        return rx == ry;
    }
    //重みをつける場合
    void eval(ll i,ll x){
        r[i] = (-1)*x;
    }
};
  //main関数内で unionfind friends(N+1);
  //などと宣言し  friends.unite(a,b);
  //のように使う


//dfs
//s:始点 i:dfs回数 t:始点からの距離
vector<int> vis; int t;
vector<int> depth;
vector<ll> ans;
//探索範囲を距離L以下に制限する場合
int L;
bool en = false;
void dfs(Graph &G, int s,int i,int st){
    t++;
    for(auto p:G[s]){
        int nx = p.first; int k = p.second;
        if(t>=L) break;
        if(nx==st&&depth[s]!=1){
            en = true; ans.push_back(k);
            break;
        }
        if(vis[nx]==i) continue;
        ans.push_back(k);
        depth[nx] = depth[s] + 1;
        vis[nx] = i;
        dfs(G,nx,i,st);
        if(en) return;
        ans.pop_back();
    }
    if(en) return;
    t--;
}
    

int main() {
    riano_;
    //ll N; cin >> N; ll ans = 0;
    ll H,W,N; cin >> H >> W >> N;
    unionfind rc(H+W);
    Graph G(H+W);
    ll st = -1;
    rep(i,N){
        ll x,y; cin >> x >> y; x--; y--;
        if(rc.same(x,y+H)) st = x;
        rc.unite(x,y+H);
        G[x].push_back(make_pair(y+H,i+1));
        G[y+H].push_back(make_pair(x,i+1));
    }
    if(st<0){
        cout << -1 << endl; return 0;
    }
    //main関数内で
    vis.assign(H+W+1,-1);
    depth.assign(H+W+1,-1);
    vis[st] = 0; depth[st] = 0; t = -1; //s:始点
    L= 2000000000; //必要なら設定する
    dfs(G,st,0,st); //s:始点 i:dfs回数
    cout << ans.size() << endl;
    rep(i,ans.size()){
        cout << ans[i] << " ";
    }
    cout << endl;
    //cout << ans << endl;
}
0