stablegradients/pie-gem5-pairs huggingface dataset:
// Slower C++ solution:
#include <bits/stdc++.h>

 

#pragma GCC optimize("O3")

using namespace std;

#define ll long long

#define ull unsigned long long

#define rep(i,n,N) for(int i=n;i<=N;++i)

#define rap(i,n,N) for(int i=n;i>=N;--i)

#define mp make_pair

#define pb push_back

#define pob pop_back

#define pf push_front

#define pof pop_front

#define fi first

#define se second

#define ff fi.fi

#define fs fi.se

#define sf se.fi

#define ss se.se

#define lc (id<<1)

#define rc ((id<<1)|1)

#define db(x) cout << ">>>>>> " << #x << " -> " << x << endl

#define all(x) x.begin(),x.end()

#define pii pair<int,int> 

#define pll pair<ll,ll>

#define plll pair<ll,pll>

#define pllll pair<pll,pll>

#define piii pair<int,pii>

#define piiii pair<pii,pii>

#define psi pair<string,int>

#define endl '\n'

const int MAX = 1e6+5;

const ll MAX2 = 11;

const ll MOD = 998244353;

const ll MOD2 = 1000000006;

const ll INF = 2e18;

const int dr[]={1,0,-1,0,1,1,-1,-1,0};

const int dc[]={0,1,0,-1,1,-1,1,-1,0};

const double pi = acos(-1);

const double EPS = 1e-9;

const int block = 316;

 

int n,dp[MAX][2],x[MAX];

char c;



int f(int pos,bool st){

	if(pos>n)return 0;

	if(dp[pos][st]!=-1)return dp[pos][st];

	

	if(st)return dp[pos][st] = min(f(pos+1,0)+x[pos],f(pos+1,1)+9-x[pos]);

	return dp[pos][st] = min(f(pos+1,0)+x[pos],f(pos+1,1)+11-x[pos]);

}



int main(){

//	cout<<fixed<<setprecision(2);

//    freopen("input.txt", "r", stdin);

//	freopen("output.txt","w",stdout);

    ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);

    while(cin>>c)x[++n] = c-'0';

    memset(dp,-1,sizeof dp);

    cout<<f(1,0)<<endl;

    return 0;

}

// Faster C++ solution for the same problem (measured speedup: 9.364264376633376x):
#include <iostream>

#include <algorithm>

#include <string>

#include <map>

using namespace std;

const int maxn = 2e5+10;



string s;

int main(){

    cin>>s;

    reverse(s.begin(),s.end());

    int cnt = 0,nx = 0;

    for(int i = 0;i<s.length();i++){

        int cur = s[i]-'0'+nx;nx = 0;

        if(cur>5){

            cnt += 10-cur;

            nx = 1;

        }else if(cur<5){

            cnt += cur;

        }else{

            if(i+1<s.length()&& s[i+1]-'0'>=5){

                cnt += 10-cur;

                nx = 1;

            }else{

                cnt += cur;

            }

        }

    }

    if(nx) cnt += nx;

    cout<<cnt<<endl;

    return 0;

}
// Slower C++ solution:
#include <bits/stdc++.h>

 

#pragma GCC optimize("O3")

using namespace std;

#define ll long long

#define ull unsigned long long

#define rep(i,n,N) for(int i=n;i<=N;++i)

#define rap(i,n,N) for(int i=n;i>=N;--i)

#define mp make_pair

#define pb push_back

#define pob pop_back

#define pf push_front

#define pof pop_front

#define fi first

#define se second

#define ff fi.fi

#define fs fi.se

#define sf se.fi

#define ss se.se

#define lc (id<<1)

#define rc ((id<<1)|1)

#define db(x) cout << ">>>>>> " << #x << " -> " << x << endl

#define all(x) x.begin(),x.end()

#define pii pair<int,int> 

#define pll pair<ll,ll>

#define plll pair<ll,pll>

#define pllll pair<pll,pll>

#define piii pair<int,pii>

#define piiii pair<pii,pii>

#define psi pair<string,int>

#define endl '\n'

const int MAX = 1e6+5;

const ll MAX2 = 11;

const ll MOD = 998244353;

const ll MOD2 = 1000000006;

const ll INF = 2e18;

