Submission #2560164
Source Code Expand
#include <iostream> #include <cstdio> #include <cmath> #include <vector> #include <list> #include <set> #include <map> #include <stack> #include <queue> #include <string> #include <utility> #include <algorithm> #include <functional> #include <deque> #define INF 1e9 #define MOD 1000000007 #define mod(x) ((x % MOD) + MOD) % MOD #define MAX_N 100001 #define MAX_M 100001 using namespace std; typedef long long ll; typedef pair<int,int> P; int par[MAX_N]; int myrank[MAX_N]; void init(int n) { for (int i = 0; i < n; i++) { par[i] = i; myrank[i] = 0; } } int find(int x) { if (par[x] == x) return x; return par[x] = find(par[x]); } void unite(int x, int y) { x = find(x); y = find(y); if (x == y) return; if (myrank[x] < myrank[y]) { par[x] = y; } else { par[y] = x; if(myrank[x] == myrank[y]) myrank[x]++; } } bool same(int x, int y) { return find(x) == find(y); } int main() { int N, M; int p[MAX_N]; int x[MAX_M]; int y[MAX_N]; cin >> N >> M; for (int i = 0; i < N; i++) { cin >> p[i]; p[i]--; } for (int i = 0; i < M; i++) { cin >> x[i] >> y[i]; x[i]--; y[i]--; } init(N); for (int i = 0; i < M; i++) { unite(x[i], y[i]); } int ans = 0; for (int i = 0; i < N; i++) { if (same(i, p[i])) { ans++; } } for (int i = 0; i < N; i++) { printf("i, p[i], same(i, p[i]): %d, %d, %d\n", i, p[i], (same(i, p[i]) ? 1 : 0)); } cout << ans << endl; return 0; }
Submission Info
Submission Time | |
---|---|
Task | D - Equals |
User | habara_k |
Language | C++14 (GCC 5.4.1) |
Score | 0 |
Code Size | 1561 Byte |
Status | WA |
Exec Time | 132 ms |
Memory | 6016 KB |
Judge Result
Set Name | Sample | All | ||||
---|---|---|---|---|---|---|
Score / Max Score | 0 / 0 | 0 / 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 | WA | 1 ms | 256 KB |
0_001.txt | WA | 1 ms | 256 KB |
0_002.txt | WA | 1 ms | 256 KB |
0_003.txt | WA | 1 ms | 256 KB |
1_004.txt | WA | 52 ms | 1024 KB |
1_005.txt | WA | 127 ms | 6016 KB |
1_006.txt | WA | 131 ms | 6016 KB |
1_007.txt | WA | 1 ms | 256 KB |
1_008.txt | WA | 1 ms | 256 KB |
1_009.txt | WA | 1 ms | 256 KB |
1_010.txt | WA | 1 ms | 256 KB |
1_011.txt | WA | 1 ms | 256 KB |
1_012.txt | WA | 1 ms | 256 KB |
1_013.txt | WA | 2 ms | 256 KB |
1_014.txt | WA | 3 ms | 256 KB |
1_015.txt | WA | 2 ms | 256 KB |
1_016.txt | WA | 2 ms | 256 KB |
1_017.txt | WA | 2 ms | 256 KB |
1_018.txt | WA | 48 ms | 1024 KB |
1_019.txt | WA | 59 ms | 5248 KB |
1_020.txt | WA | 59 ms | 5248 KB |
1_021.txt | WA | 59 ms | 5376 KB |
1_022.txt | WA | 132 ms | 6016 KB |