답안 #997629

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
997629 2024-06-12T15:12:54 Z fuad27 Hard route (IZhO17_road) C++17
100 / 100
331 ms 89428 KB
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int N = 500100;
vector<int> g[N];
int n;
pair<int,int> dp[N];
pair<long long, long long> 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) {
    long long a = dp[g[at][0]].first, b = dp[g[at][1]].first, c = dp[g[at][2]].first;
    long long res = a*(b+c);
    long long 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<long long, long long> fr = {1, 1};
  pair<long long, long long> 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(long long int, long long int)':
road.cpp:31:25: warning: unused variable 'cntc' [-Wunused-variable]
   31 |     long long cntb = 0, cntc = 0, totcnt=0;
      |                         ^~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 13656 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 13548 KB Output is correct
6 Correct 2 ms 13660 KB Output is correct
7 Correct 2 ms 13660 KB Output is correct
8 Correct 3 ms 13704 KB Output is correct
9 Correct 3 ms 13660 KB Output is correct
10 Correct 2 ms 13660 KB Output is correct
11 Correct 2 ms 13656 KB Output is correct
12 Correct 2 ms 13696 KB Output is correct
13 Correct 3 ms 13660 KB Output is correct
14 Correct 2 ms 13656 KB Output is correct
15 Correct 2 ms 13660 KB Output is correct
16 Correct 2 ms 13656 KB Output is correct
17 Correct 2 ms 13660 KB Output is correct
18 Correct 2 ms 13704 KB Output is correct
19 Correct 2 ms 13660 KB Output is correct
20 Correct 2 ms 13700 KB Output is correct
21 Correct 2 ms 13660 KB Output is correct
22 Correct 2 ms 13656 KB Output is correct
23 Correct 2 ms 13656 KB Output is correct
24 Correct 2 ms 13660 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 13656 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 13548 KB Output is correct
6 Correct 2 ms 13660 KB Output is correct
7 Correct 2 ms 13660 KB Output is correct
8 Correct 3 ms 13704 KB Output is correct
9 Correct 3 ms 13660 KB Output is correct
10 Correct 2 ms 13660 KB Output is correct
11 Correct 2 ms 13656 KB Output is correct
12 Correct 2 ms 13696 KB Output is correct
13 Correct 3 ms 13660 KB Output is correct
14 Correct 2 ms 13656 KB Output is correct
15 Correct 2 ms 13660 KB Output is correct
16 Correct 2 ms 13656 KB Output is correct
17 Correct 2 ms 13660 KB Output is correct
18 Correct 2 ms 13704 KB Output is correct
19 Correct 2 ms 13660 KB Output is correct
20 Correct 2 ms 13700 KB Output is correct
21 Correct 2 ms 13660 KB Output is correct
22 Correct 2 ms 13656 KB Output is correct
23 Correct 2 ms 13656 KB Output is correct
24 Correct 2 ms 13660 KB Output is correct
25 Correct 4 ms 13916 KB Output is correct
26 Correct 4 ms 14172 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 3 ms 14172 KB Output is correct
31 Correct 3 ms 14228 KB Output is correct
32 Correct 3 ms 14172 KB Output is correct
33 Correct 3 ms 14172 KB Output is correct
34 Correct 3 ms 14172 KB Output is correct
35 Correct 3 ms 14172 KB Output is correct
36 Correct 3 ms 14052 KB Output is correct
37 Correct 3 ms 14172 KB Output is correct
38 Correct 4 ms 14284 KB Output is correct
39 Correct 5 ms 14172 KB Output is correct
40 Correct 4 ms 13916 KB Output is correct
41 Correct 3 ms 13916 KB Output is correct
42 Correct 4 ms 13916 KB Output is correct
43 Correct 3 ms 13912 KB Output is correct
44 Correct 3 ms 13916 KB Output is correct
45 Correct 3 ms 13824 KB Output is correct
46 Correct 3 ms 13944 KB Output is correct
47 Correct 4 ms 13916 KB Output is correct
48 Correct 3 ms 13916 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 13656 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 13548 KB Output is correct
6 Correct 2 ms 13660 KB Output is correct
7 Correct 2 ms 13660 KB Output is correct
8 Correct 3 ms 13704 KB Output is correct
9 Correct 3 ms 13660 KB Output is correct
10 Correct 2 ms 13660 KB Output is correct
11 Correct 2 ms 13656 KB Output is correct
12 Correct 2 ms 13696 KB Output is correct
13 Correct 3 ms 13660 KB Output is correct
14 Correct 2 ms 13656 KB Output is correct
15 Correct 2 ms 13660 KB Output is correct
16 Correct 2 ms 13656 KB Output is correct
17 Correct 2 ms 13660 KB Output is correct
18 Correct 2 ms 13704 KB Output is correct
19 Correct 2 ms 13660 KB Output is correct
20 Correct 2 ms 13700 KB Output is correct
21 Correct 2 ms 13660 KB Output is correct
22 Correct 2 ms 13656 KB Output is correct
23 Correct 2 ms 13656 KB Output is correct
24 Correct 2 ms 13660 KB Output is correct
25 Correct 4 ms 13916 KB Output is correct
26 Correct 4 ms 14172 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 3 ms 14172 KB Output is correct
31 Correct 3 ms 14228 KB Output is correct
32 Correct 3 ms 14172 KB Output is correct
33 Correct 3 ms 14172 KB Output is correct
34 Correct 3 ms 14172 KB Output is correct
35 Correct 3 ms 14172 KB Output is correct
36 Correct 3 ms 14052 KB Output is correct
37 Correct 3 ms 14172 KB Output is correct
38 Correct 4 ms 14284 KB Output is correct
39 Correct 5 ms 14172 KB Output is correct
40 Correct 4 ms 13916 KB Output is correct
41 Correct 3 ms 13916 KB Output is correct
42 Correct 4 ms 13916 KB Output is correct
43 Correct 3 ms 13912 KB Output is correct
44 Correct 3 ms 13916 KB Output is correct
45 Correct 3 ms 13824 KB Output is correct
46 Correct 3 ms 13944 KB Output is correct
47 Correct 4 ms 13916 KB Output is correct
48 Correct 3 ms 13916 KB Output is correct
49 Correct 201 ms 61548 KB Output is correct
50 Correct 199 ms 64852 KB Output is correct
51 Correct 228 ms 67920 KB Output is correct
52 Correct 196 ms 57680 KB Output is correct
53 Correct 172 ms 65620 KB Output is correct
54 Correct 167 ms 68792 KB Output is correct
55 Correct 181 ms 61268 KB Output is correct
56 Correct 181 ms 64336 KB Output is correct
57 Correct 186 ms 68436 KB Output is correct
58 Correct 205 ms 64576 KB Output is correct
59 Correct 196 ms 64636 KB Output is correct
60 Correct 176 ms 62732 KB Output is correct
61 Correct 324 ms 89428 KB Output is correct
62 Correct 322 ms 80980 KB Output is correct
63 Correct 309 ms 56144 KB Output is correct
64 Correct 331 ms 50228 KB Output is correct
65 Correct 313 ms 46428 KB Output is correct
66 Correct 306 ms 44372 KB Output is correct
67 Correct 293 ms 43332 KB Output is correct
68 Correct 247 ms 42820 KB Output is correct
69 Correct 263 ms 42932 KB Output is correct
70 Correct 313 ms 42576 KB Output is correct
71 Correct 285 ms 42580 KB Output is correct
72 Correct 285 ms 42816 KB Output is correct
73 Correct 314 ms 42836 KB Output is correct
74 Correct 282 ms 42752 KB Output is correct
75 Correct 284 ms 43028 KB Output is correct
76 Correct 311 ms 43292 KB Output is correct
77 Correct 216 ms 44480 KB Output is correct
78 Correct 147 ms 46772 KB Output is correct