const int dr[]={1,0,-1,0,1,1,-1,-1,0};

const int dc[]={0,1,0,-1,1,-1,1,-1,0};

const double pi = acos(-1);

const double EPS = 1e-9;

const int block = 316;

 

int n,dp[MAX][2],x[MAX];

char c;



int f(int pos,bool st){

	if(pos>n)return 0;

	if(dp[pos][st]!=-1)return dp[pos][st];

	

	if(st)return dp[pos][st] = min(f(pos+1,0)+x[pos],f(pos+1,1)+9-x[pos]);

	return dp[pos][st] = min(f(pos+1,0)+x[pos],f(pos+1,1)+11-x[pos]);

}



int main(){

//	cout<<fixed<<setprecision(2);

//    freopen("input.txt", "r", stdin);

//	freopen("output.txt","w",stdout);

    ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);

    while(cin>>c)x[++n] = c-'0';

    memset(dp,-1,sizeof dp);

    cout<<f(1,0)<<endl;

    return 0;

}

// Faster C++ solution for the same problem (measured speedup: 9.495070360100582x):
#include<bits/stdc++.h>

using namespace std;

#define ll long long

#define ull unsigned long long

#define mp make_pair

typedef pair<int,int> pii;

char str[1000009];

int f[1000009];

int main()

{

    scanf("%s",str+1);

    int len=strlen(str+1);

    int answer=0;

    for(int i=1;i<=len;i++){

        f[i]=str[i]-'0';

    }

    for(int i=1;i<=len+10;i++)

    {

        if(f[i]>=10){

            f[i]-=10;

            f[i+1]++;

        }

        if(f[i]>=6){

            answer+=10-f[i];

            f[i+1]++;

        }

        else if(f[i]==5){

            answer+=5;

            if(f[i+1]>=5){

                f[i+1]++;

            }

        }

        else answer+=f[i];

    }

    cout<<answer<<endl;

    return 0;

}
// Slower C++ solution:
#include <stdio.h>



int main() {

    int n, k;

    scanf("%d %d", &n, &k);

    long long int data[200000] = {0};

    for (int i = 0; i < n; i++) {

        scanf("%d", &data[i]);

    }

    bool fr = false;

    for (int j = k - 1; j < n; j++) {

        if (!fr) {

            fr = true;

            continue;

        }

        if (data[j] > data[j - k]) {

            printf("Yes\n");

        } else {

            printf("No\n");

        }

    }

}

// Faster C++ solution for the same problem (measured speedup: 8.443027358114637x):
#include <stdio.h>



int main(){

    int n, k;

    scanf("%d %d", &n, &k);

    int a[200001];

    for(int i = 0; i<n; i++){

        scanf("%d", &a[i]); 

    }

   

    for(int i = 0, j = k; i<n-k; i++, j++){

        if(a[j] > a[j-k]){

            printf("Yes\n");

        }

        else{

            printf("No\n");

        }

    }



    return 0;

}
// Slower C++ solution:
#include <stdio.h>



int main() {

    int n, k;

    scanf("%d %d", &n, &k);

    long long int data[200000] = {0};

    for (int i = 0; i < n; i++) {

        scanf("%d", &data[i]);

    }

    bool fr = false;

    for (int j = k - 1; j < n; j++) {

        if (!fr) {

            fr = true;

            continue;

        }

        if (data[j] > data[j - k]) {

            printf("Yes\n");

        } else {

            printf("No\n");

        }

    }

}

// Faster C++ solution for the same problem (measured speedup: 9.4304694735985x):
#include <stdio.h>

#include <string.h>



int main(){

	int K, N; 

	scanf("%d %d", &N, &K);

	long long int num[N] = {0};

	

	for (int i=0; i<N; i++){

		scanf ("%lld", &num[i]);

	}

	

	for (int i=0; i < N-K ; i++){

		if (num[i] < num[i+K]){   // misal 0 1 2 3 4 5  dan K nya 2

			printf ("Yes\n");	// bandingin aja arr 0 dan arr 2 karena ngebandingin 0*1 dan 1*2

		}

		else {

			printf ("No\n");

		}

	}

return 0;

}
// Slower C++ solution:
#include <stdio.h>



