Submission #8445116
Source Code Expand
#include <bits/stdc++.h>
using namespace std;
#define repi(i, a, b) for(int i = (int)(a); i < (int)(b); ++i)
#define rep(i, n) repi(i, 0, n)
#define all(x) (x).begin(), (x).end()
struct UF {
int n;
vector< int > par;
UF(int n) : n(n), par(n, -1) {}
int find(int x) { return par[x] < 0 ? x : par[x] = find(par[x]); }
int size(int x) { return -par[find(x)]; }
bool same(int a, int b) { return find(a) == find(b); }
void unite(int a, int b) {
a = find(a);
b = find(b);
if(a == b) return;
if(par[a] < par[b]) swap(a, b);
par[b] += par[a];
par[a] = b;
}
void clear() { par.assign(n, -1); }
};
using P = pair<int, int>;
inline void Main() {
// code
int N, M, x, y, res = 0;
map<int, int> MII;
cin >> N >> M;
rep(i, N) {
cin >> x;
MII.insert(P(x-1, i));
}
UF uf(N);
rep(i, M) {
cin >> x >> y;
uf.unite(x-1, y-1);
}
rep(i, N) {
int t = N - 1 - i;
if (uf.same(t, MII.at(t))) {
res++;
}
}
cout << res << endl;
}
int main() {
std::cin.tie(0);
std::ios::sync_with_stdio(false);
std::cout << std::fixed << std::setprecision(20);
Main();
return 0;
}
Submission Info
Submission Time |
|
Task |
D - Equals |
User |
u7693 |
Language |
C++14 (GCC 5.4.1) |
Score |
400 |
Code Size |
1212 Byte |
Status |
AC |
Exec Time |
68 ms |
Memory |
5376 KB |
Judge Result
Set Name |
Sample |
All |
Score / Max Score |
0 / 0 |
400 / 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 |
AC |
64 ms |
5376 KB |
1_006.txt |
AC |
68 ms |
5376 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 |
AC |
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 |
AC |
2 ms |
256 KB |
1_018.txt |
AC |
14 ms |
256 KB |
1_019.txt |
AC |
47 ms |
5376 KB |
1_020.txt |
AC |
47 ms |
5376 KB |
1_021.txt |
AC |
48 ms |
5376 KB |
1_022.txt |
AC |
67 ms |
5376 KB |