답안 #171638

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
171638 2019-12-29T12:45:45 Z mcdx9524 Hard route (IZhO17_road) C++14
52 / 100
549 ms 1148 KB
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const int N = 5007;

int n;
vector <int> g[N];
int dep[N];
int root = -1;
int mx = -1, mx_cnt = 0;

void pre_dfs(int v, int pr) {
    dep[v] = 1;
    for (int u : g[v]) {
        if (u != pr) {
            pre_dfs(u, v);
            dep[v] = max(dep[v], dep[u] + 1);
        }
    }
}

void dfs(int v, int pr, int cur_mx, int cnt_n) {
    if ((int) g[v].size() == 1 && v != root) {
        if (cur_mx * cnt_n > mx) {
            mx = cur_mx * cnt_n;
            mx_cnt = 1;
        } else if (cur_mx * cnt_n == mx) {
            mx_cnt++;
        }
        return;
    }
    pair <int, int> fr = {-1, -1};
    pair <int, int> sc = {-1, -1};
    for (int u : g[v]) {
        if (u != pr) {
            if (dep[u] > fr.first) {
                sc = fr;
                fr = {dep[u], u};
            } else if (dep[u] > sc.first) {
                sc = {dep[u], u};
            }
        }
    }
    for (int u : g[v]) {
        if (u != pr) {
            if (sc.first == -1) {
                dfs(u, v, cur_mx, cnt_n + 1);
            } else {
                if (fr.second == u) {
                    int new_mx = cur_mx;
                    new_mx = max(new_mx, sc.first);
                    dfs(u, v, new_mx, cnt_n + 1);
                } else {
                    int new_mx = cur_mx;
                    new_mx = max(new_mx, fr.first);
                    dfs(u, v, new_mx, cnt_n + 1);
                }
            }
        }
    }
}

void calc(int v) {
    root = v;
    memset(dep, sizeof dep, 0);
    pre_dfs(v, 0);
    dfs(v, 0, 0, 0);
}

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    cin >> n;
    for (int i = 0; i < n - 1; i++) {
        int u, v;
        cin >> u >> v;
        g[u].push_back(v);
        g[v].push_back(u);
    }
    for (int i = 1; i <= n; i++) {
        if ((int) g[i].size() == 1) {
            calc(i);
        }
    }
    cout << mx << ' ' << mx_cnt / 2 << '\n';
    return 0;
}

Compilation message

