Submission #216818

# Submission time Handle Problem Language Result Execution time Memory
216818 2020-03-28T06:24:13 Z quocnguyen1012 Hard route (IZhO17_road) C++14
52 / 100
2000 ms 58680 KB
#include <bits/stdc++.h>

#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define eb emplace_back

using namespace std;
typedef long long ll;
typedef pair<int, int> ii;

const int maxn = 5e5 + 5;

vector<int> adj[maxn];
int N;
ii dist[maxn];

void prepare(int u, int p = -1)
{
  dist[u] = mp(0, 0);
  for(int v : adj[u]) if(v != p){
    prepare(v, u);
    int now = dist[v].fi + 1;
    if(dist[u].fi < now){
      dist[u].se = dist[u].fi;
      dist[u].fi = now;
    }
    else if(dist[u].se < now){
      dist[u].se = now;
    }
  }
}

pair<ll, int> dfs(int u, int p = -1, int dep = 0, int mx = 0)
{
  if(adj[u].size() == 1 && p != -1)
    return mp(1ll * dep * mx, 1);
  pair<ll, int> res = mp(0, 0);
  for(int v : adj[u]) if(v != p){
    pair<ll, int> val;
    if(dist[u].fi == dist[v].fi + 1)
      val = dfs(v, u, dep + 1, max(mx, dist[u].se));
    else val = dfs(v, u, dep + 1, max(mx, dist[u].fi));
    if(val.fi > res.fi) res = val;
    else if(val.fi == res.fi) res.se += val.se;
  }
  return res;
}

