Submission #997633

# Submission time Handle Problem Language Result Execution time Memory
997633 2024-06-12T15:15:26 Z fuad27 Hard route (IZhO17_road) C++17
100 / 100
324 ms 82144 KB
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int N = 500100;
vector<int> g[N];
int n;
pair<int,int> dp[N];
pair<ll,ll> ans={0, 1};
void dfs1(int at, int p) {
  dp[at] = {1, 1};
  for(int to:g[at]) {
    if(to==p)continue;
    dfs1(to, at);
    if(dp[to].first+1 > dp[at].first) {
      dp[at]=dp[to];
      dp[at].first++;
    }
    else if(dp[to].first+1 == dp[at].first) {
      dp[at].second+=dp[to].second;
    }
  }
}

void dfs2(int at, int p) {
  sort(g[at].begin(), g[at].end(), [&](int u, int v) {
    return dp[u].first > dp[v].first;
  });
  if(g[at].size()>=3) {
    ll a = dp[g[at][0]].first, b = dp[g[at][1]].first, c = dp[g[at][2]].first;
    ll res = a*(b+c);
    ll cntb = 0, cntc = 0, totcnt=0;
    for(int to:g[at]) {
      if(c == dp[to].first)totcnt+=cntb*dp[to].second;
      if(b == dp[to].first)cntb+=dp[to].second;
    }
    if(ans.first == res){
      ans.second+=totcnt;
    }
    else if(ans.first < res) {
      ans.first = res;
      ans.second = totcnt;
    }
  }
  pair<ll, ll> fr = {1, 1};
  pair<ll, ll> sc = {1, 1};
  for(int to:g[at]) {
    if(fr.first < dp[to].first+1) {
      fr=dp[to];
      fr.first++;
    }
    else if(fr.first == dp[to].first+1) {
      fr.second+=dp[to].second;
    }
  }
  for(int to:g[at]) {
    if(dp[to].first+1==fr.first)continue;
    if(sc.first < dp[to].first+1) {
      sc=dp[to];
      sc.first++;
    }
    else if(sc.first == dp[to].first+1) {
      sc.second+=dp[to].second;
    }
  }
  for(int to:g[at]) {
    if(to==p)continue;
    if(fr.first == dp[to].first+1 and fr.second  == dp[to].second) {
      dp[at] = sc;
    }
    else if(fr.first == dp[to].first+1) {
      dp[at] = fr;
      dp[at].second -= dp[to].second;
    }
    else {
      dp[at] = fr;
    }
    dfs2(to, at);
  }
}
signed main () {
  cin.tie(0)->sync_with_stdio(0);
  cin >> n;
  for(int i = 1;i<n;i++) {
    int u, v;
    cin >> u >> v;
    g[u].push_back(v);
    g[v].push_back(u);
  }
  dfs1(1, 0);
  dfs2(1, 0);
  cout << ans.first << " " << ans.second << "\n";
}

Compilation message

road.cpp: In function 'void dfs2(int, int)':
road.cpp:31:18: warning: unused variable 'cntc' [-Wunused-variable]
   31 |     ll cntb = 0, cntc = 0, totcnt=0;
      |                  ^~~~
