Submission #3951632
Source Code Expand
#include <iostream>
#include <string>
#include <vector>
#include <cstdlib>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <map>
#include <stack>
#include <queue>
#include <set>
#include <cstring>
using namespace std;
// ascending order
#define vsort(v) sort(v.begin(), v.end())
// descending order
#define vsort_r(v) sort(v.begin(), v.end(), greater<int>())
#define vunique(v) unique(v.begin(), v.end())
#define mp make_pair
#define ts(x) to_string(x)
#define rep(i, a, b) for(int i = (int)a; i < (int)b; i++)
#define repm(i, a, b) for(int i = (int)a; i > (int)b; i--)
#define bit(a) bitset<8>(a)
#define des_priority_queue priority_queue<int, vector<int>, greater<int> >
typedef long long ll;
typedef pair<int, int> P;
const ll INF = 1e18;
#define MAX_N 10000
int par[MAX_N]; // 親
int depth[MAX_N]; // 木の深さ
// n要素で初期化
void init(int n) {
rep(i, 0, n) {
par[i] = i;
depth[i] = 0;
}
}
// 木の根を求める
int find(int x) {
if(par[x] == x) return x;
else return par[x] = find(par[x]);
}
// xとyの属する集合を併合
void unite(int x, int y) {
x = find(x);
y = find(y);
if(x == y) return;
if(depth[x] < depth[y]) {
par[x] = y;
} else {
par[y] = x;
if(depth[x] == depth[y]) depth[x]++;
}
}
// xとyが同じ集合に属するか否か
bool same(int x, int y) {
return find(x) == find(y);
}
int main(){
cin.tie(0);
ios::sync_with_stdio(false);
int n, m;
cin >> n >> m;
int p[n];
rep(i, 0, n) {
cin >> p[i];
p[i]--;
}
init(n);
rep(i, 0, m) {
int x, y;
cin >> x >> y;
unite(x, y);
}
int cnt = 0;
rep(i, 0, n) {
if(same(p[i], i)) cnt++;
}
cout << cnt << endl;
}
Submission Info
Submission Time |
|
Task |
D - Equals |
User |
tsutarou10 |
Language |
C++14 (GCC 5.4.1) |
Score |
0 |
Code Size |
1767 Byte |
Status |
RE |
Exec Time |
107 ms |
Memory |
768 KB |
Judge Result
Set Name |
Sample |
All |
Score / Max Score |
0 / 0 |
0 / 400 |
Status |
|
|
Set Name |
Test Cases |
Sample |
0_000.txt, 0_001.txt, 0_002.txt, 0_003.txt |
All |
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 |
AC |
1 ms |
256 KB |
0_001.txt |
AC |
1 ms |
256 KB |
0_002.txt |
AC |
1 ms |
256 KB |
0_003.txt |
AC |
1 ms |
256 KB |
1_004.txt |
AC |
15 ms |
256 KB |
1_005.txt |
RE |
106 ms |
768 KB |
1_006.txt |
RE |
106 ms |
768 KB |
1_007.txt |
AC |
1 ms |
256 KB |
1_008.txt |
AC |
1 ms |
256 KB |
1_009.txt |
AC |
1 ms |
256 KB |
1_010.txt |
AC |
1 ms |
256 KB |
1_011.txt |
AC |
1 ms |
256 KB |
1_012.txt |
WA |
1 ms |
256 KB |
1_013.txt |
AC |
1 ms |
256 KB |
1_014.txt |
AC |
2 ms |
256 KB |
1_015.txt |
AC |
1 ms |
256 KB |
1_016.txt |
AC |
1 ms |
256 KB |
1_017.txt |
WA |
2 ms |
256 KB |
1_018.txt |
AC |
14 ms |
256 KB |
1_019.txt |
RE |
106 ms |
768 KB |
1_020.txt |
RE |
107 ms |
768 KB |
1_021.txt |
RE |
106 ms |
768 KB |
1_022.txt |
RE |
106 ms |
768 KB |