signed main(void)
{
  ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  #ifdef LOCAL
    freopen("A.INP", "r", stdin);
    freopen("A.OUT", "w", stdout);
  #endif // LOCAL
  cin >> N;
  for(int i = 1; i < N; ++i){
    int u, v; cin >> u >> v;
    adj[u].eb(v); adj[v].eb(u);
  }
  pair<ll, int> res = mp(0, 0);
  for(int i = 1; i <= N; ++i){
    if(adj[i].size() == 1){
      prepare(i);
      auto cur = dfs(i);
      if(res.fi < cur.fi) res = cur;
      else if(res.fi == cur.fi) res.se += cur.se;
    }
  }
  cout << res.fi << ' ' << res.se / 2;
}
# Verdict Execution time Memory Grader output
1 Correct 11 ms 12032 KB Output is correct
2 Correct 11 ms 12032 KB Output is correct
3 Correct 11 ms 12160 KB Output is correct
4 Correct 10 ms 12160 KB Output is correct
5 Correct 11 ms 12032 KB Output is correct
6 Correct 10 ms 12032 KB Output is correct
7 Correct 11 ms 12032 KB Output is correct
8 Correct 11 ms 12032 KB Output is correct
9 Correct 11 ms 12032 KB Output is correct
10 Correct 10 ms 12032 KB Output is correct
11 Correct 11 ms 12032 KB Output is correct
12 Correct 11 ms 12160 KB Output is correct
13 Correct 11 ms 12160 KB Output is correct
14 Correct 11 ms 12160 KB Output is correct
15 Correct 12 ms 12032 KB Output is correct
16 Correct 11 ms 12032 KB Output is correct
17 Correct 11 ms 12032 KB Output is correct
18 Correct 11 ms 12160 KB Output is correct
19 Correct 11 ms 12160 KB Output is correct
20 Correct 11 ms 12032 KB Output is correct
21 Correct 11 ms 12032 KB Output is correct
22 Correct 11 ms 12160 KB Output is correct
23 Correct 12 ms 12136 KB Output is correct
24 Correct 17 ms 12160 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 11 ms 12032 KB Output is correct
2 Correct 11 ms 12032 KB Output is correct
3 Correct 11 ms 12160 KB Output is correct
4 Correct 10 ms 12160 KB Output is correct
5 Correct 11 ms 12032 KB Output is correct
6 Correct 10 ms 12032 KB Output is correct
7 Correct 11 ms 12032 KB Output is correct
8 Correct 11 ms 12032 KB Output is correct
9 Correct 11 ms 12032 KB Output is correct
10 Correct 10 ms 12032 KB Output is correct
11 Correct 11 ms 12032 KB Output is correct
12 Correct 11 ms 12160 KB Output is correct
13 Correct 11 ms 12160 KB Output is correct
14 Correct 11 ms 12160 KB Output is correct
15 Correct 12 ms 12032 KB Output is correct
16 Correct 11 ms 12032 KB Output is correct
17 Correct 11 ms 12032 KB Output is correct
18 Correct 11 ms 12160 KB Output is correct
19 Correct 11 ms 12160 KB Output is correct
20 Correct 11 ms 12032 KB Output is correct
21 Correct 11 ms 12032 KB Output is correct
22 Correct 11 ms 12160 KB Output is correct
23 Correct 12 ms 12136 KB Output is correct
24 Correct 17 ms 12160 KB Output is correct
25 Correct 208 ms 12544 KB Output is correct
26 Correct 205 ms 12664 KB Output is correct
27 Correct 217 ms 12664 KB Output is correct
28 Correct 208 ms 12544 KB Output is correct
29 Correct 265 ms 12664 KB Output is correct
30 Correct 275 ms 12668 KB Output is correct
31 Correct 268 ms 12544 KB Output is correct
32 Correct 277 ms 12664 KB Output is correct
33 Correct 14 ms 12672 KB Output is correct
34 Correct 14 ms 12672 KB Output is correct
35 Correct 14 ms 12672 KB Output is correct
36 Correct 16 ms 12672 KB Output is correct
37 Correct 14 ms 12672 KB Output is correct
38 Correct 13 ms 12672 KB Output is correct
39 Correct 14 ms 12544 KB Output is correct
40 Correct 16 ms 12416 KB Output is correct
41 Correct 17 ms 12416 KB Output is correct
42 Correct 25 ms 12416 KB Output is correct
43 Correct 36 ms 12288 KB Output is correct
44 Correct 58 ms 12288 KB Output is correct
45 Correct 107 ms 12288 KB Output is correct
46 Correct 138 ms 12288 KB Output is correct
47 Correct 304 ms 12416 KB Output is correct
48 Correct 348 ms 12408 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 11 ms 12032 KB Output is correct
2 Correct 11 ms 12032 KB Output is correct
3 Correct 11 ms 12160 KB Output is correct
4 Correct 10 ms 12160 KB Output is correct
5 Correct 11 ms 12032 KB Output is correct
6 Correct 10 ms 12032 KB Output is correct
7 Correct 11 ms 12032 KB Output is correct
8 Correct 11 ms 12032 KB Output is correct
9 Correct 11 ms 12032 KB Output is correct
10 Correct 10 ms 12032 KB Output is correct
11 Correct 11 ms 12032 KB Output is correct
12 Correct 11 ms 12160 KB Output is correct
13 Correct 11 ms 12160 KB Output is correct
14 Correct 11 ms 12160 KB Output is correct
15 Correct 12 ms 12032 KB Output is correct
16 Correct 11 ms 12032 KB Output is correct
17 Correct 11 ms 12032 KB Output is correct
18 Correct 11 ms 12160 KB Output is correct
19 Correct 11 ms 12160 KB Output is correct
20 Correct 11 ms 12032 KB Output is correct
21 Correct 11 ms 12032 KB Output is correct
22 Correct 11 ms 12160 KB Output is correct
23 Correct 12 ms 12136 KB Output is correct
24 Correct 17 ms 12160 KB Output is correct
25 Correct 208 ms 12544 KB Output is correct
26 Correct 205 ms 12664 KB Output is correct
27 Correct 217 ms 12664 KB Output is correct
28 Correct 208 ms 12544 KB Output is correct
29 Correct 265 ms 12664 KB Output is correct
30 Correct 275 ms 12668 KB Output is correct
31 Correct 268 ms 12544 KB Output is correct
32 Correct 277 ms 12664 KB Output is correct
33 Correct 14 ms 12672 KB Output is correct
34 Correct 14 ms 12672 KB Output is correct
35 Correct 14 ms 12672 KB Output is correct
36 Correct 16 ms 12672 KB Output is correct
37 Correct 14 ms 12672 KB Output is correct
38 Correct 13 ms 12672 KB Output is correct
39 Correct 14 ms 12544 KB Output is correct
40 Correct 16 ms 12416 KB Output is correct
41 Correct 17 ms 12416 KB Output is correct
42 Correct 25 ms 12416 KB Output is correct
43 Correct 36 ms 12288 KB Output is correct
44 Correct 58 ms 12288 KB Output is correct
45 Correct 107 ms 12288 KB Output is correct
46 Correct 138 ms 12288 KB Output is correct
47 Correct 304 ms 12416 KB Output is correct
48 Correct 348 ms 12408 KB Output is correct
49 Execution timed out 2082 ms 58680 KB Time limit exceeded
50 Halted 0 ms 0 KB -