stablegradients/pie-gem5-bysrc huggingface dataset:
// Original C++ solution (slower):
#include <bits/stdc++.h>

using namespace std;

int n,m,ans;

int main(){

    cin>>n>>m;

    for(int i=m/n;i>=1;i--){

        if(m%i==0){

            int g=m/i;

            if(g>=n){

            	cout<<i<<endl;

            	return 0;

			}

        }

    }

    return 0;

}

// Optimized solution to the same problem (faster):
#include <stdio.h>

#include <math.h>



int d[20002], e[20002], c = 0;



int main()

{	 

	int n, m, i, k, j;

	

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

	for(i = 1, k = sqrt(m); i <= k; i++){

		if(m % i == 0){

			d[c] = i;

			e[c++] = m / i;

		}

	}

	for(i = c - 1; i >= 0; i--){

		d[c] = e[i], e[c] = d[i], c++;

	}

	for(i = c - 1; i >= 0 && e[i] < n; i--);

	printf("%d\n", d[i]);

		

	return 0;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include <iostream>

using namespace std;

int main()

{

    long long n, m, ans = 0;

    cin >> n >> m;

    for(long long i = 1; i * i <= m; i++)

    {

        if(m % i == 0)

        {

            if(m >= n * i)

            {

                ans = max(ans, i);

            }

            if(m >= n * (m / i))

            {

                ans = max(ans, m / i);

            }

        }

    }

    cout << ans << endl;

    return 0;

}

// Optimized solution to the same problem (faster):
#include <stdio.h>

#include <math.h>



int d[20002], e[20002], c = 0;



int main()

{	 

	int n, m, i, k, j;

	

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

	for(i = 1, k = sqrt(m); i <= k; i++){

		if(m % i == 0){

			d[c] = i;

			e[c++] = m / i;

		}

	}

	for(i = c - 1; i >= 0; i--){

		d[c] = e[i], e[c] = d[i], c++;

	}

	for(i = c - 1; i >= 0 && e[i] < n; i--);

	printf("%d\n", d[i]);

		

	return 0;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include<iostream>

#include<vector>

#include<string>

#include<cmath>

#include<algorithm>

#include <numeric>

 

using namespace std;

using ll=long long;

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

#define ALL(a) (a).begin(),(a).end()



int main(){

    ll n, m;

    cin >> n >> m;



    if(m%n == 0){

        cout << m/n << endl;

        return 0;

    }

    ll mx = (ll)m/n;



    // vector<ll> div;

    ll ans = 1;

    for(ll i = 2; i <= m/n; i++){

        if(m%i == 0)ans = i;

    }

    cout << ans << endl;



}

// Optimized solution to the same problem (faster):
#include <bits/stdc++.h>

 

#define rep(i,n) for(ll i=0;i<n;++i)

#define all_map(itr,mp) for(auto itr=mp.begin();itr!=mp.end();itr++)

#define ALL(a) (a).begin(),(a).end()



using namespace std;

using ll  = long long;

using pll = pair<ll, ll>;



const ll MOD  = 1e9 + 7;

const ll LINF = 1LL << 62;

const int INF = 1e9 + 7;



int main(){

    ll n, m;

    cin >> n >> m;



    for(ll i = m/n; i >= 1; --i){

        ll dif = m - i*n;

        if(dif % i == 0){

            cout << i << endl;

            return 0;

        }

    }

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include<stdio.h>

#include<string.h>

int dp[1000005][2],num[1000005]={0};

char tmp[1000005];

int min(int a,int b){

	return a<b?a:b;

}

int main()

{

	int len,ans;

    for(int i=0;i<1000005;i++)for(int j=0;j<2;j++)dp[i][j]=99;

    scanf("%s",tmp);

    len=strlen(tmp);

    for(int i=1;i<=len;i++)num[i]=tmp[i-1]-'0';

    dp[len][0]=num[len];dp[len][1]=10-num[len];

    for(int i=len-1;i>=0;i--)

    {

    	/*数位*/

    	dp[i][0]=min(dp[i+1][0]+num[i],dp[i+1][1]+num[i]+1),

        dp[i][1]=min(10+dp[i+1][0]-num[i],10+dp[i+1][1]-(num[i]+1));

	}

	ans=min(dp[0][0],dp[0][1]);

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

}

// Optimized solution to the same problem (faster):
#include <cstdio>

#include <iostream>

#include <algorithm>

#include <cstring>

#include <queue> //priority_queue 优先队列

#include <map>

#include <set> //multiset  set<int>::reverse_iterator rit; for(rit=s.rbegin();rit!=s.rend();rit++)

#include <vector>

#include <stack>

#include <cmath>

#include <utility>

#include <sstream>

#include <string>

#define mod 1000000007

#define mod9 998244353

typedef unsigned long long ull;

typedef long long ll;

typedef double db;

const db eps=1e-6;

const int INF = 0x3f3f3f3f;

const ll inf=0x3f3f3f3f3f3f3f3f;

const int N=1e6+8;

inline ll read(){ll s=0,w=1;char ch=getchar();while(ch<'0'||ch>'9'){if(ch=='-')w=-1;ch=getchar();}while(ch>='0'&&ch<='9') s=s*10+ch-'0',ch=getchar();return s*w;}

using namespace std;



int t,n,m,k,ans=0,cnt=0;

char a[N]={0};

int b[N]={0};

int main(){

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

    int len=strlen(a+1);

    for(int i=1;i<=len;i++) b[i]=a[i]-'0';

    for(int i=len;i>=0;i--){

        int sum=b[i];

        int pre=b[i-1];

        if(sum<5){

            ans+=sum;

        }else if(sum>5){

            ans+=10-sum;

            b[i-1]++;

        }else if(pre>=5){

            ans+=10-sum;

            b[i-1]++;

        }else{

            ans+=sum;

        }

    }

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

    return 0;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include <iostream>

using namespace std;

bool carry = false;

int main() {

  ios::sync_with_stdio(0);

  cin.tie(0);

  string S;

  cin >> S;

  int N = S.size();

  int ans = 0;

  for (int i = N; i >= 0; i--) {

    int n = S[i] - '0';

    if (carry) n++;

    switch(n){

    case 5:

      ans+=5;

      break;

    case 0:

    case 10:

      break;

    case 1:

    case 9:

      ans+=1;

      break;

    case 2:

    case 8:

      ans+=2;

      break;

    case 3:

    case 7:

      ans+=3;

      break;

    case 4:

    case 6:

      ans+=4;

      break;

    }

    carry = (n > 5 || (n == 5 && i > 0 && S[i-1] > '4'));

  }

  if (carry) ans++;

  cout << ans << '\n';

  return 0;

}

 

// Optimized solution to the same problem (faster):
#include<cstdio>

using namespace std;

int main() {

  int flag = 1;

  int flag2 = 1;

  int ans = 0;

  int c;

  int n;

  while ((c = getchar_unlocked()) >= '0') {

    n = c - '0';

    flag2 = flag;

    if (n == 5) {

      if (flag == 1) {

        flag = 0;

        ans += 5;

      } else {

        flag = -1;

        ans += 4;

      }

    } else if (n < 4) {

      if (flag == -1) {

        ans += n + 1;

      } else {

        ans += n;

      }

      flag = 1;

    } else if (n > 5) {

      if (flag == 1) {

        ans += 10 - n;

      } else {

        ans += 9 - n;

      }

      flag = -1;

    } else {

      if (flag == -1) {

        flag = 0;

        ans += 5;

      } else {

        flag = 1;

        ans += 4;

      }

    }

  }

  if (n > 5 || (n == 5 && flag2 != 1)){

    ++ans;

  }

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

  return 0;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include <iostream>

using namespace std;

bool carry = false;

int main() {

  ios::sync_with_stdio(0);

  cin.tie(0);

  string S;

  cin >> S;

  int N = S.size();

  int ans = 0;

  int flag = 1;

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

    int n = S[i] - '0';

    if (n < 4) {

      if (flag == -1) {

        ans += n + 1;

      } else {

        ans += n;

      }

      flag = 1;

    }

    else if (n > 5) {

      if (flag == 1) {

        ans += 10 - n;

      }

      else {

        ans += 9 - n;

      }

      flag = -1;

    } else if(n == 4) {

      if (flag == -1) {

        flag = 0;

        ans += 5;

      }

      else {

        flag = 1;

        ans += 4;

      }

    } else if (n == 5) {

      if (flag == 1) {

        flag = 0;

        ans += 5;

      } else {

        flag = -1;

        ans += 4;

      }

    }

  }

  int last = S[N - 1] - '0';

  if(last <= 4) {

    if(flag == -1) {

      ans += last + 1;

    } else {

      ans += last;

    }

  } else if (last == 5) {

    ans += 5;

  } else {

    if(flag == 1) {

      ans += 11 - last;

    } else {

      ans += 10 - last;

    }

  }

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

  return 0;

}


// Optimized solution to the same problem (faster):
#include<cstdio>

using namespace std;

int main() {

  int flag = 1;

  int flag2 = 1;

  int ans = 0;

  int c;

  int n;

  while ((c = getchar_unlocked()) >= '0') {

    n = c - '0';

    flag2 = flag;

    if (n == 5) {

      if (flag == 1) {

        flag = 0;

        ans += 5;

      } else {

        flag = -1;

        ans += 4;

      }

    } else if (n < 4) {

      if (flag == -1) {

        ans += n + 1;

      } else {

        ans += n;

      }

      flag = 1;

    } else if (n > 5) {

      if (flag == 1) {

        ans += 10 - n;

      } else {

        ans += 9 - n;

      }

      flag = -1;

    } else {

      if (flag == -1) {

        flag = 0;

        ans += 5;

      } else {

        flag = 1;

        ans += 4;

      }

    }

  }

  if (n > 5 || (n == 5 && flag2 != 1)){

    ++ans;

  }

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

  return 0;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#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;

}

// Optimized solution to the same problem (faster):
#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;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#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");

        }

    }

}

// Optimized solution to the same problem (faster):
#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;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#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;

}

