結果

問題 No.2316 Freight Train
ユーザー planesplanes
提出日時 2023-05-26 21:31:48
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 344 ms / 2,000 ms
コード長 1,124 bytes
コンパイル時間 1,470 ms
コンパイル使用メモリ 170,324 KB
実行使用メモリ 7,892 KB
最終ジャッジ日時 2023-08-26 10:29:48
合計ジャッジ時間 11,117 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 1 ms
4,384 KB
testcase_03 AC 335 ms
7,692 KB
testcase_04 AC 177 ms
5,424 KB
testcase_05 AC 125 ms
5,420 KB
testcase_06 AC 43 ms
4,380 KB
testcase_07 AC 292 ms
4,376 KB
testcase_08 AC 183 ms
7,828 KB
testcase_09 AC 248 ms
6,268 KB
testcase_10 AC 274 ms
5,348 KB
testcase_11 AC 226 ms
7,188 KB
testcase_12 AC 280 ms
6,944 KB
testcase_13 AC 339 ms
7,844 KB
testcase_14 AC 336 ms
7,704 KB
testcase_15 AC 338 ms
7,736 KB
testcase_16 AC 338 ms
7,816 KB
testcase_17 AC 338 ms
7,760 KB
testcase_18 AC 337 ms
7,744 KB
testcase_19 AC 344 ms
7,808 KB
testcase_20 AC 330 ms
7,796 KB
testcase_21 AC 338 ms
7,888 KB
testcase_22 AC 338 ms
7,704 KB
testcase_23 AC 301 ms
7,728 KB
testcase_24 AC 328 ms
7,804 KB
testcase_25 AC 320 ms
7,812 KB
testcase_26 AC 310 ms
7,892 KB
testcase_27 AC 275 ms
4,376 KB
testcase_28 AC 1 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

  #include <bits/stdc++.h> 
using namespace std;
using ll =long long;
#define all(v) v.begin(),v.end()
 #define rep(i,a,b) for(int i=a;i<b;i++)
#define rrep(i,a,b) for(int i=a;i>=b;i--)

ll INF=2e18;

struct UnionFind {

vector<ll> par;
vector<ll> r;

void init(ll n) {
    par=vector<ll> (n);
    r=vector<ll> (n);

    for(ll i=0;i<n;i++) {
        par[i]=i;
        r[i]=0LL;
    }
}

ll find(ll x) {
    if(par[x]==x) {
        return x;
    }
    return par[x]=find(par[x]);

}

void unite(ll x,ll y) {
    x=find(x);
    y=find(y);
    if(x==y) {
        return;
    }
    if(r[x]<r[y]) {
        par[x]=y;
    }
    else {
        par[y]=x;
    if(r[x]==r[y]) {
        r[x]++;
    }
    }
}

bool same(ll x,ll y) {
    return find(x)==find(y);
}

};





int main() {
    ios::sync_with_stdio(false);
  cin.tie(0);
ll N,Q;cin>>N>>Q;
vector<ll> P(N);
for(ll i=0;i<N;i++) {
  cin>>P[i];
  if(P[i]>=0) P[i]--;
}

UnionFind p;
p.init(N);
for(ll i=0;i<N;i++) {
if(P[i]!=-1) {
  p.unite(i,P[i]);
}
}

for(ll i=0;i<Q;i++) {
  ll A,B;cin>>A>>B;
  A--;B--;
  if(p.same(A,B)) cout<<"Yes"<<endl;
  else cout<<"No"<<endl;
}
}


0