def find_cycles(arr):
'''
[where 1 goes, where 2 goes, ... where n goes]
'''
n = len(arr)
round = 0
first_visited = [0 for i in range(n)]
cycles = []
for i in range(n):
if first_visited[i] == 0:
round += 1
index = i
while first_visited[index] == 0:
first_visited[index] = round
index = arr[index]
if first_visited[index] == round:
cycle = [index]
next = arr[index]
while next != index:
cycle.append(next)
next = arr[next]
cycles.append(cycle)
return cycles
for _ in range(int(input())):
n, k = map(int, input().split())
b = list(map(lambda x: int(x)-1, input().split()))
if k == 1:
if all(b[i]==i for i in range(n)):
print('YES')
else:
print('NO')
else:
for c in find_cycles(b):
if len(c) != k:
print('NO')
break
else:
print('YES')
#include <bits/stdc++.h>
#include "ext/pb_ds/assoc_container.hpp"
#include "ext/pb_ds/tree_policy.hpp"
using namespace std;
using namespace __gnu_pbds;
#define ll long long
#define all(x) (x).begin(), (x).end()
template<class T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
// find_by_order, order_of_key
void solve() {
ll n, k;
cin >> n >> k;
vector<ll> v(n);
for (auto &i : v) {
cin >> i;
i--;
}
// idea : cycle size should be k
vector<ll> next(n);
for (ll i = 0; i < n; i++) {
next[i] = v[i];
}
vector<ll> vis(n, 0), cycle(n, 0);
function<ll(ll)> calc = [&] (ll a) {
if (vis[a] == 1) {
if (cycle[a] != 0) return cycle[a];
ll sz = 1;
ll k = a;
while (next[k] != a) {
sz++;
k = next[k];
}
return cycle[a] = sz;
}
vis[a] = 1;
return cycle[a] = calc(next[a]);
};
for (ll i = 0; i < n; i++) {
if (vis[i] == 0) {
cycle[i] = calc(i);
}
}
for (ll i = 0; i < n; i++) {
if (k != 1 && cycle[i] != k) {
cout << "No\n";
return;
}
if (v[i] != i && k == 1) {
cout << "No\n";
return;
}
}
cout << "Yes\n";
}
int main() {
ios_base::sync_with_stdio(false); cin.tie(NULL);
ll t = 1;
cin >> t;
while (t--) {
solve();
}
return 0;
}
287A - IQ Test | 1108A - Two distinct points |
1064A - Make a triangle | 1245C - Constanze's Machine |
1005A - Tanya and Stairways | 1663F - In Every Generation |
1108B - Divisors of Two Integers | 1175A - From Hero to Zero |
1141A - Game 23 | 1401B - Ternary Sequence |
598A - Tricky Sum | 519A - A and B and Chess |
725B - Food on the Plane | 154B - Colliders |
127B - Canvas Frames | 107B - Basketball Team |
245A - System Administrator | 698A - Vacations |
1216B - Shooting | 368B - Sereja and Suffixes |
1665C - Tree Infection | 1665D - GCD Guess |
29A - Spit Problem | 1097B - Petr and a Combination Lock |
92A - Chips | 1665B - Array Cloning Technique |
1665A - GCD vs LCM | 118D - Caesar's Legions |
1598A - Computer Game | 1605A - AM Deviation |