# | Submission time | Handle | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
93239 | 2019-01-07T10:34:02 Z | SamAnd | Hard route (IZhO17_road) | C++17 | 2 ms | 376 KB |
#include <bits/stdc++.h> using namespace std; const int N = 5003; int n; vector<int> a[N]; int ans[N][N]; int d[N], maxd[N];; void dfs0(int x, int p) { if (x == p) d[x] = 0; else d[x] = d[p] + 1; maxd[x] = d[x]; for (int i = 0; i < a[x].size(); ++i) { int h = a[x][i]; if (h == p) continue; dfs0(h, x); maxd[x] = max(maxd[x], maxd[h]); } } void dfs(int x, int p, int dd, int pp) { multiset<int> s; for (int i = 0; i < a[x].size(); ++i) { int h = a[x][i]; if (h == p) continue; s.insert(maxd[h] - d[x]); } for (int i = 0; i < a[x].size(); ++i) { int h = a[x][i]; if (h == p) continue; s.erase(s.find(maxd[h] - d[x])); if (s.empty()) dfs(h, x, dd, pp); else dfs(h, x, max(dd, *(--s.end())), pp); s.insert(maxd[h] - d[x]); } ans[pp][x] = dd * d[x]; } int main() { freopen("input2.txt", "r", stdin); cin >> n; for (int i = 0; i < n - 1; ++i) { int x, y; cin >> x >> y; a[x].push_back(y); a[y].push_back(x); } for (int x = 1; x <= n; ++x) { if (a[x].size() == 1) { if (x == 6) cout << ""; dfs0(x, x); dfs(x, x, 0, x); } } int maxu = -1; for (int x = 1; x <= n; ++x) { if (a[x].size() > 1) continue; for (int y = 1; y <= n; ++y) { if (a[y].size() > 1) continue; maxu = max(maxu, ans[x][y]); } } int q = 0; for (int x = 1; x <= n; ++x) { if (a[x].size() > 1) continue; for (int y = 1; y <= n; ++y) { if (a[y].size() > 1) continue; if (ans[x][y] == maxu) ++q; } } cout << maxu << ' ' << q << endl; return 0; }
Compilation message
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Incorrect | 2 ms | 376 KB | Output isn't correct |
2 | Halted | 0 ms | 0 KB | - |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Incorrect | 2 ms | 376 KB | Output isn't correct |
2 | Halted | 0 ms | 0 KB | - |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Incorrect | 2 ms | 376 KB | Output isn't correct |
2 | Halted | 0 ms | 0 KB | - |