int main() {

    int n, k;

    scanf("%d %d", &n, &k);

    long long int data[200000] = {0};

    for (int i = 0; i < n; i++) {

        scanf("%d", &data[i]);

    }

    bool fr = false;

    for (int j = k - 1; j < n; j++) {

        if (!fr) {

            fr = true;

            continue;

        }

        if (data[j] > data[j - k]) {

            printf("Yes\n");

        } else {

            printf("No\n");

        }

    }

}

// Faster C++ solution for the same problem (measured speedup: 9.452538385725024x):
#include<stdio.h>

int main(){

    int N,K;

    scanf("%d %d", &N, &K);

    int total[N];

    int i;

    for(i=0;i<N;i++){

        scanf("%d", &total[i]);

    }

    for(i=K;i<N;i++){

        if(total[i-K]<total[i]){

            printf("Yes\n");

        }else {

            printf("No\n");

        }

    }

    return 0;

}
// Slower C++ solution:
#include <stdio.h>



int main() {

    int n, k;

    scanf("%d %d", &n, &k);

    long long int data[200000] = {0};

    for (int i = 0; i < n; i++) {

        scanf("%d", &data[i]);

    }

    bool fr = false;

    for (int j = k - 1; j < n; j++) {

        if (!fr) {

            fr = true;

            continue;

        }

        if (data[j] > data[j - k]) {

            printf("Yes\n");

        } else {

            printf("No\n");

        }

    }

}

// Faster C++ solution for the same problem (measured speedup: 9.45045050890767x):
#include <stdio.h>



int main(){

	

	int a, b;

	scanf("%d %d", &a, &b);

	int nilai[a];

	for(int i = 0; i<a; i++){	

		scanf("%d", &nilai[i]);

	}

	

	for(int j = 0; j<a-b; j++){

		if(nilai[j] < nilai[j+b]){

		printf("Yes\n");

		}else{

			printf("No\n");

		}

		

	}

	return 0;

}
// Slower C++ solution:
#include <stdio.h>



int main() {

    int n, k;

    scanf("%d %d", &n, &k);

    long long int data[200000] = {0};

    for (int i = 0; i < n; i++) {

        scanf("%d", &data[i]);

    }

    bool fr = false;

    for (int j = k - 1; j < n; j++) {

        if (!fr) {

            fr = true;

            continue;

        }

        if (data[j] > data[j - k]) {

            printf("Yes\n");

        } else {

            printf("No\n");

        }

    }

}

// Faster C++ solution for the same problem (measured speedup: 9.43661898517692x):
#include <stdio.h>

int main(){

	int N, K;

	scanf("%d %d", &N, &K);

	int arr[N];

	for (int i = 0; i< N ;i++){

	scanf("%d", &arr[i]);



	if (i >= K){

		if (arr[i] > arr[i - K] ){

	    printf("Yes\n");

		}

		else {

		    printf("No\n");

		}

	}

}

	return 0;

}
// Slower C++ solution:
#include <stdio.h>



int main() {

    int n, k;

    scanf("%d %d", &n, &k);

    long long int data[200000] = {0};

    for (int i = 0; i < n; i++) {

        scanf("%d", &data[i]);

    }

    bool fr = false;

    for (int j = k - 1; j < n; j++) {

        if (!fr) {

            fr = true;

            continue;

        }

        if (data[j] > data[j - k]) {

            printf("Yes\n");

        } else {

            printf("No\n");

        }

    }

}

// Faster C++ solution for the same problem (measured speedup: 9.49464170806108x):
#include <stdio.h>



int main() {

  int t, k;

  scanf ("%d %d", &t, &k);

  int nilai[t];



  for (int i = 0; i < t; i++) {

    scanf("%d", &nilai[i]);

  }

    

  for (int i = 0; i < t-k; i++) {

    (nilai[i] < nilai[i+k]) ? puts("Yes") : puts("No");

  }

    

  return 0;

}
// Slower C++ solution:
#include <stdio.h>



int main(){

    int n, k;

    scanf("%d %d", &n, &k);

    int a[200001];

    for(int i = 0; i<n; i++){

        scanf("%d", &a[i]); 

    }

   

    for(int i = 0, j = k; i<n-k; i++, j++){

        if(a[j] > a[j-k]){

            printf("Yes\n");

        }

        else{

            printf("No\n");

        }

    }



    return 0;

}

