I just figured that if the size of the queue is greater than 1 at any time (the queue has nodes with in-degree 0), the answer is âNOâ because multiple topological sorts will exist.

All you need to do is add `if (pq.size() > 1) break;`

in the while loop of Kahnâs algorithm where `pq`

is the priority queue.

The code is simply

```
#include <bits/stdc++.h>
using namespace std;
const int size = 1e2+1;
vector <int> adj[size];
vector <int> res;
vector <int> in(size);
void kahn(int n)
{
priority_queue <int, vector <int>, greater <int>> pq;
for (int i = 1; i <= n; ++i) if (in[i] == 0) pq.push(i);
while (!pq.empty())
{
if (pq.size() > 1) break;
int curr = pq.top();
res.push_back(curr);
pq.pop();
for (int child : adj[curr])
{
in[child]--;
if (in[child] == 0)
pq.push(child);
}
}
if (res.size() != n) cout << "NO\n";
else {
cout << "YES\n";
for (int i : res)
{
string ans = "k";
ans.append(to_string(i));
cout << ans << '\n';
}
}
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
int t;
cin >> t;
while (t--)
{
int n, m;
string u, v;
cin >> n;
cin >> m;
for (int i = 0; i < m; ++i)
{
cin >> u >> v;
adj[stoi(v.substr(1, v.size()-1))].push_back(stoi(u.substr(1, u.size()-1)));
in[stoi(u.substr(1, u.size()-1))]++;
}
kahn(n);
res.clear();
for (int i = 0; i < size; ++i) adj[i].clear();
in.clear();
in.resize(size);
}
}
```

Pretty simple and my first time using topological sort