Language: C++

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

typedef long long LL;

#define __m int mid=(l+r)>>1;
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1

#define rep(i,a,b) for(register int i = (a); i< (b); i++)
#define per(i,a,b) for(register int i = (a); i> (b); i--)
#define mem(a) memset(a,0,sizeof(a));
#define pb push_back

#ifdef LOCAL
    #define bug(x) cout<<#x<<" : "<<x<<endl;
#else
    #define bug(x) ;
#endif

template<class T>
inline bool read(T &num)  
{
       char in;bool IsN=false;
       while(in=getchar(),in!='-'&&(in<'0'||in>'9'))if(in==EOF)return false;
       in=='-'?(IsN=true,num=0):num=in-'0';
       while(in=getchar(),in>='0'&&in<='9')num=num*10+in-'0';
       return IsN?num=-num:num,true;
}

const int maxn = 1e5+7;
const int inf  = 2e9+7;
const int mod  = 1e9+7;

int fa[maxn],num[maxn];
void init(){
    rep(i,0,maxn)
        fa[i]=i;
}
int find(int x){
    return fa[x]==x?x:find(fa[x]);
}
void merge(int x,int y){
    int fx=find(x),fy=find(y);
    if(fx==fy)
        return ;
    fa[fx]=fy;
}
int main(){
#ifdef LOCAL
    //freopen("sample.in","r",stdin);
#endif
    ios::sync_with_stdio(false);
    int n,m;
    cin>>n>>m;
    rep(i,1,n+1)cin>>num[i];
    init();
    while(m--){
        int a,b;
        cin>>a>>b;
        merge(a,b);
    }
    rep(i,1,n+1){
        while(find(i)==find(num[i])){
            if(num[i]==i)
                break;
            swap(num[i],num[num[i]]);
        }
    }
    int ans = 0;
    rep(i,1,n+1)if(num[i]==i)ans++;
    cout<<ans<<endl;
    return 0;
}
#include<bits/stdc++.h>
using namespace std;

typedef long long LL;

#define __m int mid=(l+r)>>1;
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1

#define rep(i,a,b) for(register int i = (a); i< (b); i++)
#define per(i,a,b) for(register int i = (a); i> (b); i--)
#define mem(a) memset(a,0,sizeof(a));
#define pb push_back

#ifdef LOCAL
    #define bug(x) cout<<#x<<" : "<<x<<endl;
#else
    #define bug(x) ;
#endif

template<class T>
inline bool read(T &num)  
{
       char in;bool IsN=false;
       while(in=getchar(),in!='-'&&(in<'0'||in>'9'))if(in==EOF)return false;
       in=='-'?(IsN=true,num=0):num=in-'0';
       while(in=getchar(),in>='0'&&in<='9')num=num*10+in-'0';
       return IsN?num=-num:num,true;
}

const int maxn = 1e5+7;
const int inf  = 2e9+7;
const int mod  = 1e9+7;

int fa[maxn],num[maxn];
void init(){
    rep(i,0,maxn)
        fa[i]=i;
}
int find(int x){
    return fa[x]==x?x:find(fa[x]);
}
void merge(int x,int y){
    int fx=find(x),fy=find(y);
    if(fx==fy)
        return ;
    fa[fx]=fy;
}
int main(){
#ifdef LOCAL
    //freopen("sample.in","r",stdin);
#endif
    ios::sync_with_stdio(false);
    int n,m;
    cin>>n>>m;
    rep(i,1,n+1)cin>>num[i];
    init();
    while(m--){
        int a,b;
        cin>>a>>b;
        merge(a,b);
    }
    rep(i,1,n+1){
        while(find(i)==find(num[i])){
            if(num[i]==i)
                break;
            swap(num[i],num[num[i]]);
        }
    }
    int ans = 0;
    rep(i,1,n+1)if(num[i]==i)ans++;
    cout<<ans<<endl;
    return 0;
}


Compiler output:
a.cc: In function 'void init()':
a.cc:37:9: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
   37 |     rep(i,0,maxn)
      |         ^
a.cc:10:37: note: in definition of macro 'rep'
   10 | #define rep(i,a,b) for(register int i = (a); i< (b); i++)
      |                                     ^
a.cc: In function 'int main()':
a.cc:56:9: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
   56 |     rep(i,1,n+1)cin>>num[i];
      |         ^
