Submission #1051039

#TimeUsernameProblemLanguageResultExecution timeMemory
1051039Ahmed57Hard route (IZhO17_road)C++17
52 / 100
4 ms8028 KiB
#include "bits/stdc++.h" using namespace std; vector<int> adj[100001]; int mx[100001]; int cnt[100001]; pair<int,int> pref[100001]; long long overMA = 0; long long overCNT = 1; void dfs(int i,int pr){ mx[i] = 0 , cnt[i] = 1; for(auto j:adj[i]){ if(j==pr)continue; dfs(j,i); if(mx[j]+1>mx[i]){ mx[i] = mx[j]+1; cnt[i] = 0; } if(mx[j]+1==mx[i]){ cnt[i]+=cnt[j]; } } } void reroot(int i,int pr,int MA,int CNT){ vector<pair<int,int>> v; if(MA)v.push_back({MA,CNT}); for(auto j:adj[i]){ if(j==pr)continue; v.push_back({mx[j]+1,cnt[j]}); } sort(v.begin(),v.end()); reverse(v.begin(),v.end()); if(v.size()>=3){ int z = v[0].first , y = v[1].first , x = v[2].first; long long cost = 0; if(x<y&&y<z){ int A = v[0].second , B = v[1].second , C = v[2].second; for(int i = 3;i<v.size();i++)if(v[i].first==x)C+=v[i].second; cost = B*C; }else if(x<y&&y==z){ int A = v[0].second , B = v[1].second , C = v[2].second; for(int i = 3;i<v.size();i++)if(v[i].first==x)C+=v[i].second; cost = (A+B)*C; }else if(x==y&&y<z){ int A = v[0].second , B = v[1].second , C = v[2].second; cost = B*C; long long sum = B+C; for(int i = 3;i<v.size();i++){ if(v[i].first==x){ cost+=sum*v[i].second; sum+=v[i].second; } } }else if(x==y&&y==z){ int A = v[0].second , B = v[1].second , C = v[2].second; cost = A*B+(A+B)*C; long long sum = A+B+C; for(int i = 3;i<v.size();i++){ if(v[i].first==x){ cost+=sum*v[i].second; sum+=v[i].second; } } } if(overMA<(x+y)*z){ overMA = (x+y)*z; overCNT = 0; } if(overMA==(x+y)*z){ overCNT+=cost; } } pair<int,int> cur = {MA,CNT}; for(auto j:adj[i]){ if(j==pr)continue; pref[j] = cur; if(mx[j]+1>cur.first){ cur.first = mx[j]+1; cur.second = 0; } if(mx[j]+1==cur.first){ cur.second+=cnt[j]; } } reverse(adj[i].begin(),adj[i].end()); cur = {-1e9,0}; for(auto j:adj[i]){ if(j==pr)continue; if(cur.first>pref[j].first)pref[j] = cur; else if(cur.first==pref[j].first)pref[j].second+=cur.second; if(mx[j]+1>cur.first){ cur.first = mx[j]+1; cur.second = 0; } if(mx[j]+1==cur.first){ cur.second+=cnt[j]; } } reverse(adj[i].begin(),adj[i].end()); for(auto j:adj[i]){ if(j==pr)continue; reroot(j,i,pref[j].first+1,pref[j].second); } } int main(){ int n; cin>>n; for(int i = 1;i<n;i++){ int a,b; cin>>a>>b; adj[a].push_back(b); adj[b].push_back(a); } dfs(1,0); reroot(1,0,0,1); cout<<overMA<<" "<<overCNT<<endl; }

Compilation message (stderr)

road.cpp: In function 'void reroot(int, int, int, int)':
road.cpp:38:28: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   38 |             for(int i = 3;i<v.size();i++)if(v[i].first==x)C+=v[i].second;
      |                           ~^~~~~~~~~
road.cpp:37:17: warning: unused variable 'A' [-Wunused-variable]
   37 |             int A = v[0].second , B = v[1].second , C = v[2].second;
      |                 ^
road.cpp:42:28: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   42 |             for(int i = 3;i<v.size();i++)if(v[i].first==x)C+=v[i].second;
      |                           ~^~~~~~~~~
road.cpp:48:28: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   48 |             for(int i = 3;i<v.size();i++){
      |                           ~^~~~~~~~~
road.cpp:45:17: warning: unused variable 'A' [-Wunused-variable]
   45 |             int A = v[0].second , B = v[1].second , C = v[2].second;
      |                 ^
road.cpp:58:28: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   58 |             for(int i = 3;i<v.size();i++){
      |                           ~^~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...