// Faster C++ solution for the same problem (measured speedup: 1.1245541800756726x):
#include <stdio.h>



int main() {

  int t, k;

  scanf ("%d %d", &t, &k);

  int nilai[t];



  for (int i = 0; i < t; i++) {

    scanf("%d", &nilai[i]);

  }

    

  for (int i = 0; i < t-k; i++) {

    (nilai[i] < nilai[i+k]) ? puts("Yes") : puts("No");

  }

    

  return 0;

}
// Slower C++ solution:
#include<cstdio>

#include<algorithm>

#include<cstring>

#include<iostream>

#include<queue>

#include<cmath>

#include<math.h>

using namespace std;

int main(){

    long int a[200005],n,k;

    memset(a,0,sizeof(a));

    cin>>n>>k;

    for(long int i=0;i<n;i++){

    	cin>>a[i];

	}

    for(long int i=k;i<n;i++){

    	if(a[i]>a[i-k])cout<<"Yes"<<endl;

    	else cout<<"No"<<endl;

	}

    

    

    

    return 0;

}


// Faster C++ solution for the same problem (measured speedup: 13.779130872998172x):
#include <stdio.h>



int main() {

  int t, k;

  scanf ("%d %d", &t, &k);

  int nilai[t];



  for (int i = 0; i < t; i++) {

    scanf("%d", &nilai[i]);

  }

    

  for (int i = 0; i < t-k; i++) {

    (nilai[i] < nilai[i+k]) ? puts("Yes") : puts("No");

  }

    

  return 0;

}
// Slower C++ solution:
#include<stdio.h>

int main() {

 

	long long int n, k;

	scanf("%lld", &n);

	scanf("%lld", &k);

 

	long long int a[200000];

	for (long long int i = 0;i < n;i++)

	{

		scanf("%lld", &a[i]);

	}

	for (long long int j = 0;j < n - k;j++)

	{

		if (a[k + j] > a[j])

		{

			printf("Yes\n");

		}

		else

			printf("No\n");

	}

 

	return 0;

}

// Faster C++ solution for the same problem (measured speedup: 1.1991158851258845x):
#include<stdio.h>



int main(){

    

    int cases, term;

    scanf("%d %d", &cases, &term);

    int score[cases];

    for(int i = 0; i < cases; i++){

        scanf("%d", &score[i]);//terima input nilai

    }

    for(int i = term; i < cases; i++){

        if(score[i-term]<score[i])//jika lebih besar dari sebelumnya, maka ad perubahan

        {

            printf("Yes\n");

        }else//selain itu no

        {

            printf("No\n");

        }

    }

}
// Slower C++ solution:
#include<stdio.h>

int main() {

 

	long long int n, k;

	scanf("%lld", &n);

	scanf("%lld", &k);

 

	long long int a[200000];

	for (long long int i = 0;i < n;i++)

	{

		scanf("%lld", &a[i]);

	}

	for (long long int j = 0;j < n - k;j++)

	{

		if (a[k + j] > a[j])

		{

			printf("Yes\n");

		}

		else

			printf("No\n");

	}

 

	return 0;

}

// Faster C++ solution for the same problem (measured speedup: 1.2026959211348884x):
#include <stdio.h>



int main(){

    int n,k;

    scanf("%d %d", &n,&k);

    long long int input[n];

    for(int i=0;i<n;i++){

        scanf("%lld", &input[i]);

    }



    for(int i=0;i<n-k;i++){

        if(input[i]<input[i+k]){

            puts("Yes");

        } else {

            puts("No");

        }  

    }

    return 0;

}
// Slower C++ solution:
#include<stdio.h>

int main() {

 

	long long int n, k;

	scanf("%lld", &n);

	scanf("%lld", &k);

 

	long long int a[200000];

	for (long long int i = 0;i < n;i++)

	{

		scanf("%lld", &a[i]);

	}

	for (long long int j = 0;j < n - k;j++)

	{

		if (a[k + j] > a[j])

		{

			printf("Yes\n");

		}

		else

			printf("No\n");

	}

 

	return 0;

}

