Submission #171637

# Submission time Handle Problem Language Result Execution time Memory
171637 2019-12-29T12:44:55 Z mcdx9524 Hard route (IZhO17_road) C++14
19 / 100
2000 ms 25028 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;
map <int, int> cnt;
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) {
        cnt[cur_mx * cnt_n]++;
        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:70:30: warning: 'memset' used with constant zero length parameter; this could be due to transposed parameters [-Wmemset-transposed-args]
     memset(dep, sizeof dep, 0);
                              ^
# Verdict Execution time Memory Grader output
1 Correct 2 ms 504 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 2 ms 504 KB Output is correct
4 Correct 2 ms 508 KB Output is correct
5 Correct 2 ms 500 KB Output is correct
6 Correct 2 ms 376 KB Output is correct
7 Correct 2 ms 476 KB Output is correct
8 Correct 3 ms 496 KB Output is correct
9 Correct 2 ms 504 KB Output is correct
10 Correct 3 ms 504 KB Output is correct
11 Correct 3 ms 504 KB Output is correct
12 Correct 3 ms 504 KB Output is correct
13 Correct 2 ms 504 KB Output is correct
14 Correct 3 ms 504 KB Output is correct
15 Correct 3 ms 508 KB Output is correct
16 Correct 2 ms 376 KB Output is correct
17 Correct 2 ms 504 KB Output is correct
18 Correct 2 ms 380 KB Output is correct
19 Correct 3 ms 504 KB Output is correct
20 Correct 2 ms 504 KB Output is correct
21 Correct 2 ms 504 KB Output is correct
22 Correct 2 ms 504 KB Output is correct
23 Correct 3 ms 376 KB Output is correct
24 Correct 3 ms 376 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 504 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 2 ms 504 KB Output is correct
4 Correct 2 ms 508 KB Output is correct
5 Correct 2 ms 500 KB Output is correct
6 Correct 2 ms 376 KB Output is correct
7 Correct 2 ms 476 KB Output is correct
8 Correct 3 ms 496 KB Output is correct
9 Correct 2 ms 504 KB Output is correct
10 Correct 3 ms 504 KB Output is correct
11 Correct 3 ms 504 KB Output is correct
12 Correct 3 ms 504 KB Output is correct
13 Correct 2 ms 504 KB Output is correct
14 Correct 3 ms 504 KB Output is correct
15 Correct 3 ms 508 KB Output is correct
16 Correct 2 ms 376 KB Output is correct
17 Correct 2 ms 504 KB Output is correct
18 Correct 2 ms 380 KB Output is correct
19 Correct 3 ms 504 KB Output is correct
20 Correct 2 ms 504 KB Output is correct
21 Correct 2 ms 504 KB Output is correct
22 Correct 2 ms 504 KB Output is correct
23 Correct 3 ms 376 KB Output is correct
24 Correct 3 ms 376 KB Output is correct
25 Correct 371 ms 944 KB Output is correct
26 Correct 360 ms 1020 KB Output is correct
27 Correct 367 ms 1020 KB Output is correct
28 Correct 361 ms 888 KB Output is correct
29 Execution timed out 2051 ms 25028 KB Time limit exceeded
30 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 504 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 2 ms 504 KB Output is correct
4 Correct 2 ms 508 KB Output is correct
5 Correct 2 ms 500 KB Output is correct
6 Correct 2 ms 376 KB Output is correct
7 Correct 2 ms 476 KB Output is correct
8 Correct 3 ms 496 KB Output is correct
9 Correct 2 ms 504 KB Output is correct
10 Correct 3 ms 504 KB Output is correct
11 Correct 3 ms 504 KB Output is correct
12 Correct 3 ms 504 KB Output is correct
13 Correct 2 ms 504 KB Output is correct
14 Correct 3 ms 504 KB Output is correct
15 Correct 3 ms 508 KB Output is correct
16 Correct 2 ms 376 KB Output is correct
17 Correct 2 ms 504 KB Output is correct
18 Correct 2 ms 380 KB Output is correct
19 Correct 3 ms 504 KB Output is correct
20 Correct 2 ms 504 KB Output is correct
21 Correct 2 ms 504 KB Output is correct
22 Correct 2 ms 504 KB Output is correct
23 Correct 3 ms 376 KB Output is correct
24 Correct 3 ms 376 KB Output is correct
25 Correct 371 ms 944 KB Output is correct
26 Correct 360 ms 1020 KB Output is correct
27 Correct 367 ms 1020 KB Output is correct
28 Correct 361 ms 888 KB Output is correct
29 Execution timed out 2051 ms 25028 KB Time limit exceeded
30 Halted 0 ms 0 KB -