8 条题解
-
-1
#include <bits/stdc++.h> using namespace std; vector<vector > adj; vector get_path(int s, int t, int n) { vector parent(n + 1, 0); vector visited(n + 1, false); queue q; q.push(s); visited[s] = true; parent[s] = -1; bool found = false; while (!q.empty() && !found) { int u = q.front(); q.pop(); for (int v : adj[u]) { if (!visited[v]) { visited[v] = true; parent[v] = u; q.push(v); if (v == t) { found = true; break; } } } } vector path; int curr = t; while (curr != -1) { path.push_back(curr); curr = parent[curr]; } reverse(path.begin(), path.end()); return path; }
int main() { int n, a; cin >> n >> a; adj.resize(n + 1); for (int i = 0; i < n - 1; ++i) { int x, y; cin >> x >> y; adj[x].push_back(y); adj[y].push_back(x); } vector<pair<int, int>> good_pairs; for (int i = 0; i < a; ++i) { int u, v; cin >> u >> v; good_pairs.emplace_back(u, v); } int bu, bv; cin >> bu >> bv; vector B = get_path(bu, bv, n); vector good_mark(n + 1, false); for (auto& p : good_pairs) { int u = p.first, v = p.second; vector path = get_path(u, v, n); for (int node : path) { good_mark[node] = true; } } int ans = 0; for (int node : B) { if (!good_mark[node]) { ans++; } } cout << ans << endl; return 0; }
- 1
信息
- ID
- 1
- 时间
- 1000ms
- 内存
- 256MiB
- 难度
- 6
- 标签
- 递交数
- 35
- 已通过
- 11
- 上传者