// Faster C++ solution for the same problem (measured speedup: 1.2026959211348884x):
#include<stdio.h>



int main () {

	int n, k;

	scanf("%d %d", &n, &k);

	long long int angka[n];



	for (int i = 0 ; i < n ; i++) {

		scanf("%lld", &angka[i]);

	}

	for(int i = 0 ; i < n-k ; i++) {

		if (angka[i] < angka[k+i]) {

			puts("Yes");

		} else {

			puts("No");

		}		

	}

	return 0;

}
// Slower C++ solution:
#include<stdio.h>

int main() {

 

	long long int n, k;

	scanf("%lld", &n);

	scanf("%lld", &k);

 

	long long int a[200000];

	for (long long int i = 0;i < n;i++)

	{

		scanf("%lld", &a[i]);

	}

	for (long long int j = 0;j < n - k;j++)

	{

		if (a[k + j] > a[j])

		{

			printf("Yes\n");

		}

		else

			printf("No\n");

	}

 

	return 0;

}

// Faster C++ solution for the same problem (measured speedup: 1.204456965010414x):
#include <stdio.h>

//sekolah budi dimana tahun dibagi menjadi N semester

//n semester,k yang ingin dibanding dari smester k sampai n

// 

int main(){

int n,k;

scanf("%d %d",&n,&k);

int nilai[n];//input skor uas sesuai semester yg diminta

for(int i = 0;i<n;i++){

	scanf("%d",&nilai[i]);

}

	for(int j=0;j<n-k;j++){

		if(nilai[j]<nilai[j+k]){

			puts("Yes");

		}

		else{

			puts("No");

		}

	}

	return 0;

}
// Slower C++ solution:
#include<stdio.h>

int main() {

 

	long long int n, k;

	scanf("%lld", &n);

	scanf("%lld", &k);

 

	long long int a[200000];

	for (long long int i = 0;i < n;i++)

	{

		scanf("%lld", &a[i]);

	}

	for (long long int j = 0;j < n - k;j++)

	{

		if (a[k + j] > a[j])

		{

			printf("Yes\n");

		}

		else

			printf("No\n");

	}

 

	return 0;

}

// Faster C++ solution for the same problem (measured speedup: 1.199542481978004x):
#include <stdio.h>



int main() {



	long long int N;

	long long int K;

	scanf("%lld %lld", &N, &K);

	

	long long int arr[N];

	for(int i=0; i<N; i++){

		scanf("%lld", &arr[i]);

	}

	

	for(long long int i=0, j=K; j<N; i++, j++){

		if(arr[j]>arr[i]){

			printf("Yes\n");

		}

		else {

			printf("No\n");

		}

	}



    return 0;

}
// Slower C++ solution:
#include<stdio.h>

int main() {

 

	long long int n, k;

	scanf("%lld", &n);

	scanf("%lld", &k);

 

	long long int a[200000];

	for (long long int i = 0;i < n;i++)

	{

		scanf("%lld", &a[i]);

	}

	for (long long int j = 0;j < n - k;j++)

	{

		if (a[k + j] > a[j])

		{

			printf("Yes\n");

		}

		else

			printf("No\n");

	}

 

	return 0;

}

// Faster C++ solution for the same problem (measured speedup: 1.1977451308929417x):
#include <stdio.h>



int main() {

    int n,k;

    scanf ("%d%d",&n,&k);

    long long int score[n];

    

    for (int i = 0; i < n; i++) {

        scanf("%lld", &score[i]);

    }

    

    for (int i = 0, j = k; i < n-k; i++,j++) {

        if (score[j-k] < score[j]) {

            printf ("Yes\n");

        }

        else if (score[j-k] >= score[j]) {

            printf ("No\n");

        }

    }

    return 0;

}
// Slower C++ solution:
#include<stdio.h>

int main() {

 

	long long int n, k;

	scanf("%lld", &n);

	scanf("%lld", &k);

 

	long long int a[200000];

	for (long long int i = 0;i < n;i++)

	{

		scanf("%lld", &a[i]);

	}

	for (long long int j = 0;j < n - k;j++)

	{

		if (a[k + j] > a[j])

		{

			printf("Yes\n");

		}

		else

			printf("No\n");

	}

 

	return 0;

}

