-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1249B2.cpp
More file actions
56 lines (52 loc) · 2.01 KB
/
Copy path1249B2.cpp
File metadata and controls
56 lines (52 loc) · 2.01 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
#include "bits/stdc++.h"
using namespace std;
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(), (x).end()
template<class Fun> class y_combinator_result {
#define endl '\n'
Fun fun_;
public:
template<class T> explicit y_combinator_result(T &&fun): fun_(std::forward<T>(fun)) {}
template<class ...Args> decltype(auto) operator()(Args &&...args) { return fun_(std::ref(*this), std::forward<Args>(args)...); }
};
template<class Fun> decltype(auto) y_combinator(Fun &&fun) { return y_combinator_result<std::decay_t<Fun>>(std::forward<Fun>(fun)); }
template<typename A, typename B> ostream& operator<<(ostream &os, const pair<A, B> &p) { return os << '(' << p.first << ", " << p.second << ')'; }
template<typename T_container, typename T = typename enable_if<!is_same<T_container, string>::value, typename T_container::value_type>::type> ostream& operator<<(ostream &os, const T_container &v) { os << '{'; string sep; for (const T &x : v) os << sep << x, sep = ", "; return os << '}'; }
void dbg_out() { cerr << endl; }
template<typename TT, typename... UU> void dbg_out(TT H, UU... T) { cerr << ' ' << H; dbg_out(T...); }
#ifndef ONLINE_JUDGE
#include "debug.hpp"
#define dbg(...) cerr << "(" << #__VA_ARGS__ << "):", dbg_out(__VA_ARGS__)
#else
#define print(...)
#define dbg(...)
#endif
void solve(){
int n; cin >> n;
vector<int> link(n), res(n);
for(int i = 0; i < n; ++i) {
cin >> link[i];
link[i]--;
}
for(int i = 0; i < n; ++i) {
if(res[i] == 0) {
int j = link[i];
int cnt = 1;
while(j != i) j = link[j], cnt += 1;
res[j] = cnt;
j = link[j];
while(j != i) res[j] = cnt, j = link[j];
}
}
for(int i = 0; i < n; ++i) cout << res[i] << " ";
cout << "\n";
}
int main(){
ios::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr);
int testt = 1;
cin >> testt;
for (int i = 1; i <= testt; ++i){
// cout << "Case #" << i << ": ";
solve();
}
}