1 条题解
-
0
深搜
#include <bits/stdc++.h> using namespace std; int n, m; vector<int> e[1005]; bool vis[1005]; // 返回 u 出发走没走过的点能到的编号最大的点 int dfs(int u) { int res = u; // 枚举所有u能连到的点v for (int i = 0; i < e[u].size(); i++) { int v = e[u][i]; if (vis[v] == true) continue; vis[v] = true; res = max(res, dfs(v)); } return res; } int main() { ios::sync_with_stdio(false); cin.tie(0); cin >> n >> m; for (int i = 1; i <= m; i++) { int u, v; cin >> u >> v; e[u].push_back(v); } memset(vis, false, sizeof(vis)); vis[1] = true; cout << dfs(1) << "\n"; return 0; }
广搜
#include <bits/stdc++.h> using namespace std; int n, m; vector<int> e[1005]; bool vis[1005]; queue<int> q; int main() { ios::sync_with_stdio(false); cin.tie(0); cin >> n >> m; for (int i = 1; i <= m; i++) { int u, v; cin >> u >> v; e[u].push_back(v); } memset(vis, false, sizeof(vis)); vis[1] = true; q.push(1); int ans = 1; while (!q.empty()) { int u = q.front(); q.pop(); ans = max(ans, u); for (int i = 0; i < e[u].size(); i++) { int v = e[u][i]; if (vis[v]) continue; vis[v] = true; q.push(v); } } cout << ans << "\n"; return 0; }
- 1
信息
- ID
- 1197
- 时间
- 1000ms
- 内存
- 256MiB
- 难度
- 4
- 标签
- 递交数
- 55
- 已通过
- 25
- 上传者