Submission #3424599
Source Code Expand
#include <iostream> #include <vector> #include <string> #include <algorithm> #include <cmath> #include <stack> #include <queue> #include <functional> #include <limits.h> #include <set> #include <map> using namespace std; #define ll long long #define ull unsigned long long #define rep(i,N) for (int i=0;i<N;i++) #define loop(i,N,M) for(int i=N;i<M;i++) //変数 ll N, M; vector<ll> p,x,y; class UnionFind { public: UnionFind(ll size) : parent(size), rank(size) { for (ll i = 0; i < size; i++) { parent[i] = i; rank[i] = 0; } }; ll root(ll x) { if (parent[x] == x) { return x; } else { return parent[x] = root(parent[x]); } } bool same(ll x, ll y) { return root(x) == root(y); } void unite(ll x, ll y) { x = root(x); y = root(y); if (x == y) return; if (rank[x] < rank[y]) { parent[x] = y; } else { parent[y] = x; if (rank[x] == rank[y]) rank[x]++; } } private: //parent[i]==j : iの親がj vector<ll> parent; vector<ll> rank; }; //解法 void solve() { UnionFind UF(N); rep(i, M) { UF.unite(x[i] - 1, y[i] - 1); } ll ans = 0; rep(i, N) { if (UF.same(p[i]-1, p[p[i]-1]-1) == true) { ans++; } } cout << ans; return; } int main() { //入力 cin >> N >> M; p.resize(N); x.resize(M); y.resize(M); rep(i, N) cin >> p[i]; rep(i, M) cin >> x[i] >> y[i]; solve(); cout << endl; return 0; }
Submission Info
Submission Time | |
---|---|
Task | D - Equals |
User | huma17 |
Language | C++14 (GCC 5.4.1) |
Score | 400 |
Code Size | 1484 Byte |
Status | AC |
Exec Time | 89 ms |
Memory | 4096 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 | 43 ms | 1792 KB |
1_005.txt | AC | 85 ms | 4096 KB |
1_006.txt | AC | 88 ms | 4096 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 | 3 ms | 384 KB |
1_015.txt | AC | 1 ms | 256 KB |
1_016.txt | AC | 1 ms | 256 KB |
1_017.txt | AC | 2 ms | 256 KB |
1_018.txt | AC | 40 ms | 1664 KB |
1_019.txt | AC | 31 ms | 2560 KB |
1_020.txt | AC | 30 ms | 2560 KB |
1_021.txt | AC | 37 ms | 2560 KB |
1_022.txt | AC | 89 ms | 4096 KB |