Submission #2500955


Source Code Expand

Copy
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <bitset>
#include <cmath>
#include <string>
#define FI first
#define SE second
#define PF push_front
#define PB push_back
#define PPF pop_front
#define PPB pop_back
#define MA make_pair
#define ll long long
#define PA pair<int,int>
#define VE vector<int>
#define VP vector<PA>
#define FOR(i,a,b) for(int i=a;i<b;i++)
#define ROF(i,a,b) for(int i=b-1;i>=a;i--)
#define YES(i) cout<<(i?"YES":"NO")<<endl
#define Yes(i) cout<<(i?"Yes":"No")<<endl
using namespace std;
//
const int INF=1e9+7;
const int mod=1e9+7;
//
//
struct poi{
  int X;int Y;int Z;
  bool operator<(const poi&R)const{
    return X==R.X ? Y==R.Y ? Z<R.Z : Y<R.Y : X<R.X;
  }
};
//
//
int N,M;
int P[100000];
VE V[100000];
int f[100000];
void DFS(int now,int mem){
	FOR(i,0,(int)V[now].size()){
		if(f[V[now][i]]!=0){
			continue;
		}else{
			f[V[now][i]]=mem;
			DFS(V[now][i],mem);
		}
	}
}
int main(){
	cin>>N>>M;
	FOR(i,0,N){
		cin>>P[i];
		P[i]--;
	}
	FOR(i,0,M){
		int X,Y;
		cin>>X>>Y;
		X--;Y--;
		V[X].PB(Y);
		V[Y].PB(X);
	}
	int mem=1;
	FOR(i,0,N){
		if(f[i]==0){
			DFS(i,mem);
			mem++;
		}
	}
	int ans=0;
	FOR(i,0,N){
		if(f[i]!=0&&f[P[i]]==f[i]){
			ans++;
		}else if(i==P[i]){
			ans++;
		}
	}
	cout<<ans<<endl;
	return 0;
}

Submission Info

Submission Time
Task D - Equals
User nii
Language C++14 (GCC 5.4.1)
Score 400
Code Size 1388 Byte
Status
Exec Time 137 ms
Memory 9472 KB

Judge Result

Set Name Score / Max Score Test Cases
Sample 0 / 0 0_000.txt, 0_001.txt, 0_002.txt, 0_003.txt
All 400 / 400 0_000.txt, 0_001.txt, 0_002.txt, 0_003.txt, 1_004.txt, 1_005.txt, 1_006.txt, 1_007.txt, 1_008.txt, 1_009.txt, 1_010.txt, 1_011.txt, 1_012.txt, 1_013.txt, 1_014.txt, 1_015.txt, 1_016.txt, 1_017.txt, 1_018.txt, 1_019.txt, 1_020.txt, 1_021.txt, 1_022.txt
Case Name Status Exec Time Memory
0_000.txt 3 ms 2560 KB
0_001.txt 3 ms 2560 KB
0_002.txt 3 ms 2560 KB
0_003.txt 3 ms 2560 KB
1_004.txt 55 ms 3584 KB
1_005.txt 108 ms 4352 KB
1_006.txt 136 ms 9472 KB
1_007.txt 3 ms 2560 KB
1_008.txt 3 ms 2560 KB
1_009.txt 3 ms 2560 KB
1_010.txt 3 ms 2560 KB
1_011.txt 3 ms 2560 KB
1_012.txt 3 ms 2560 KB
1_013.txt 3 ms 2560 KB
1_014.txt 5 ms 2688 KB
1_015.txt 3 ms 2560 KB
1_016.txt 3 ms 2560 KB
1_017.txt 4 ms 2688 KB
1_018.txt 51 ms 3712 KB
1_019.txt 38 ms 2944 KB
1_020.txt 38 ms 3328 KB
1_021.txt 40 ms 3456 KB
1_022.txt 137 ms 7424 KB