結果

問題 No.2346 Replace!!
ユーザー ytqm3ytqm3
提出日時 2023-06-04 01:12:10
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
(最新)
AC  
(最初)
実行時間 -
コード長 1,792 bytes
コンパイル時間 4,407 ms
コンパイル使用メモリ 266,984 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-08-28 10:41:27
合計ジャッジ時間 9,011 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 AC 13 ms
4,376 KB
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 AC 5 ms
4,380 KB
testcase_14 AC 5 ms
4,376 KB
testcase_15 AC 5 ms
4,380 KB
testcase_16 AC 5 ms
4,376 KB
testcase_17 AC 5 ms
4,376 KB
testcase_18 AC 5 ms
4,376 KB
testcase_19 AC 5 ms
4,376 KB
testcase_20 AC 5 ms
4,376 KB
testcase_21 AC 5 ms
4,380 KB
testcase_22 AC 5 ms
4,380 KB
testcase_23 AC 5 ms
4,376 KB
testcase_24 AC 5 ms
4,380 KB
testcase_25 AC 5 ms
4,376 KB
testcase_26 AC 5 ms
4,376 KB
testcase_27 AC 5 ms
4,380 KB
testcase_28 AC 1 ms
4,380 KB
testcase_29 AC 3 ms
4,376 KB
testcase_30 AC 2 ms
4,380 KB
testcase_31 AC 52 ms
4,380 KB
testcase_32 AC 52 ms
4,380 KB
testcase_33 AC 53 ms
4,380 KB
testcase_34 AC 53 ms
4,376 KB
testcase_35 AC 52 ms
4,380 KB
testcase_36 AC 53 ms
4,376 KB
testcase_37 AC 51 ms
4,380 KB
testcase_38 AC 52 ms
4,376 KB
testcase_39 AC 53 ms
4,376 KB
testcase_40 AC 53 ms
4,376 KB
testcase_41 AC 52 ms
4,376 KB
testcase_42 AC 51 ms
4,376 KB
testcase_43 AC 52 ms
4,376 KB
testcase_44 AC 52 ms
4,380 KB
testcase_45 AC 51 ms
4,380 KB
testcase_46 AC 51 ms
4,380 KB
testcase_47 AC 52 ms
4,376 KB
testcase_48 AC 52 ms
4,380 KB
testcase_49 AC 52 ms
4,376 KB
testcase_50 AC 51 ms
4,380 KB
testcase_51 AC 36 ms
4,376 KB
testcase_52 AC 24 ms
4,380 KB
testcase_53 AC 16 ms
4,376 KB
testcase_54 AC 52 ms
4,376 KB
testcase_55 AC 51 ms
4,376 KB
testcase_56 AC 51 ms
4,376 KB
testcase_57 AC 73 ms
4,376 KB
testcase_58 AC 51 ms
4,380 KB
testcase_59 AC 52 ms
4,380 KB
testcase_60 AC 52 ms
4,376 KB
testcase_61 AC 78 ms
4,380 KB
testcase_62 AC 52 ms
4,376 KB
testcase_63 AC 51 ms
4,380 KB
testcase_64 AC 74 ms
4,380 KB
testcase_65 AC 76 ms
4,376 KB
testcase_66 AC 51 ms
4,376 KB
testcase_67 AC 97 ms
4,380 KB
testcase_68 AC 73 ms
4,376 KB
testcase_69 AC 52 ms
4,376 KB
testcase_70 AC 52 ms
4,384 KB
testcase_71 AC 51 ms
4,376 KB
testcase_72 AC 51 ms
4,376 KB
testcase_73 AC 57 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#include <atcoder/all>
using namespace atcoder;
using namespace std;

bool is_subseq(vector<int> a,vector<int> b){
	//b は a の部分列か?
	int idx=0;
	for(int i=0;i<a.size();++i){
		if(a[i]==b[idx]){ idx++; }
		if(idx==b.size()){ return 1; }
	}
	return 0;
}

void solve(){
  int N;
  cin>>N;
  vector<int> A(N),B(N);
  for(auto& v:A){ cin>>v; v--; }
  for(auto& v:B){ cin>>v; v--; }
  //V : サイクルとして採用したか
  vector<int> V(N);
  //S : サイクルの集合
  vector<vector<int>> S;
  for(int i=0;i<N;++i){
    int nw=i,f=0;
    for(int j=0;j<N;++j){
      if(nw==B[i]){ f=1; }
      nw=A[nw];
      if(nw==i && V[i]==0){
        vector<int> T;
        auto tmp=i;
        for(int k=0;k<=j;++k){
          T.emplace_back(tmp);
          V[tmp]=1;
          tmp=A[tmp];
        }
  			assert(tmp==i);
        S.emplace_back(T);
      }
    }
    if(!f){
    	cout<<"No"<<endl;
      return;
    }
  }
  for(auto v:S){
  	int n=v.size();
  	//v0 -> v1 -> ... -> v{n-1} -> v0
  	//vi - B[vi] が連結か判定
  	atcoder::dsu uf(N);
  	for(int i=0;i<n;++i){
  		uf.merge(v[i],B[v[i]]);
  	}
  	if(uf.groups().size()!=N-n+1){
  		cout<<"No"<<endl;
  		return;
  	}
  	//vi -> B[vi] を張ったグラフにおけるサイクルを検出
  	vector<int> w;
  	for(int i=0;i<n;++i){
  		int nw=v[i];
  		for(int j=0;j<n;++j){
  			nw=B[nw];
  			if(nw==v[i]){
  				int tmp=v[i];
  				for(int k=0;k<=j;++k){
  					w.emplace_back(tmp);
  					tmp=B[tmp];
  				}
  				assert(tmp==v[i]);
  				goto check;
  			}
  		}
  	}
  	check:
  	//w0 は w のうち v で最初にでてくるもの
  	if(!is_subseq(v,w)){
  		cout<<"No"<<endl;
  		return;
  	}
  }
  cout<<"Yes"<<endl;
}

int main(){
  int T;
  cin>>T;
  while(T--){
    solve();
  }
}
0