Submission #3928744
Source Code Expand
//include //------------------------------------------ #include <vector> #include <list> #include <map> #include <unordered_map> #include <climits> #include <set> #include <unordered_set> #include <deque> #include <stack> #include <bitset> #include <algorithm> #include <functional> #include <numeric> #include <utility> #include <sstream> #include <iostream> #include <iomanip> #include <cstdio> #include <cmath> #include <cstdlib> #include <cctype> #include <string> #include <cstring> #include <ctime> #include <queue> #include <random> #include <complex> #include <regex> using namespace std; #define SHOW_VECTOR(v) {std::cerr << #v << "\t:";for(const auto& xxx : v){std::cerr << xxx << " ";}std::cerr << "\n";} #define SHOW_MAP(v){std::cerr << #v << endl; for(const auto& xxx: v){std::cerr << xxx.first << " " << xxx.second << "\n";}} struct UnionFind { vector<int> par; vector<int> sizes; UnionFind(int n) : par(n), sizes(n, 1) { for (int i = 0; i < n; i++) par[i] = i; } int find(int x) { if (x == par[x]) return par[x]; else return par[x] = find(par[x]); } void unite(int x, int y) { x = find(x); y = find(y); if (x == y) return; if (sizes[x] < sizes[y]) swap(x, y); sizes[x] += sizes[y]; par[y] = x; } bool same(int x, int y) { return find(x) == find(y); } int getSizes(int x) { return sizes[x]; } }; int main() { int N, M; cin >> N >> M; vector<int> p(N); for (int i = 0; i < N; i++)cin >> p[i], p[i]--; UnionFind UF(N); for (int i = 0; i < M; i++) { int x, y; cin >> x >> y; x--, y--; UF.unite(x, y); } int ans = 0; for (int i = 0; i < N; i++) { if (UF.same(i, p[i])) ans++; } cout << ans << endl; }
Submission Info
Submission Time | |
---|---|
Task | D - Equals |
User | ganyariya |
Language | C++14 (GCC 5.4.1) |
Score | 400 |
Code Size | 2044 Byte |
Status | AC |
Exec Time | 90 ms |
Memory | 1408 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 | 45 ms | 256 KB |
1_005.txt | AC | 84 ms | 1408 KB |
1_006.txt | AC | 88 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 | 2 ms | 256 KB |
1_014.txt | AC | 3 ms | 256 KB |
1_015.txt | AC | 2 ms | 256 KB |
1_016.txt | AC | 2 ms | 256 KB |
1_017.txt | AC | 2 ms | 256 KB |
1_018.txt | AC | 41 ms | 256 KB |
1_019.txt | AC | 29 ms | 1408 KB |
1_020.txt | AC | 30 ms | 1408 KB |
1_021.txt | AC | 30 ms | 1408 KB |
1_022.txt | AC | 90 ms | 1408 KB |