Submission #94299

#TimeUsernameProblemLanguageResultExecution timeMemory
94299KastandaHard route (IZhO17_road)C++11
100 / 100
1149 ms102028 KiB
#include<bits/stdc++.h> #define pb push_back using namespace std; const int N = 500005; int n, D[N], C[N], D2[N], C2[N]; long long Max, cnt; vector < int > Adj[N]; void DFS(int v, int p) { for (int i = 0; i < Adj[v].size(); i++) if (Adj[v][i] == p) swap(Adj[v][i], Adj[v].back()), Adj[v].pop_back(); if (!Adj[v].size()) { D[v] = 0; C[v] = 1; return ; } for (int &u : Adj[v]) DFS(u, v), D[v] = max(D[v], D[u] + 1); for (int &u : Adj[v]) if (D[v] == D[u] + 1) C[v] += C[u]; } void DFS2(int v, int p) { int Mxd[2] = {D2[v], -1}; int Cnt[2] = {0 , 0}; for (int &u : Adj[v]) { if (D[u] + 1 > Mxd[1]) Mxd[1] = D[u] + 1; if (Mxd[1] > Mxd[0]) swap(Mxd[0], Mxd[1]); } if (D2[v] == Mxd[0]) Cnt[0] += C2[v]; if (D2[v] == Mxd[1]) Cnt[1] += C2[v]; for (int &u : Adj[v]) { if (D[u] + 1 == Mxd[0]) Cnt[0] += C[u]; if (D[u] + 1 == Mxd[1]) Cnt[1] += C[u]; } for (int &u : Adj[v]) { if (D[u] + 1 < Mxd[0]) { D2[u] = Mxd[0] + 1; C2[u] = Cnt[0]; DFS2(u, v); continue; } D2[u] = Mxd[1] + 1; C2[u] = Cnt[1]; if (D[u] + 1 == Mxd[1]) C2[u] -= C[u]; DFS2(u, v); } } void DFS3(int v, int p) { for (int &u : Adj[v]) DFS3(u, v); int Mxd[4] = {-1, -1, -1, -1}; int Cnt[4] = {0, 0, 0, 0}; if (p != 0) Mxd[0] = D2[v]; for (int &u : Adj[v]) { Mxd[3] = D[u] + 1; for (int i = 2; i >= 0; i --) if (Mxd[i + 1] > Mxd[i]) swap(Mxd[i + 1], Mxd[i]); } if (Mxd[2] == -1) return ; if (p != 0) for (int i = 0; i <= 2; i++) if (D2[v] == Mxd[i]) Cnt[i] += C2[v]; for (int &u : Adj[v]) for (int i = 0; i <= 2; i++) if (D[u] + 1 == Mxd[i]) Cnt[i] += C[u]; long long dist = 1LL * Mxd[0] * (Mxd[1] + Mxd[2]); long long cc = 1LL * Cnt[1] * Cnt[2]; if (Mxd[1] == Mxd[2]) { if (p != 0 && D2[v] == Mxd[1]) cc -= 1LL * C2[v] * C2[v]; for (int &u : Adj[v]) if (D[u] + 1 == Mxd[1]) cc -= 1LL * C[u] * C[u]; cc >>= 1; } if (Max < dist) Max = dist, cnt = 0; if (Max == dist) cnt += cc; } int main() { scanf("%d", &n); for (int i = 1; i <= n - 1; i++) { int v, u; scanf("%d%d", &v, &u); Adj[v].pb(u); Adj[u].pb(v); } if (n <= 2) return !printf("0 1\n"); int root = 0; for (int i = 1; i <= n; i++) if (Adj[i].size() > 1) root = i; DFS(root, 0); DFS2(root, 0); DFS3(root, 0); if (cnt == 0) return !printf("0 1\n"); return !printf("%lld %lld\n", Max, cnt); }

Compilation message (stderr)

road.cpp: In function 'void DFS(int, int)':
road.cpp:10:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for (int i = 0; i < Adj[v].size(); i++)
                     ~~^~~~~~~~~~~~~~~
road.cpp: In function 'int main()':
road.cpp:119:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d", &n);
     ~~~~~^~~~~~~~~~
road.cpp:123:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d%d", &v, &u);
         ~~~~~^~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...