Submission #4466006
Source Code Expand
#include <bits/stdc++.h>
using namespace std;
using VS = vector<string>; using LL = long long;
using VI = vector<int>; using VVI = vector<VI>;
using PII = pair<int, int>; using PLL = pair<LL, LL>;
using VL = vector<LL>; using VVL = vector<VL>;
#define ALL(a) begin((a)),end((a))
#define RALL(a) (a).rbegin(), (a).rend()
#define SZ(a) int((a).size())
#define SORT(c) sort(ALL((c)))
#define RSORT(c) sort(RALL((c)))
#define UNIQ(c) (c).erase(unique(ALL((c))), end((c)))
#define FOR(i, s, e) for (int(i) = (s); (i) < (e); (i)++)
#define FORR(i, s, e) for (int(i) = (s); (i) > (e); (i)--)
//#pragma GCC optimize ("-O3")
#ifdef YANG33
#include "mydebug.hpp"
#else
#define DD(x)
#endif
const int INF = 1e9; const LL LINF = 1e16;
const LL MOD = 1000000007; const double PI = acos(-1.0);
int DX[8] = { 0, 0, 1, -1, 1, 1, -1, -1 }; int DY[8] = { 1, -1, 0, 0, 1, -1, 1, -1 };
/* ----- 2019/03/04 Problem: ABC 097 D / Link: http://abc097.contest.atcoder.jp/tasks/abc097_d ----- */
struct UnionFind {
vector<int> data;
UnionFind(int n) { data.assign(n, -1); }
bool unionSet(int x, int y) {
x = root(x); y = root(y);
if (x != y) {
if (data[y] < data[x]) swap(x, y);
data[x] += data[y]; data[y] = x;
}
return x != y;
}
bool same(int x, int y) { return root(x) == root(y); }
int root(int x) { return data[x] < 0 ? x : data[x] = root(data[x]); }
int size(int x) { return -data[root(x)]; }
};
int main() {
cin.tie(0);
ios_base::sync_with_stdio(false);
LL N, M; cin >> N >> M;
vector<LL> a(N);
for (int i = 0; i < N; ++i) {
cin >> a[i];
a[i]--;
}
vector<LL> x(M), y(M);
for (int i = 0; i < M; ++i) {
cin >> x[i] >> y[i];
x[i]--, y[i]--;
}
UnionFind uf(N);
FOR(i, 0, M) {
uf.unionSet(x[i], y[i]);
}
LL ans = 0LL;
FOR(i, 0, N) {
if (uf.same(a[i], i))ans++;
}
cout << (ans) << "\n";
return 0;
}
Submission Info
| Submission Time |
|
| Task |
D - Equals |
| User |
Yang33 |
| Language |
C++14 (GCC 5.4.1) |
| Score |
400 |
| Code Size |
1981 Byte |
| Status |
AC |
| Exec Time |
28 ms |
| Memory |
2944 KiB |
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 KiB |
| 0_001.txt |
AC |
1 ms |
256 KiB |
| 0_002.txt |
AC |
1 ms |
256 KiB |
| 0_003.txt |
AC |
1 ms |
256 KiB |
| 1_004.txt |
AC |
15 ms |
1792 KiB |
| 1_005.txt |
AC |
26 ms |
2944 KiB |
| 1_006.txt |
AC |
28 ms |
2944 KiB |
| 1_007.txt |
AC |
1 ms |
256 KiB |
| 1_008.txt |
AC |
1 ms |
256 KiB |
| 1_009.txt |
AC |
1 ms |
256 KiB |
| 1_010.txt |
AC |
1 ms |
256 KiB |
| 1_011.txt |
AC |
1 ms |
256 KiB |
| 1_012.txt |
AC |
1 ms |
256 KiB |
| 1_013.txt |
AC |
1 ms |
256 KiB |
| 1_014.txt |
AC |
2 ms |
384 KiB |
| 1_015.txt |
AC |
1 ms |
256 KiB |
| 1_016.txt |
AC |
1 ms |
256 KiB |
| 1_017.txt |
AC |
2 ms |
256 KiB |
| 1_018.txt |
AC |
14 ms |
1664 KiB |
| 1_019.txt |
AC |
10 ms |
1408 KiB |
| 1_020.txt |
AC |
10 ms |
1408 KiB |
| 1_021.txt |
AC |
10 ms |
1408 KiB |
| 1_022.txt |
AC |
28 ms |
2944 KiB |