-
Notifications
You must be signed in to change notification settings - Fork 0
/
sol.cpp
67 lines (61 loc) · 1.06 KB
/
sol.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAX_N = 2e5 + 1;
vector<int> adj[MAX_N];
set<int> s[MAX_N], sz;
bool vis[MAX_N];
int a[MAX_N];
void dfs(int u) {
vis[u] = true;
sz.insert(adj[u].size());
for (int v : adj[u]) {
if (!vis[v]) {
dfs(v);
}
}
}
void test_case(int& tc) {
int n;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> a[i];
int u = i, v = a[i] - 1;
if (s[u].find(v) != s[u].end()) {
continue;
}
adj[u].push_back(v);
adj[v].push_back(u);
s[u].insert(v);
s[v].insert(u);
}
memset(vis, false, sizeof(vis));
int mn = 0, mx = 0;
bool acyclic_exists = false;
for (int u = 0; u < n; u++) {
if (!vis[u]) {
sz.clear();
dfs(u);
if (sz.find(1) == sz.end()) {
mn++;
} else {
acyclic_exists = true;
}
mx++;
}
}
cout << mn + (acyclic_exists ? 1 : 0) << " " << mx << "\n";
for (int u = 0; u < n; u++) {
adj[u].clear();
s[u].clear();
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int T = 1;
cin >> T;
for (int tc = 1; tc <= T; tc++) {
test_case(tc);
}
}