// Faster C++ solution for the same problem (measured speedup: 1.204456965010414x):
#include<stdio.h>



#define ll long long



int main(){

	int n, k;

	scanf("%d %d", &n, &k);

	int arr[n];

	for(int i = 0; i < n; i++){

		scanf("%d", &arr[i]);

	}

	for(int j = 0; j < n - k; j++){

//		printf("compare %d < %d (?)\n", arr[j], arr[j + k]);

		if(arr[j] < arr[j + k]){

			puts("Yes");

		}

		else{

			puts("No");

		}

	}

	return 0;

}
// Slower C++ solution:
#include <stdio.h>



int main() {

    int i, x, y, a[200001];

    long long int tmp;



    scanf("%d%d", &x, &y);

    

    for (i = 0; i < x; i++) {

        scanf("%d", a + i);

    }



    for (i = y; i < x; i++) {

        if (a[i] >a[i - y]) {

            printf("Yes\n");

        } else {

            printf("No\n");

        }

    }

    

}

// Faster C++ solution for the same problem (measured speedup: 1.1238995290418345x):
//1 tahun dibagi N semester

//tiap akhir semester ada ujian

//hasilnya dikasi grade gt kan



//utk tiap semester K-1, gadikasi grade

//utk tiap semester K hingga N

//hasil ujian sebelumnnya di semester ke K dan baik itu dikasi nilai atau tidak

//dikalikan dengan nilai ujian N



//M dapet Ai di akhir semester ke i (A itu nilai) di semester i gt

//dengan i >= K+1, kalo ga dikasi grade ga dianggep (K-1 gadikasi grade mksdnya) ini kan dikasi

//dan i <= N, kan tiap N ujiannya (gmn ada nilai kl gaada ujian)



//tentukan nilai dia utk semester ke i lebih gede tidak dengan hasil semester i-1 (sebelumnya





//BANDINGINNYA PAKE ARRAY, yatuhan



#include <stdio.h>

int main(){

	int N, K;

	scanf("%d %d", &N, &K);



	int ujian[N+1];



	for(int i =0; i<N; i++)

	{

		scanf("%d", &ujian[i+1]);	//masukin sampe arraynya full

	}



	for(int i= K+1; i<= N; i++)	//selama i memenuhi, kyk di soal

	{

		if(ujian[i] > ujian[i-K])	//selama hasil ujian ke i (kayak Ai disoal) lebih dari yg sebelumnya

		{

			puts("Yes");

		}

		else puts("No");

	}



	return 0;

}
// Slower C++ solution:
#include <stdio.h>



int main() {

    int i, x, y, a[200001];

    long long int tmp;



    scanf("%d%d", &x, &y);

    

    for (i = 0; i < x; i++) {

        scanf("%d", a + i);

    }



    for (i = y; i < x; i++) {

        if (a[i] >a[i - y]) {

            printf("Yes\n");

        } else {

            printf("No\n");

        }

    }

    

}

// Faster C++ solution for the same problem (measured speedup: 1.1226679072421617x):
#include <stdio.h>



int main (){

	int n, k;

	scanf ("%d %d", &n, &k);

	int arr[n];

	for (int i = 0; i < n; i++){

		scanf ("%d", &arr[i]);

		if (i >= k){

//			printf ("Nilai Semester ke-i: %d\n", arr[i]);

//			printf ("Nilai Semester ke0(i - k) : %d\n", arr[i - k]);

			arr[i] > arr[i - k] ? puts("Yes") : puts("No");

		}

	}

	

	

	

	return 0;

}
// Slower C++ solution:
#include <bits/stdc++.h>

using namespace std;

#define mp make_pair

#define pb push_back



typedef pair<int, int> ii;

typedef long long ll;

typedef pair<ll, ll> l4;



const int maxn = 1e6+1;

int n, m, perm[maxn], p[maxn];

int pa(int id)

{

  return id==p[id]?id:p[id]=pa(p[id]);

}

void merge(int x, int y)

{

  p[pa(x)] = pa(y);

}



int main()