# Verdict Execution time Memory Grader output
1 Correct 3 ms 13660 KB Output is correct
2 Correct 2 ms 13660 KB Output is correct
3 Correct 2 ms 13660 KB Output is correct
4 Correct 2 ms 13660 KB Output is correct
5 Correct 2 ms 13660 KB Output is correct
6 Correct 2 ms 13660 KB Output is correct
7 Correct 2 ms 13768 KB Output is correct
8 Correct 3 ms 13660 KB Output is correct
9 Correct 2 ms 13884 KB Output is correct
10 Correct 2 ms 13660 KB Output is correct
11 Correct 2 ms 13660 KB Output is correct
12 Correct 3 ms 13660 KB Output is correct
13 Correct 3 ms 13660 KB Output is correct
14 Correct 2 ms 13660 KB Output is correct
15 Correct 2 ms 13656 KB Output is correct
16 Correct 2 ms 13660 KB Output is correct
17 Correct 2 ms 13660 KB Output is correct
18 Correct 2 ms 13660 KB Output is correct
19 Correct 3 ms 13656 KB Output is correct
20 Correct 2 ms 13660 KB Output is correct
21 Correct 2 ms 13748 KB Output is correct
22 Correct 2 ms 13656 KB Output is correct
23 Correct 2 ms 13660 KB Output is correct
24 Correct 3 ms 13660 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 13660 KB Output is correct
2 Correct 2 ms 13660 KB Output is correct
3 Correct 2 ms 13660 KB Output is correct
4 Correct 2 ms 13660 KB Output is correct
5 Correct 2 ms 13660 KB Output is correct
6 Correct 2 ms 13660 KB Output is correct
7 Correct 2 ms 13768 KB Output is correct
8 Correct 3 ms 13660 KB Output is correct
9 Correct 2 ms 13884 KB Output is correct
10 Correct 2 ms 13660 KB Output is correct
11 Correct 2 ms 13660 KB Output is correct
12 Correct 3 ms 13660 KB Output is correct
13 Correct 3 ms 13660 KB Output is correct
14 Correct 2 ms 13660 KB Output is correct
15 Correct 2 ms 13656 KB Output is correct
16 Correct 2 ms 13660 KB Output is correct
17 Correct 2 ms 13660 KB Output is correct
18 Correct 2 ms 13660 KB Output is correct
19 Correct 3 ms 13656 KB Output is correct
20 Correct 2 ms 13660 KB Output is correct
21 Correct 2 ms 13748 KB Output is correct
22 Correct 2 ms 13656 KB Output is correct
23 Correct 2 ms 13660 KB Output is correct
24 Correct 3 ms 13660 KB Output is correct
25 Correct 4 ms 14172 KB Output is correct
26 Correct 3 ms 14172 KB Output is correct
27 Correct 3 ms 14172 KB Output is correct
28 Correct 3 ms 14212 KB Output is correct
29 Correct 3 ms 14172 KB Output is correct
30 Correct 3 ms 14172 KB Output is correct
31 Correct 3 ms 14172 KB Output is correct
32 Correct 4 ms 14172 KB Output is correct
33 Correct 3 ms 14172 KB Output is correct
34 Correct 4 ms 14172 KB Output is correct
35 Correct 4 ms 14172 KB Output is correct
36 Correct 3 ms 14172 KB Output is correct
37 Correct 4 ms 14180 KB Output is correct
38 Correct 4 ms 14428 KB Output is correct
39 Correct 4 ms 14004 KB Output is correct
40 Correct 3 ms 13924 KB Output is correct
41 Correct 3 ms 13916 KB Output is correct
42 Correct 3 ms 13916 KB Output is correct
43 Correct 3 ms 13916 KB Output is correct
44 Correct 4 ms 13916 KB Output is correct
45 Correct 4 ms 14016 KB Output is correct
46 Correct 3 ms 13912 KB Output is correct
47 Correct 3 ms 13916 KB Output is correct
48 Correct 3 ms 13916 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 13660 KB Output is correct
2 Correct 2 ms 13660 KB Output is correct
3 Correct 2 ms 13660 KB Output is correct
4 Correct 2 ms 13660 KB Output is correct
5 Correct 2 ms 13660 KB Output is correct
6 Correct 2 ms 13660 KB Output is correct
7 Correct 2 ms 13768 KB Output is correct
8 Correct 3 ms 13660 KB Output is correct
9 Correct 2 ms 13884 KB Output is correct
10 Correct 2 ms 13660 KB Output is correct
11 Correct 2 ms 13660 KB Output is correct
12 Correct 3 ms 13660 KB Output is correct
13 Correct 3 ms 13660 KB Output is correct
14 Correct 2 ms 13660 KB Output is correct
15 Correct 2 ms 13656 KB Output is correct
16 Correct 2 ms 13660 KB Output is correct
17 Correct 2 ms 13660 KB Output is correct
18 Correct 2 ms 13660 KB Output is correct
19 Correct 3 ms 13656 KB Output is correct
20 Correct 2 ms 13660 KB Output is correct
21 Correct 2 ms 13748 KB Output is correct
22 Correct 2 ms 13656 KB Output is correct
23 Correct 2 ms 13660 KB Output is correct
24 Correct 3 ms 13660 KB Output is correct
25 Correct 4 ms 14172 KB Output is correct
26 Correct 3 ms 14172 KB Output is correct
27 Correct 3 ms 14172 KB Output is correct
28 Correct 3 ms 14212 KB Output is correct
29 Correct 3 ms 14172 KB Output is correct
30 Correct 3 ms 14172 KB Output is correct
31 Correct 3 ms 14172 KB Output is correct
32 Correct 4 ms 14172 KB Output is correct
33 Correct 3 ms 14172 KB Output is correct
34 Correct 4 ms 14172 KB Output is correct
35 Correct 4 ms 14172 KB Output is correct
36 Correct 3 ms 14172 KB Output is correct
37 Correct 4 ms 14180 KB Output is correct
38 Correct 4 ms 14428 KB Output is correct
39 Correct 4 ms 14004 KB Output is correct
40 Correct 3 ms 13924 KB Output is correct
41 Correct 3 ms 13916 KB Output is correct
42 Correct 3 ms 13916 KB Output is correct
43 Correct 3 ms 13916 KB Output is correct
44 Correct 4 ms 13916 KB Output is correct
45 Correct 4 ms 14016 KB Output is correct
46 Correct 3 ms 13912 KB Output is correct
47 Correct 3 ms 13916 KB Output is correct
48 Correct 3 ms 13916 KB Output is correct
49 Correct 181 ms 49624 KB Output is correct
50 Correct 172 ms 52560 KB Output is correct
51 Correct 195 ms 55380 KB Output is correct
52 Correct 189 ms 45536 KB Output is correct
53 Correct 151 ms 50768 KB Output is correct
54 Correct 166 ms 57580 KB Output is correct
55 Correct 155 ms 50000 KB Output is correct
56 Correct 165 ms 53328 KB Output is correct
57 Correct 183 ms 61012 KB Output is correct
58 Correct 167 ms 57412 KB Output is correct
59 Correct 172 ms 57476 KB Output is correct
60 Correct 164 ms 55552 KB Output is correct
61 Correct 313 ms 82144 KB Output is correct
62 Correct 296 ms 73812 KB Output is correct
63 Correct 324 ms 48976 KB Output is correct
64 Correct 283 ms 43120 KB Output is correct
65 Correct 248 ms 39380 KB Output is correct
66 Correct 232 ms 37204 KB Output is correct
67 Correct 284 ms 36176 KB Output is correct
68 Correct 223 ms 35708 KB Output is correct
69 Correct 284 ms 35656 KB Output is correct
70 Correct 220 ms 35412 KB Output is correct
71 Correct 265 ms 35496 KB Output is correct
72 Correct 221 ms 35576 KB Output is correct
73 Correct 241 ms 35408 KB Output is correct
74 Correct 262 ms 35736 KB Output is correct
75 Correct 255 ms 35788 KB Output is correct
76 Correct 214 ms 35784 KB Output is correct
77 Correct 205 ms 36572 KB Output is correct
78 Correct 122 ms 37308 KB Output is correct