Submission #303975

#TimeUsernameProblemLanguageResultExecution timeMemory
303975peuchMousetrap (CEOI17_mousetrap)C++17
25 / 100
1136 ms76028 KiB
#include<bits/stdc++.h> using namespace std; const int MAXN = 1000010; int n, t, m; int pai[MAXN], prof[MAXN]; long long dp[MAXN]; int marc[MAXN]; long long ans; vector<int> ar[MAXN]; void dfs(int cur); void getDP(int cur); int main(){ scanf("%d %d %d", &n, &t, &m); for(int i = 1; i < n; i++){ int a, b; scanf("%d %d", &a, &b); ar[a].push_back(b); ar[b].push_back(a); } dfs(t); stack<int> fila; int aux = m; while(aux != t){ fila.push(aux); marc[aux] = 1; aux = pai[aux]; } int sum = 0; while(!fila.empty()){ getDP(fila.top()); ans = max(ans, dp[fila.top()] + sum + prof[m] - prof[fila.top()]); sum += ar[fila.top()].size() - 2; fila.pop(); } printf("%lld\n", ans); } void dfs(int cur){ for(int i = 0; i < ar[cur].size(); i++){ int viz = ar[cur][i]; if(viz == pai[cur]) continue; prof[viz] = prof[cur] + 1; pai[viz] = cur; dfs(viz); } } void getDP(int cur){ marc[cur] = 1; pair<long long, long long> aux = make_pair(0, 0); int tam = 0; for(int i = 0; i < ar[cur].size(); i++){ int viz = ar[cur][i]; if(viz == pai[cur]) continue; if(marc[viz]) continue; getDP(viz); if(dp[viz] > aux.first) aux.second = aux.first, aux.first = dp[viz]; else aux.second = max(aux.second, dp[viz]); tam++; } dp[cur] = aux.second + 1 + tam - 1; if(tam == 0) dp[cur] = 0; }

Compilation message (stderr)

mousetrap.cpp: In function 'void dfs(int)':
mousetrap.cpp:44:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   44 |   for(int i = 0; i < ar[cur].size(); i++){
      |                  ~~^~~~~~~~~~~~~~~~
mousetrap.cpp: In function 'void getDP(int)':
mousetrap.cpp:57:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   57 |   for(int i = 0; i < ar[cur].size(); i++){
      |                  ~~^~~~~~~~~~~~~~~~
mousetrap.cpp: In function 'int main()':
mousetrap.cpp:18:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   18 |   scanf("%d %d %d", &n, &t, &m);
      |   ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
mousetrap.cpp:21:9: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   21 |    scanf("%d %d", &a, &b);
      |    ~~~~~^~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...