a.cc:10:37: note: in definition of macro 'rep'
   10 | #define rep(i,a,b) for(register int i = (a); i< (b); i++)
      |                                     ^
a.cc:63:9: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
   63 |     rep(i,1,n+1){
      |         ^
a.cc:10:37: note: in definition of macro 'rep'
   10 | #define rep(i,a,b) for(register int i = (a); i< (b); i++)
      |                                     ^
a.cc:71:9: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
   71 |     rep(i,1,n+1)if(num[i]==i)ans++;
      |         ^
a.cc:10:37: note: in definition of macro 'rep'
   10 | #define rep(i,a,b) for(register int i = (a); i< (b); i++)
      |                                     ^
a.cc: At global scope:
a.cc:96:13: error: redefinition of 'template<class T> bool read(T&)'
   96 | inline bool read(T &num)
      |             ^~~~
a.cc:22:13: note: 'template<class T> bool read(T&)' previously declared here
   22 | inline bool read(T &num)
      |             ^~~~
a.cc:105:11: error: redefinition of 'const int maxn'
  105 | const int maxn = 1e5+7;
      |           ^~~~
a.cc:31:11: note: 'const int maxn' previously defined here
   31 | const int maxn = 1e5+7;
      |           ^~~~
a.cc:106:11: error: redefinition of 'const int inf'
  106 | const int inf  = 2e9+7;
      |           ^~~
a.cc:32:11: note: 'const int inf' previously defined here
   32 | const int inf  = 2e9+7;
      |           ^~~
a.cc:107:11: error: redefinition of 'const int mod'
  107 | const int mod  = 1e9+7;
      |           ^~~
a.cc:33:11: note: 'const int mod' previously defined here
   33 | const int mod  = 1e9+7;
      |           ^~~
a.cc:109:5: error: redefinition of 'int fa [100007]'
  109 | int fa[maxn],num[maxn];
      |     ^~
a.cc:35:5: note: 'int fa [100007]' previously declared here
   35 | int fa[maxn],num[maxn];
      |     ^~
a.cc:109:14: error: redefinition of 'int num [100007]'
  109 | int fa[maxn],num[maxn];
      |              ^~~
a.cc:35:14: note: 'int num [100007]' previously declared here
   35 | int fa[maxn],num[maxn];
      |              ^~~
a.cc:110:6: error: redefinition of 'void init()'
  110 | void init(){
      |      ^~~~
a.cc:36:6: note: 'void init()' previously defined here
   36 | void init(){
      |      ^~~~
a.cc: In function 'void init()':
a.cc:111:9: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
  111 |     rep(i,0,maxn)
      |         ^
a.cc:84:37: note: in definition of macro 'rep'
   84 | #define rep(i,a,b) for(register int i = (a); i< (b); i++)
      |                                     ^
a.cc: At global scope:
a.cc:114:5: error: redefinition of 'int find(int)'
  114 | int find(int x){
      |     ^~~~
a.cc:40:5: note: 'int find(int)' previously defined here
   40 | int find(int x){
      |     ^~~~
a.cc:117:6: error: redefinition of 'void merge(int, int)'
  117 | void merge(int x,int y){
      |      ^~~~~
a.cc:43:6: note: 'void merge(int, int)' previously defined here
   43 | void merge(int x,int y){
      |      ^~~~~
a.cc:123:5: error: redefinition of 'int main()'
  123 | int main(){
      |     ^~~~
a.cc:49:5: note: 'int main()' previously defined here
   49 | int main(){
      |     ^~~~
a.cc: In function 'int main()':
a.cc:130:9: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
  130 |     rep(i,1,n+1)cin>>num[i];
      |         ^
a.cc:84:37: note: in definition of macro 'rep'
   84 | #define rep(i,a,b) for(register int i = (a); i< (b); i++)
      |                                     ^
a.cc:137:9: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
  137 |     rep(i,1,n+1){
      |         ^
a.cc:84:37: note: in definition of macro 'rep'
   84 | #define rep(i,a,b) for(register int i = (a); i< (b); i++)
      |                                     ^
a.cc:145:9: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
  145 |     rep(i,1,n+1)if(num[i]==i)ans++;
      |         ^
a.cc:84:37: note: in definition of macro 'rep'
   84 | #define rep(i,a,b) for(register int i = (a); i< (b); i++)
      |                                     ^