AcWing 3250. 通信网络
原题链接
中等
作者:
王小强
,
2021-02-20 21:24:49
,
所有人可见
,
阅读 301
用二维数组来做访问标记,别的就是普通的DFS!
#include <iostream>
#include <vector>
using namespace std;
const int N = 1010;
int n, m, u, v;
vector<vector<int>> g(N);
vector<vector<int>> visited(N, vector<int>(N)); // adjacency matrix
void dfs(int cur, int start) {
if (visited[start][cur]) return;
visited[start][cur] = 1; // mark as seen
for (const auto& nxt : g[cur]) dfs(nxt, start);
}
bool check(int x) {
for (int i = 1; i <= n; i++)
if (!visited[x][i] && !visited[i][x]) return false;
return true;
}
int main(void) {
cin >> n >> m;
while (m--) {
cin >> u >> v;
g[u].emplace_back(v);
}
for (int i = 1; i <= n; ++i)
dfs(i, i);
int ans = 0;
for (int i = 1; i <= n; ++i)
ans += check(i);
printf("%d\n", ans);
return 0;
}