답안 #997632

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
997632 2024-06-12T15:14:53 Z fuad27 Hard route (IZhO17_road) C++17
52 / 100
271 ms 59304 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) {
    int a = dp[g[at][0]].first, b = dp[g[at][1]].first, c = dp[g[at][2]].first;
    int res = a*(b+c);
    int 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:19: warning: unused variable 'cntc' [-Wunused-variable]
   31 |     int cntb = 0, cntc = 0, totcnt=0;
      |                   ^~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 13656 KB Output is correct
2 Correct 3 ms 13888 KB Output is correct
3 Correct 3 ms 13660 KB Output is correct
4 Correct 3 ms 13660 KB Output is correct
5 Correct 4 ms 13888 KB Output is correct
6 Correct 2 ms 13660 KB Output is correct
7 Correct 3 ms 13764 KB Output is correct
8 Correct 3 ms 13660 KB Output is correct
9 Correct 3 ms 13660 KB Output is correct
10 Correct 3 ms 13660 KB Output is correct
11 Correct 3 ms 13660 KB Output is correct
12 Correct 3 ms 13912 KB Output is correct
13 Correct 4 ms 13660 KB Output is correct
14 Correct 4 ms 13660 KB Output is correct
15 Correct 3 ms 13660 KB Output is correct
16 Correct 3 ms 13656 KB Output is correct
17 Correct 2 ms 13660 KB Output is correct
18 Correct 3 ms 13660 KB Output is correct
19 Correct 3 ms 13660 KB Output is correct
20 Correct 3 ms 13884 KB Output is correct
21 Correct 2 ms 13660 KB Output is correct
22 Correct 2 ms 13660 KB Output is correct
23 Correct 2 ms 13660 KB Output is correct
24 Correct 3 ms 13660 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 13656 KB Output is correct
2 Correct 3 ms 13888 KB Output is correct
3 Correct 3 ms 13660 KB Output is correct
4 Correct 3 ms 13660 KB Output is correct
5 Correct 4 ms 13888 KB Output is correct
6 Correct 2 ms 13660 KB Output is correct
7 Correct 3 ms 13764 KB Output is correct
8 Correct 3 ms 13660 KB Output is correct
9 Correct 3 ms 13660 KB Output is correct
10 Correct 3 ms 13660 KB Output is correct
11 Correct 3 ms 13660 KB Output is correct
12 Correct 3 ms 13912 KB Output is correct
13 Correct 4 ms 13660 KB Output is correct
14 Correct 4 ms 13660 KB Output is correct
15 Correct 3 ms 13660 KB Output is correct
16 Correct 3 ms 13656 KB Output is correct
17 Correct 2 ms 13660 KB Output is correct
18 Correct 3 ms 13660 KB Output is correct
19 Correct 3 ms 13660 KB Output is correct
20 Correct 3 ms 13884 KB Output is correct
21 Correct 2 ms 13660 KB Output is correct
22 Correct 2 ms 13660 KB Output is correct
23 Correct 2 ms 13660 KB Output is correct
24 Correct 3 ms 13660 KB Output is correct
25 Correct 5 ms 14172 KB Output is correct
26 Correct 4 ms 14168 KB Output is correct
27 Correct 4 ms 14172 KB Output is correct
28 Correct 4 ms 14172 KB Output is correct
29 Correct 3 ms 14168 KB Output is correct
30 Correct 4 ms 14172 KB Output is correct
31 Correct 4 ms 14172 KB Output is correct
32 Correct 3 ms 14168 KB Output is correct
33 Correct 3 ms 14172 KB Output is correct
34 Correct 4 ms 14168 KB Output is correct
35 Correct 4 ms 14160 KB Output is correct
36 Correct 4 ms 14172 KB Output is correct
37 Correct 5 ms 14408 KB Output is correct
38 Correct 4 ms 14428 KB Output is correct
39 Correct 4 ms 14172 KB Output is correct
40 Correct 3 ms 14172 KB Output is correct
41 Correct 4 ms 13916 KB Output is correct
42 Correct 3 ms 13916 KB Output is correct
43 Correct 4 ms 13916 KB Output is correct
44 Correct 4 ms 13912 KB Output is correct
45 Correct 4 ms 13916 KB Output is correct
46 Correct 3 ms 14020 KB Output is correct
47 Correct 3 ms 14164 KB Output is correct
48 Correct 3 ms 13916 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 13656 KB Output is correct
2 Correct 3 ms 13888 KB Output is correct
3 Correct 3 ms 13660 KB Output is correct
4 Correct 3 ms 13660 KB Output is correct
5 Correct 4 ms 13888 KB Output is correct
6 Correct 2 ms 13660 KB Output is correct
7 Correct 3 ms 13764 KB Output is correct
8 Correct 3 ms 13660 KB Output is correct
9 Correct 3 ms 13660 KB Output is correct
10 Correct 3 ms 13660 KB Output is correct
11 Correct 3 ms 13660 KB Output is correct
12 Correct 3 ms 13912 KB Output is correct
13 Correct 4 ms 13660 KB Output is correct
14 Correct 4 ms 13660 KB Output is correct
15 Correct 3 ms 13660 KB Output is correct
16 Correct 3 ms 13656 KB Output is correct
17 Correct 2 ms 13660 KB Output is correct
18 Correct 3 ms 13660 KB Output is correct
19 Correct 3 ms 13660 KB Output is correct
20 Correct 3 ms 13884 KB Output is correct
21 Correct 2 ms 13660 KB Output is correct
22 Correct 2 ms 13660 KB Output is correct
23 Correct 2 ms 13660 KB Output is correct
24 Correct 3 ms 13660 KB Output is correct
25 Correct 5 ms 14172 KB Output is correct
26 Correct 4 ms 14168 KB Output is correct
27 Correct 4 ms 14172 KB Output is correct
28 Correct 4 ms 14172 KB Output is correct
29 Correct 3 ms 14168 KB Output is correct
30 Correct 4 ms 14172 KB Output is correct
31 Correct 4 ms 14172 KB Output is correct
32 Correct 3 ms 14168 KB Output is correct
33 Correct 3 ms 14172 KB Output is correct
34 Correct 4 ms 14168 KB Output is correct
35 Correct 4 ms 14160 KB Output is correct
36 Correct 4 ms 14172 KB Output is correct
37 Correct 5 ms 14408 KB Output is correct
38 Correct 4 ms 14428 KB Output is correct
39 Correct 4 ms 14172 KB Output is correct
40 Correct 3 ms 14172 KB Output is correct
41 Correct 4 ms 13916 KB Output is correct
42 Correct 3 ms 13916 KB Output is correct
43 Correct 4 ms 13916 KB Output is correct
44 Correct 4 ms 13912 KB Output is correct
45 Correct 4 ms 13916 KB Output is correct
46 Correct 3 ms 14020 KB Output is correct
47 Correct 3 ms 14164 KB Output is correct
48 Correct 3 ms 13916 KB Output is correct
49 Correct 254 ms 53180 KB Output is correct
50 Correct 237 ms 56404 KB Output is correct
51 Correct 271 ms 59304 KB Output is correct
52 Correct 203 ms 49232 KB Output is correct
53 Incorrect 224 ms 54580 KB Output isn't correct
54 Halted 0 ms 0 KB -