Submission #2560099
Source Code Expand
#include <iostream> #include <vector> #include <cstdio> #include <string> #include <cmath> #include <set> #include <map> using namespace std; const int MAX_UF = 210000; struct UnionFind { int size_uf; int par[MAX_UF]; int rank[MAX_UF]; int size_component[MAX_UF]; UnionFind(int n = 0) { size_uf = n; for (int i = 0; i < n; ++i) par[i] = i, rank[i] = 0, size_component[i] = 1; } void init(int n) { size_uf = n; for (int i = 0; i < n; ++i) par[i] = i, rank[i] = 0, size_component[i] = 1; } int root(int x) { return par[x] == x ? x : par[x] = root(par[x]); } bool issame(int x, int y) { return root(x) == root(y); } int size(int x) { return size_component[root(x)]; } void connect(int x, int y) { x = root(x), y = root(y); int sx = size_component[x], sy = size_component[y]; if (x == y) return; int r; if (rank[x] < rank[y]) par[x] = y, r = y; else { par[y] = x, r = x; if (rank[x] == rank[y]) ++rank[x]; } size_component[r] = sx + sy; } } uf; int N, M; vector<int> P; int main() { cin >> N >> M; P.resize(N); for (int i = 0; i < N; ++i) { cin >> P[i]; --P[i]; } uf.init(N); for (int i = 0; i < M; ++i) { int x, y; cin >> x >> y; --x, --y; uf.connect(x, y); } long long res = 0; map<int,set<int> > perm, index; for (int i = 0; i < N; ++i) { int r = uf.root(i); perm[r].insert(P[i]); index[r].insert(i); } for (map<int,set<int> >::iterator it = perm.begin(); it != perm.end(); ++it) { long long tmp = 0; int r = it->first; set<int> se = it->second; for (set<int>::iterator it2 = se.begin(); it2 != se.end(); ++it2) { if (index[r].count(*it2)) ++tmp; } res += tmp; } cout << res << endl; }
Submission Info
Submission Time | |
---|---|
Task | D - Equals |
User | drken |
Language | C++14 (GCC 5.4.1) |
Score | 400 |
Code Size | 1757 Byte |
Status | AC |
Exec Time | 177 ms |
Memory | 29952 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 | 42 ms | 256 KB |
1_005.txt | AC | 148 ms | 29824 KB |
1_006.txt | AC | 177 ms | 15872 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 | 512 KB |
1_016.txt | AC | 2 ms | 512 KB |
1_017.txt | AC | 2 ms | 384 KB |
1_018.txt | AC | 41 ms | 384 KB |
1_019.txt | AC | 127 ms | 29952 KB |
1_020.txt | AC | 98 ms | 29952 KB |
1_021.txt | AC | 98 ms | 29696 KB |
1_022.txt | AC | 170 ms | 17920 KB |