Submission #366954

#TimeUsernameProblemLanguageResultExecution timeMemory
366954dolphingarlicMousetrap (CEOI17_mousetrap)C++14
45 / 100
862 ms77548 KiB
#include <bits/stdc++.h>
typedef long long ll;
using namespace std;

int n, t, m;
vector<int> graph[1000001];
ll dp[1000001], ans = 0;

bool dfs(int node = t, int parent = 0) {
    bool ret = false;
    ll mx1 = 0, mx2 = 0;
    for (int i : graph[node]) if (i != parent) {
        ret |= dfs(i, node);
        if (dp[i] >= mx1) mx2 = mx1, mx1 = dp[i];
        else if (dp[i] > mx2) mx2 = dp[i];
    }
    dp[node] = graph[node].size() - 1 + mx2;
    if (node == m) return ans += dp[node], true;
    else return ans += (int(graph[node].size()) - 2) * ret, ret;
}

int main() {
    cin.tie(0)->sync_with_stdio(0);
    cin >> n >> t >> m;
    for (int i = 1; i < n; i++) {
        int u, v;
        cin >> u >> v;
        graph[u].push_back(v);
        graph[v].push_back(u);
    }
    ans = -int(graph[t].size()) + 2;
    dfs();
    cout << ans;
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...