WA on #1 #6 #8 #10的第一组数据
代码
#include <bits/stdc++.h>
using namespace std;
int T, n, m, vis[1005];
vector<int> e[1005];
void dfs(int u) {
vis[u] = 1;
for (int v : e[u]) {
if (!vis[v]) {
dfs(v);
}
}
return;
}
bool check() {
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
vis[j] = 0;
}
dfs(i);
for (int j = 1; j <= n; j++) {
if (!vis[j]) {
return 0;
}
}
}
return 1;
}
int main() {
cin >> T;
while (T--) {
cin >> n >> m;
for (int i = 1; i <= n; i++) {
e[i].clear();
}
for (int i = 1; i <= m; i++) {
int u, v;
cin >> u >> v;
e[u].push_back(v);
}
if (check()) {
cout << "Yes" << endl;
} else {
cout << "No" << endl;
}
}
return 0;
}