road.cpp: In function 'void calc(int)':
road.cpp:68:30: warning: 'memset' used with constant zero length parameter; this could be due to transposed parameters [-Wmemset-transposed-args]
     memset(dep, sizeof dep, 0);
                              ^
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 504 KB Output is correct
2 Correct 2 ms 504 KB Output is correct
3 Correct 2 ms 376 KB Output is correct
4 Correct 2 ms 380 KB Output is correct
5 Correct 2 ms 504 KB Output is correct
6 Correct 2 ms 376 KB Output is correct
7 Correct 2 ms 376 KB Output is correct
8 Correct 2 ms 376 KB Output is correct
9 Correct 2 ms 376 KB Output is correct
10 Correct 2 ms 504 KB Output is correct
11 Correct 2 ms 504 KB Output is correct
12 Correct 2 ms 376 KB Output is correct
13 Correct 2 ms 376 KB Output is correct
14 Correct 2 ms 504 KB Output is correct
15 Correct 2 ms 504 KB Output is correct
16 Correct 2 ms 504 KB Output is correct
17 Correct 2 ms 504 KB Output is correct
18 Correct 2 ms 504 KB Output is correct
19 Correct 2 ms 376 KB Output is correct
20 Correct 2 ms 504 KB Output is correct
21 Correct 2 ms 376 KB Output is correct
22 Correct 2 ms 380 KB Output is correct
23 Correct 2 ms 504 KB Output is correct
24 Correct 2 ms 504 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 504 KB Output is correct
2 Correct 2 ms 504 KB Output is correct
3 Correct 2 ms 376 KB Output is correct
4 Correct 2 ms 380 KB Output is correct
5 Correct 2 ms 504 KB Output is correct
6 Correct 2 ms 376 KB Output is correct
7 Correct 2 ms 376 KB Output is correct
8 Correct 2 ms 376 KB Output is correct
9 Correct 2 ms 376 KB Output is correct
10 Correct 2 ms 504 KB Output is correct
11 Correct 2 ms 504 KB Output is correct
12 Correct 2 ms 376 KB Output is correct
13 Correct 2 ms 376 KB Output is correct
14 Correct 2 ms 504 KB Output is correct
15 Correct 2 ms 504 KB Output is correct
16 Correct 2 ms 504 KB Output is correct
17 Correct 2 ms 504 KB Output is correct
18 Correct 2 ms 504 KB Output is correct
19 Correct 2 ms 376 KB Output is correct
20 Correct 2 ms 504 KB Output is correct
21 Correct 2 ms 376 KB Output is correct
22 Correct 2 ms 380 KB Output is correct
23 Correct 2 ms 504 KB Output is correct
24 Correct 2 ms 504 KB Output is correct
25 Correct 317 ms 888 KB Output is correct
26 Correct 309 ms 988 KB Output is correct
27 Correct 308 ms 888 KB Output is correct
28 Correct 306 ms 860 KB Output is correct
29 Correct 549 ms 988 KB Output is correct
30 Correct 411 ms 988 KB Output is correct
31 Correct 416 ms 992 KB Output is correct
32 Correct 407 ms 888 KB Output is correct
33 Correct 7 ms 1148 KB Output is correct
34 Correct 7 ms 888 KB Output is correct
35 Correct 7 ms 1016 KB Output is correct
36 Correct 7 ms 992 KB Output is correct
37 Correct 5 ms 1144 KB Output is correct
38 Correct 5 ms 1016 KB Output is correct
39 Correct 6 ms 888 KB Output is correct
40 Correct 8 ms 732 KB Output is correct
41 Correct 12 ms 760 KB Output is correct
42 Correct 21 ms 632 KB Output is correct
43 Correct 39 ms 632 KB Output is correct
44 Correct 67 ms 632 KB Output is correct
45 Correct 131 ms 632 KB Output is correct
46 Correct 167 ms 760 KB Output is correct
47 Correct 399 ms 760 KB Output is correct
48 Correct 402 ms 756 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 504 KB Output is correct
2 Correct 2 ms 504 KB Output is correct
3 Correct 2 ms 376 KB Output is correct
4 Correct 2 ms 380 KB Output is correct
5 Correct 2 ms 504 KB Output is correct
6 Correct 2 ms 376 KB Output is correct
7 Correct 2 ms 376 KB Output is correct
8 Correct 2 ms 376 KB Output is correct
9 Correct 2 ms 376 KB Output is correct
10 Correct 2 ms 504 KB Output is correct
11 Correct 2 ms 504 KB Output is correct
12 Correct 2 ms 376 KB Output is correct
13 Correct 2 ms 376 KB Output is correct
14 Correct 2 ms 504 KB Output is correct
15 Correct 2 ms 504 KB Output is correct
16 Correct 2 ms 504 KB Output is correct
17 Correct 2 ms 504 KB Output is correct
18 Correct 2 ms 504 KB Output is correct
19 Correct 2 ms 376 KB Output is correct
20 Correct 2 ms 504 KB Output is correct
21 Correct 2 ms 376 KB Output is correct
22 Correct 2 ms 380 KB Output is correct
23 Correct 2 ms 504 KB Output is correct
24 Correct 2 ms 504 KB Output is correct
25 Correct 317 ms 888 KB Output is correct
26 Correct 309 ms 988 KB Output is correct
27 Correct 308 ms 888 KB Output is correct
28 Correct 306 ms 860 KB Output is correct
29 Correct 549 ms 988 KB Output is correct
30 Correct 411 ms 988 KB Output is correct
31 Correct 416 ms 992 KB Output is correct
32 Correct 407 ms 888 KB Output is correct
33 Correct 7 ms 1148 KB Output is correct
34 Correct 7 ms 888 KB Output is correct
35 Correct 7 ms 1016 KB Output is correct
36 Correct 7 ms 992 KB Output is correct
37 Correct 5 ms 1144 KB Output is correct
38 Correct 5 ms 1016 KB Output is correct
39 Correct 6 ms 888 KB Output is correct
40 Correct 8 ms 732 KB Output is correct
41 Correct 12 ms 760 KB Output is correct
42 Correct 21 ms 632 KB Output is correct
43 Correct 39 ms 632 KB Output is correct
44 Correct 67 ms 632 KB Output is correct
45 Correct 131 ms 632 KB Output is correct
46 Correct 167 ms 760 KB Output is correct
47 Correct 399 ms 760 KB Output is correct
48 Correct 402 ms 756 KB Output is correct
49 Runtime error 3 ms 760 KB Execution killed with signal 11 (could be triggered by violating memory limits)
50 Halted 0 ms 0 KB -