Submission #127070

#TimeUsernameProblemLanguageResultExecution timeMemory
127070LawlietMousetrap (CEOI17_mousetrap)C++14
25 / 100
1209 ms68044 KiB
#include <bits/stdc++.h> #define MAX 1000010 using namespace std; typedef long long int lli; int n; int mouse, trap; int n1, n2; int pai[MAX]; lli dp[MAX]; vector<int> grafo[MAX]; void DFSInit(int i, int p) { for(int g = 0 ; g < grafo[i].size() ; g++) { int prox = grafo[i][g]; if(prox == p) continue; pai[prox] = i; DFSInit(prox , i); } } void DFS(int i, int p, int blockI, int blockProx) { if(grafo[i].size() == 1 && i != trap) { dp[i] = 0; return; } vector<int> aux; aux.push_back( 0 ); for(int g = 0 ; g < grafo[i].size() ; g++) { int prox = grafo[i][g]; if(prox == p) continue; if(blockI == i && blockProx == prox) continue; pai[prox] = i; //printf("i = %d prox = %d\n",i,prox); DFS(prox , i , blockI , blockProx); aux.push_back( dp[prox] ); } sort(aux.begin() , aux.end()); dp[i] = grafo[i].size() - 2 + aux[aux.size() - 2] + 1; if(i == blockI) dp[i]--; //printf("dp(%d) = %lld\n",i,dp[i]); } int main() { scanf("%d %d %d",&n,&trap,&mouse); for(int g = 0 ; g < n - 1 ; g++) { scanf("%d %d",&n1,&n2); grafo[n1].push_back(n2); grafo[n2].push_back(n1); } DFSInit(trap , trap); DFS(trap , trap , 0 , 0); int aux = pai[mouse]; int sum = 0; while(aux != trap) { sum += grafo[aux].size() - 2; aux = pai[aux]; } //printf("dp = %d sum = %d\n",dp[mouse],sum); lli ans = dp[mouse] + sum; int cur = pai[mouse]; int last = mouse; //int sum = 0; while(cur != trap) { DFS(trap , trap , cur , last); //printf("cur = %d\n",cur); aux = pai[cur]; sum = 0; while(aux != trap) { sum += grafo[aux].size() - 2; aux = pai[aux]; } //printf("dp = %d sum = %d\n",dp[cur],sum); ans = max(ans , dp[cur] + sum); last = cur; cur = pai[last]; } printf("%lld\n",ans); }

Compilation message (stderr)

mousetrap.cpp: In function 'void DFSInit(int, int)':
mousetrap.cpp:20:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int g = 0 ; g < grafo[i].size() ; g++)
                  ~~^~~~~~~~~~~~~~~~~
mousetrap.cpp: In function 'void DFS(int, int, int, int)':
mousetrap.cpp:44:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int g = 0 ; g < grafo[i].size() ; g++)
                  ~~^~~~~~~~~~~~~~~~~
mousetrap.cpp: In function 'int main()':
mousetrap.cpp:72:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d %d %d",&n,&trap,&mouse);
  ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
mousetrap.cpp:76:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d %d",&n1,&n2);
   ~~~~~^~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...