-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCodeforces_Round_Dance.cpp
More file actions
71 lines (66 loc) · 1.47 KB
/
Copy pathCodeforces_Round_Dance.cpp
File metadata and controls
71 lines (66 loc) · 1.47 KB
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
68
69
70
71
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define endl '\n'
#define optimize() ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
const ll N=2e5+10;
ll parent[N];
ll setRank[N];
void makeSet(ll n){
for(ll i=1; i<=n; i++)
parent[i]=i;
}
ll findSet(ll n){
if(parent[n]==n) return n;
return parent[n]=findSet(parent[n]);
}
void unionSet(ll a, ll b){
a=findSet(a);
b=findSet(b);
if(setRank[a]<setRank[b])
swap(a,b);
parent[b]=a;
if(setRank[a]==setRank[b])
setRank[a]++;
}
void solve(){
ll n;
cin>>n;
vector<ll> a(n+1);
vector<set<ll>> vs(n+1);
makeSet(n);
for(ll i=1; i<=n; i++){
cin>>a[i];
vs[i].insert(a[i]);
vs[a[i]].insert(i);
unionSet(i,a[i]);
}
ll maxSetCnt=0;
for(ll i=1; i<=n; i++){
ll x=findSet(i);
if(parent[i]==i)
maxSetCnt++;
}
ll nodeWithOneNeighbour=0;
for(ll i=1; i<=n; i++){
if(vs[i].size()==1)
nodeWithOneNeighbour++;
}
ll setCanBeMargedlloOne=nodeWithOneNeighbour/2;
ll cntSetToDecrease=setCanBeMargedlloOne-1;
if(setCanBeMargedlloOne==0)
cntSetToDecrease=0;
ll minSetCnt=maxSetCnt-cntSetToDecrease;
cout<<minSetCnt<<" "<<maxSetCnt<<endl;
}
signed main(){
// freopen("input.txt","r",stdin);
// freopen("output.txt","w",stdout);
optimize();
ll t;
cin>>t;
for(ll i=1; i<=t; i++){
solve();
}
return 0;
}