Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- #define ll long long
- const int maxn = 1e5 + 10;
- int n, k;
- int b[maxn];
- int dep[maxn], f[maxn];
- bool tag;
- void solve() {
- cin >> n >> k;
- for (int i = 1; i <= n; i ++) {
- dep[i] = 0;
- f[i] = i;
- }
- for (int i = 1; i <= n; i ++) {
- cin >> b[i];
- }
- if (k == 1) {
- for (int i = 1; i <= n; i ++) {
- if (b[i] != i) {
- cout << "NO\n";
- return;
- }
- }
- cout << "YES\n";
- return;
- }
- if (k == 2) {
- for (int i = 1; i <= n; i ++) {
- if (b[b[i]] != i) {
- cout << "NO\n";
- return;
- }
- }
- cout << "YES\n";
- }
- tag = 0;
- for (int i = 1; i <= n; i ++) {
- if (dep[i]) continue;
- dep[i] = 1;
- int x = i;
- f[x] = i;
- while (!dep[b[x]]) {
- dep[b[x]] = dep[x] + 1;
- x = b[x];
- f[x] = i;
- }
- int res = dep[x] - dep[b[x]] + 1;
- // cout << i << " " << res << endl;
- if (f[x] != f[b[x]]) continue;
- if (res == k) {
- tag = 1;
- break;
- }
- }
- if (tag) cout << "YES\n";
- else cout << "NO\n";
- }
- int main() {
- ios::sync_with_stdio(false);
- cin.tie(nullptr);
- int T = 1;
- cin >> T;
- while (T--) {
- solve();
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement