Submission #3009670
Source Code Expand
#include<bits/stdc++.h>
using namespace std;
int P[100005];
class DisjointSet {
public:
vector<int> rank, p;
DisjointSet() {}
DisjointSet(int size) {
rank.resize(size, 0);
p.resize(size, 0);
for (int i = 0; i < size; i++) makeSet(i);
}
void makeSet(int x) {
p[x] = x;
rank[x] = 0;
}
bool same(int x, int y) {
return findSet(x) == findSet(y);
}
void unite(int x, int y) {
link(findSet(x), findSet(y));
}
void link(int x, int y) {
if (rank[x] > rank[y]) {
p[y] = x;
} else {
p[x] = y;
if (rank[x] == rank[y]) rank[y]++;
}
}
int findSet(int x) {
if (x != p[x]) {
p[x] = findSet(p[x]);
}
return p[x];
}
};
int main() {
int N, M, x, y;
scanf("%d %d", &N, &M);
DisjointSet ds = DisjointSet(N+1);
for (int i = 1; i <= N; i++) scanf("%d", &P[i]);
for (int i = 1; i <= M; i++) {
scanf("%d %d", &x, &y);
ds.unite(x, y);
}
int ans = 0;
for (int i = 1; i <= N; i++) {
if (P[i] == i) ans++;
else if (ds.same(i, P[i])) ans++;
}
printf("%d\n", ans);
return 0;
}
Submission Info
Submission Time |
|
Task |
D - Equals |
User |
atumari |
Language |
C++14 (GCC 5.4.1) |
Score |
400 |
Code Size |
1327 Byte |
Status |
AC |
Exec Time |
34 ms |
Memory |
1408 KB |
Compile Error
./Main.cpp: In function ‘int main()’:
./Main.cpp:49:27: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d", &N, &M);
^
./Main.cpp:53:52: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
for (int i = 1; i <= N; i++) scanf("%d", &P[i]);
^
./Main.cpp:56:31: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d", &x, &y);
^
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 |
16 ms |
256 KB |
1_005.txt |
AC |
28 ms |
1408 KB |
1_006.txt |
AC |
34 ms |
1408 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 |
1 ms |
256 KB |
1_018.txt |
AC |
15 ms |
256 KB |
1_019.txt |
AC |
11 ms |
1408 KB |
1_020.txt |
AC |
11 ms |
1408 KB |
1_021.txt |
AC |
11 ms |
1408 KB |
1_022.txt |
AC |
33 ms |
1408 KB |