// Optimized solution to the same problem (faster):
#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;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#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;

}


// Optimized solution to the same problem (faster):
#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;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#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;

}

// Optimized solution to the same problem (faster):
#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;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#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");

        }

    }

    

}

// Optimized solution to the same problem (faster):
//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;

}


// Fastest known solution for this problem:
// Original C++ solution (slower):
#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);

}






// Optimized solution to the same problem (faster):
#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;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#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;

}

// Optimized solution to the same problem (faster):
#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;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
/// However long the night,

///     The dawn will break

/// ICPC next year



#include<bits/stdc++.h>

using namespace std ;

const int N = 1e5+5 ;



int n ,m ,arr[N] ,u ,v ;

vector<int> adj[N] ;

bool vis[N] ;

int vid ,cur[N] ;

vector<int> idx ;

void dfs(int p){

    vis[p] = 1 ;

    cur[arr[p]] = vid ;

    idx.push_back(p);

    for(int ch:adj[p]){

        if(vis[ch]) continue;

        dfs(ch) ;

    }

}

int main(){

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

    for(int i=1;i<=n;++i) scanf("%d",arr+i);

    while(m--){

        scanf("%d%d",&u,&v);

        adj[u].push_back(v);

        adj[v].push_back(u);

    }

    int ans = 0 ;

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

        if(vis[i]) continue ;

        idx.clear() ;

        ++vid ;

        dfs(i) ;

        for(int j:idx)

            if(cur[j]==vid) ++ans ;

    }

    cout << ans ;

    return 0;

}


