提出 #75914060


ソースコード 拡げる

#include<bits/stdc++.h>
#define ll long long
#define pii pair<int,int>
#define pll pair<ll,ll>
#define pb push_back
#define eb emplace_back
#define VI vector<int>
#define VII vector<pii>
#define VL vector<ll>
#define VLL vector<pll>
#define rep(i,l,r) for(int i=(l);i<=(r);++i)
#define per(i,r,l) for(int i=(r);i>=(l);--i)

#define LOCAL true

using namespace std;
bool st;
const bool MUL=true;

const int N=3e5+10;
int n,a[N],b[N],in[N],out[N],fa[N],sz[N],D[N];
bool vis[N],act[N];

int find(int x){return fa[x]==x?x:fa[x]=find(fa[x]);}
void merge(int x,int y){x=find(x),y=find(y);if(x!=y){if(sz[x]<sz[y])swap(x,y);fa[y]=x,sz[x]+=sz[y];}return;}

void clr(){
	return;
}
void solve(){
	cin>>n;
	rep(i,1,n)cin>>a[i];
	rep(i,1,n)cin>>b[i];
	rep(i,1,n)in[i]=out[i]=D[i]=vis[i]=act[i]=0,fa[i]=i,sz[i]=1;
	rep(i,1,n)out[a[i]]++,in[b[i]]++,vis[a[i]]=vis[b[i]]=1,merge(a[i],b[i]);
	int m=0,rt=0;
	rep(i,1,n)if(vis[i]){
		int r=find(i);
		if(out[i]>in[i])D[r]+=out[i]-in[i];
		if(!act[r])act[r]=1,m++,rt=r;
	}
	if(m==1)cout<<(D[rt]==0?n:n-D[rt])<<"\n";
	else{
		int ans=n;
		rep(i,1,n)if(act[i])ans-=max(1,D[i]);
		cout<<ans<<"\n";
	}
	return; 
}

bool ed;
signed main(){
	ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
	int tc=1;if(MUL){cin>>tc;}while(tc--)clr(),solve();
// 	cerr<<"time:"<<clock()<<"ms.\n";
// 	cerr<<fixed<<setprecision(4)<<"memory:"<<(1.0*(&st-&ed)/1048576.0)<<"MB.\n";
	return 0;
}

提出情報

提出日時
問題 B - Incomplete Shuffle
ユーザ Fourier_WJY
言語 C++23 (GCC 15.2.0)
得点 800
コード長 1453 Byte
結果 AC
実行時間 39 ms
メモリ 12500 KiB

ジャッジ結果

セット名 Sample All
得点 / 配点 0 / 0 800 / 800
結果
AC × 1
AC × 33
セット名 テストケース
Sample 00_sample_00.txt
All 00_sample_00.txt, 01_small_00.txt, 01_small_01.txt, 01_small_02.txt, 02_handmade_00.txt, 02_handmade_01.txt, 02_handmade_02.txt, 02_handmade_03.txt, 02_handmade_04.txt, 03_random_00.txt, 03_random_01.txt, 03_random_02.txt, 03_random_03.txt, 03_random_04.txt, 03_random_05.txt, 03_random_06.txt, 03_random_07.txt, 03_random_08.txt, 03_random_09.txt, 03_random_10.txt, 03_random_11.txt, 03_random_12.txt, 03_random_13.txt, 03_random_14.txt, 03_random_15.txt, 03_random_16.txt, 03_random_17.txt, 03_random_18.txt, 03_random_19.txt, 03_random_20.txt, 03_random_21.txt, 03_random_22.txt, 03_random_23.txt
ケース名 結果 実行時間 メモリ
00_sample_00.txt AC 1 ms 3628 KiB
01_small_00.txt AC 22 ms 3556 KiB
01_small_01.txt AC 4 ms 3716 KiB
01_small_02.txt AC 24 ms 3796 KiB
02_handmade_00.txt AC 25 ms 12436 KiB
02_handmade_01.txt AC 25 ms 12420 KiB
02_handmade_02.txt AC 20 ms 12268 KiB
02_handmade_03.txt AC 19 ms 12388 KiB
02_handmade_04.txt AC 33 ms 12368 KiB
03_random_00.txt AC 37 ms 12260 KiB
03_random_01.txt AC 37 ms 12268 KiB
03_random_02.txt AC 37 ms 12372 KiB
03_random_03.txt AC 18 ms 8104 KiB
03_random_04.txt AC 37 ms 12500 KiB
03_random_05.txt AC 9 ms 5908 KiB
03_random_06.txt AC 37 ms 12372 KiB
03_random_07.txt AC 4 ms 4356 KiB
03_random_08.txt AC 27 ms 3732 KiB
03_random_09.txt AC 25 ms 3520 KiB
03_random_10.txt AC 26 ms 3564 KiB
03_random_11.txt AC 16 ms 7388 KiB
03_random_12.txt AC 12 ms 6312 KiB
03_random_13.txt AC 39 ms 12388 KiB
03_random_14.txt AC 39 ms 12268 KiB
03_random_15.txt AC 39 ms 12268 KiB
03_random_16.txt AC 39 ms 12200 KiB
03_random_17.txt AC 27 ms 3704 KiB
03_random_18.txt AC 26 ms 3716 KiB
03_random_19.txt AC 27 ms 3728 KiB
03_random_20.txt AC 24 ms 12200 KiB
03_random_21.txt AC 25 ms 12432 KiB
03_random_22.txt AC 25 ms 12248 KiB
03_random_23.txt AC 25 ms 12252 KiB