結果

問題 No.2346 Replace!!
ユーザー kotatsugamekotatsugame
提出日時 2023-06-10 00:08:23
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 222 ms / 2,000 ms
コード長 1,281 bytes
コンパイル時間 718 ms
コンパイル使用メモリ 75,868 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-08-30 15:12:55
合計ジャッジ時間 6,688 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include<iostream>
#include<vector>
using namespace std;
int N,A[5000],B[5000];
bool vis[5000];
bool loop[5000];
int inv[5000],deg[5000];
bool check(vector<int>L)
{
	while(true)
	{
		for(int i=0;i<L.size();i++)
		{
			inv[L[i]]=i;
			deg[i]=0;
		}
		for(int i=0;i<L.size();i++)
		{
			deg[inv[B[L[i]]]]++;
		}
		bool fn=false;
		int use=-1;
		for(int i=0;i<L.size();i++)
		{
			if(B[L[i]]!=L[(i+1)%L.size()])
			{
				fn=true;
				if(deg[(i+1)%L.size()]==0)use=(i+1)%L.size();
			}
		}
		if(!fn)return true;
		if(use==-1)return false;
		L.erase(L.begin()+use);
	}
}
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	int T;
	cin>>T;
	for(;T--;)
	{
		cin>>N;
		for(int i=0;i<N;i++)cin>>A[i],A[i]--;
		for(int i=0;i<N;i++)cin>>B[i],B[i]--;
		for(int i=0;i<N;i++)loop[i]=false;
		bool ok=true;
		vector<vector<int> >loops;
		for(int i=0;i<N;i++)
		{
			int u=A[i];
			for(int j=0;j<N;j++)vis[j]=false;
			while(!vis[u])
			{
				vis[u]=true;
				u=A[u];
			}
			if(!vis[B[i]])ok=false;
			if(u==A[i]&&!loop[u])
			{
				loops.push_back(vector<int>());
				while(!loop[u])
				{
					loops.back().push_back(u);
					loop[u]=true;
					u=A[u];
				}
			}
		}
		if(ok)
		{
			for(vector<int>L:loops)
			{
				if(!check(L))ok=false;
			}
		}
		cout<<(ok?"Yes\n":"No\n");
	}
}
0