Submission #3454797
Source Code Expand
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
typedef tuple<ll, ll, ll> T;
int N, M;
vector<int> p(100001, 0);
vector<int> idx[101010];
int f[101010];
class UnionFind {
public:
vector<int> par, rank;
void init(int n) {
par = vector<int>(n);
rank = vector<int>(n);
for (int i = 0; i < n; i++) par[i] = i, rank[i] = 0;
}
// 木の根を求める
int root(int x) {
return (par[x] == x) ? x : par[x] = root(par[x]);
}
// xとyの属する集合を併合
void unite(int x, int y) {
x = root(x), y = root(y);
if (x == y)return;
if (rank[x] < rank[y]) par[x] = y;
else {
par[y] = x;
if (rank[x] == rank[y]) rank[x]++;
}
}
// xとyが同じ集合に属するか否か
bool same(int x, int y) {
return root(x) == root(y);
}
};
int main(void){
scanf("%d %d", &N, &M);
for(int i = 1; i <= N; ++i) scanf("%d", &p[i]);
UnionFind uf;
uf.init(N + 1);
for(int i = 0; i < M; ++i){
int x, y; scanf("%d %d", &x, &y);
uf.unite(x, y);
}
for(int i = 1; i <= N; ++i) idx[uf.root(i)].push_back(i);
int ans = 0;
for(int i = 1; i <= N; ++i){
for(auto j : idx[i]) f[j] = 1;
for(auto j : idx[i]) if(f[p[j]]) ++ans;
for(auto j : idx[i]) f[j] = 0;
}
printf("%d\n", ans);
return 0;
}
Submission Info
Submission Time |
|
Task |
D - Equals |
User |
sgneet |
Language |
C++14 (GCC 5.4.1) |
Score |
400 |
Code Size |
1450 Byte |
Status |
AC |
Exec Time |
36 ms |
Memory |
7296 KB |
Compile Error
./Main.cpp: In function ‘int main()’:
./Main.cpp:44: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:45:51: 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:51:41: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
int x, y; 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 |
2 ms |
2944 KB |
0_001.txt |
AC |
2 ms |
2944 KB |
0_002.txt |
AC |
2 ms |
2944 KB |
0_003.txt |
AC |
2 ms |
2944 KB |
1_004.txt |
AC |
17 ms |
3072 KB |
1_005.txt |
AC |
34 ms |
7296 KB |
1_006.txt |
AC |
35 ms |
4728 KB |
1_007.txt |
AC |
3 ms |
2944 KB |
1_008.txt |
AC |
2 ms |
2944 KB |
1_009.txt |
AC |
3 ms |
2944 KB |
1_010.txt |
AC |
2 ms |
2944 KB |
1_011.txt |
AC |
2 ms |
3072 KB |
1_012.txt |
AC |
2 ms |
3072 KB |
1_013.txt |
AC |
3 ms |
2944 KB |
1_014.txt |
AC |
3 ms |
2944 KB |
1_015.txt |
AC |
3 ms |
3072 KB |
1_016.txt |
AC |
3 ms |
3072 KB |
1_017.txt |
AC |
3 ms |
3072 KB |
1_018.txt |
AC |
16 ms |
3072 KB |
1_019.txt |
AC |
18 ms |
7296 KB |
1_020.txt |
AC |
18 ms |
7296 KB |
1_021.txt |
AC |
18 ms |
7296 KB |
1_022.txt |
AC |
36 ms |
4984 KB |