제출 #342337

#제출 시각아이디문제언어결과실행 시간메모리
342337cowHard route (IZhO17_road)C++14
0 / 100
10 ms12276 KiB
// // main.cpp // Hard Paths // // Created by Isaac Noel on 1/1/21. // #include <iostream> #include <vector> using namespace std; #define mn (int) 5e5+5 int N; vector<int> graph[mn]; int mD[mn][2]; int mLoc, mDist, root; int dist[mn]; int pa[mn]; int ans, ansNum; bool depthB; int dfs(int l, int p){ pa[l] = p; for(int j: graph[l]) if(j!=p) { dist[j] = dist[l]+1; if(dist[j] > mDist){ mDist = dist[j]; mLoc = j; } int depC = dfs(j, l); if(!depthB) continue; if(depC >= mD[l][0]){ mD[l][1] = mD[l][0]; mD[l][0] = depC; } else mD[l][1] = max(mD[l][1], depC); } return depthB ? mD[l][0]+1 : 0; } int dop=1, dPath; void check(int H){ if(H > ans) {ans = H; ansNum = 0;} if(H == ans) {ansNum++;} } void retrace(int loc){ if(dop == dPath) return; if(graph[loc].size() < 3){ if(ans == 0) ansNum = 1; dop++; retrace(pa[loc]); return; } int dist = mD[loc][0]; if(dist == dop) dist = mD[loc][1]; int H = dPath * dist; check(H); H = (dop+dist) * (dPath-dop); check(H); H = (dist + dPath-dop) * dop; check(H); dop++; retrace(pa[loc]); } int main() { cin >> N; for(int i = 0; i < N-1; i++){ int a, b; cin >> a >> b; graph[a].push_back(b); graph[b].push_back(a); } dfs(1,-1); root = mLoc; dist[root] = 0; mDist = 0; depthB = true; dPath = dfs(root, -1)-1; retrace(pa[mLoc]); cout << ans << " " << ansNum; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...