// Optimized solution to the same problem (faster):
#include <bits/stdc++.h>

using namespace std;

const int N = 1e5 + 25, M = 1e6 + 25, oo = 1e9+7, mxmsk = 1 << 16;

const long long OO = 1e18+25;

    

#define f first

#define s second

    

int n,m,a[N], x,y, dsu[N], cnt;

    

int findparent(int u)

{

    if(u == dsu[u]) return u;

    return dsu[u] = findparent(dsu[u]);

}



void merge(int u, int v)

{

    u = findparent(u);

    v = findparent(v);

    dsu[u] = v;

}

    

int main() {

    iota(dsu, dsu+N, 0);

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

    for(int i=1; i<=n ; i++) scanf("%d",a+i);

    while(m--)

    {

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

        merge(x,y);

    }

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

    {

        cnt += (findparent(i) == findparent(a[i]));

    }

    printf("%d",cnt);

    return 0;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include<bits/stdc++.h>

#define LL long long

#define inf 0x3fffffffffffffff

using namespace std;



int n,m,p[100010],v[100010];

vector<int> e[100010];

set<int> id,pe;



void dfs(int x)

{

    v[x]=1;

    id.insert(x);

    pe.insert(p[x]);

    for (auto y:e[x])

        if (!v[y]) dfs(y);

}



int main()

{

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

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

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

    int x,y;

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

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

        e[x].push_back(y),

        e[y].push_back(x);

    memset(v,0,sizeof(v));

    int ans=0;

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

        if (!v[i])

        {

            id.clear();pe.clear();

            dfs(i);

            for (auto x:id)

                if (pe.find(x)!=pe.end()) ans++;

        }

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

    return 0;

}

// Optimized solution to the same problem (faster):
#include <bits/stdc++.h>

using namespace std;

const int N = 1e5 + 25, M = 1e6 + 25, oo = 1e9+7, mxmsk = 1 << 16;

const long long OO = 1e18+25;

    

#define f first

#define s second

    

int n,m,a[N], x,y, dsu[N], cnt;

    

int findparent(int u)

{

    if(u == dsu[u]) return u;

    return dsu[u] = findparent(dsu[u]);

}



void merge(int u, int v)

{

    u = findparent(u);

    v = findparent(v);

    dsu[u] = v;

}

    

int main() {

    iota(dsu, dsu+N, 0);

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

    for(int i=1; i<=n ; i++) scanf("%d",a+i);

    while(m--)

    {

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

        merge(x,y);

    }

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

    {

        cnt += (findparent(i) == findparent(a[i]));

    }

    printf("%d",cnt);

    return 0;

}

// Fastest known solution for this problem: