AcWing 3250. 通信网络
原题链接
中等
作者:
王欢祥
,
2024-12-05 10:54:34
,
所有人可见
,
阅读 1
#include <iostream>
#include <cstring>
#include <vector>
using namespace std;
const int N = 1010;
vector<int> v[N], w[N];
int n, m;
int visited[N], vis2[N];
void dfs1(int x) {
visited[x] = 1;
for (int i = 0; i < v[x].size(); i++) {
if (!visited[v[x][i]]) {
dfs1(v[x][i]);
}
}
return ;
}
void dfs2(int x) {
vis2[x] = 1;
for (int i = 0; i < w[x].size(); i++) {
if (!vis2[w[x][i]]) {
dfs2(w[x][i]);
}
}
return ;
}
int main() {
int res = 0;
cin >> n >> m;
for (int i = 1; i <= m; i++) {
int a, b;
cin >> a >> b;
v[a].push_back(b);
w[b].push_back(a);
}
for (int i = 1; i <= n; i++) {
int f = 1;
memset(visited, 0, sizeof visited);
memset(vis2, 0, sizeof vis2);
dfs1(i);
dfs2(i);
for (int i = 1; i <= n; i++) {
if (!visited[i] && !vis2[i]) {
f = 0;
break;
}
}
if (f)
res++;
}
cout << res << endl;
return 0;
}