{

  scanf("%d", &n);

  scanf("%d", &m);

  for (int i = 1; i <= n; ++i)

    {

      scanf("%d", perm+i);

      p[i] = i;

    }

  for (int i = 1; i <= m; ++i)

    {

      int x, y;

      scanf("%d %d", &x, &y);

      merge(x, y);

    }

  int ans = 0;

  for (int i = 1; i <= n; ++i)

    ans += pa(i) == pa(perm[i]);

  printf("%d\n", ans);

}






// Faster C++ solution for the same problem (measured speedup: 5.136539585309199x):
#include<stdio.h>

const int MAXL = 1e5 + 10;

int Tree[MAXL];

int findRoot(int x) {

	if (Tree[x] == x) return x;

	else return Tree[x] = findRoot(Tree[x]);

}



int list[MAXL];

int belong[MAXL];



int main() {

	int n, m;

	while (scanf("%d%d", &n, &m) != EOF) {

		for (int i = 1; i <= n; i++) {

			scanf("%d", &list[i]);

			Tree[i] = i;

		}

		while (m--) {

			int a, b;

			scanf("%d%d", &a, &b);

			a = findRoot(a);

			b = findRoot(b);

			if (a != b) {

				Tree[a] = b;

			}

		}



		for (int i = 1; i <= n; i++) {

			belong[list[i]] = findRoot(i);

		}

		int ans = 0;

		for (int i = 1; i <= n; i++) {

			if (belong[i] == findRoot(i))

				ans++;

		}

		printf("%d\n", ans);

	}

	return 0;

}
// Slower C++ solution:
#include<iostream>  

#include<algorithm>  

#include<cstring>  

#include<cstdio>  

  

using namespace std;



const int inf = 0x3f3f3f3f;



struct node

{

    int u,v;

};

node e[200050];

int head[100050],vis[100050],tr[100050],a[100050];

int cnt;

int n,m;



void addedge(int u,int v)

{

    e[cnt].v=v;

    e[cnt].u=head[u];

    head[u]=cnt++;

}



int findx(int x)

{

    if(tr[x]==x) return x;

    return tr[x]=findx(tr[x]);

}

void merge(int u, int v)  

{

    int fx=findx(u);

    int fy=findx(v);

    if(tr[fy]!=fx)

        tr[fy]=fx;

}

void dfs(int u)

{  

    vis[u]=1;  

    for(int i=head[u];i!=-1;i=e[i].u)

    {  

        int v=e[i].v;

        if(!vis[v])

        {

            tr[v]=cnt;

            dfs(v);

        }

    }

}

void init()

{

    cnt=0;

    memset(head,-1,sizeof(head));

    memset(vis,0,sizeof(vis));

    for(int i=0;i<n;i++) tr[i]=i;

}

int main()  

{

    init();

    scanf("%d%d",&n,&m); 

    for(int i=1;i<=n;++i)

        scanf("%d",&a[i]);

    for(int i=0;i<m;++i)

    {

        int x,y;

        scanf("%d%d",&x,&y);

        addedge(x,y);

        addedge(y,x);

    }

    int ans=0;

    cnt=1;

    for(int i=1;i<=n;++i)

    {

        if(!vis[i])

        {

            tr[i]=cnt;

            dfs(i);

            cnt++;

        }

        if(tr[a[i]]==tr[i])

            ans++;

    }

    printf("%d\n",ans);

    return 0;

}

// Faster C++ solution for the same problem (measured speedup: 1.8178809881761506x):
#include<cstdio>

#include<cstring>

#include<cmath>

#include<algorithm>

#include<queue>

#include<stack>

#include<vector>

#include<set>

#include<map>

#include<iostream>

using namespace std;

int n,m,a[100005],f[100005],x,y,ans;

int find(int k){

	if(f[k]!=k) f[k]=find(f[k]);

	return f[k];

}

void pa(int a,int b){

	int t1=find(a),t2=find(b);

	if(t1!=t2) f[t2]=t1;

}

int main(){

	scanf("%d%d",&n,&m);

	for(int i=1;i<=n;i++){

		f[i]=i;

		scanf("%d",&a[i]);

	}

	for(int i=1;i<=m;i++){

		scanf("%d%d",&x,&y);

		pa(x,y);

	}

	for(int i=1;i<=n;i++){

		if(find(i)==find(a[i]))ans++;

	}

	printf("%d",ans);

	return 0;

}