이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include<iostream>
#include<algorithm>
#include<vector>
#define all(x) x.begin(), x.end()
using namespace std;
typedef long long ll;
const int N = 1e6 + 16;
int n, t, m;
vector<int> g[N];
bool checksubtask2() { // It is guaranteed that a passage between rooms m and t exists.
for (int v : g[m])
if (v == t) return true;
return false;
}
int dfs(int u, int p) {
if (g[u].size() == 1) return 0; // hết đường
if (g[u].size() == 2) return 1; // còn 1 đường nhưng sẽ bị chặn
vector<int> res;
for (int v : g[u]) if (v != p) {
res.push_back(dfs(v, u) + 1); // nếu chuột chạy vào v thì min bước là ? (+1 lượt dọn phân khi quay lại)
}
sort(all(res));
return g[u].size() - 2 + res[1]; // chặn đường thứ nhất, chuột đi vào đường thứ nhì
}
void subtask2() {
cout << dfs(m, t) << '\n';
}
int main() {
cin.tie(NULL)->sync_with_stdio(false);
cin >> n >> t >> m;
for (int i = 1, u, v; i < n; ++i) {
cin >> u >> v;
g[u].push_back(v);
g[v].push_back(u);
}
if (checksubtask2()) subtask2();
}
/** /\_/\
* (= ._.)
* / >0 \>1
**/
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |