답안 #941423

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
941423 2024-03-08T22:14:50 Z TAhmed33 Hard route (IZhO17_road) C++
52 / 100
112 ms 1116 KB
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll inf = -1e9;
const int MAXN = 5e3 + 25;
vector <int> adj[MAXN];
int n;
ll mx = 0, cnt = 1;
pair <ll, ll> dfs (int pos, int par, int dep) {
    if (par != -1 && adj[pos].size() == 1) {
        return {0, 1};
    }
    pair <ll, ll> d1 = {inf, 0}, d2 = {inf, 0};
    pair <ll, ll> ret = {inf, 0};
    bool flag = 0;
    for (auto j : adj[pos]) {
        if (j == par) continue;
        auto h = dfs(j, pos, dep + 1);
       /* if (pos == 1) {
            cout << h.first + 1 << " | " << h.second << '\n';
        }*/
        h.first += 1;
        if (h.first > d1.first) {
            d2 = d1; d1 = h;
        } else if (h.first == d1.first) {
            d1.second += h.second;
        } else if (h.first > d2.first) {
            d2 = h;
        } else if (h.first == d2.first) {
            d2.second += h.second;
        }
        if (h.first > ret.first) {
            ret = h; flag = 0;
        } else if (h.first == ret.first) {
            ret.second += h.second;
            flag = 1;
        }
    }
    if (flag) {
        //cout << pos << "::::\n";
        ll t = (dep + d1.first) * d1.first, s = ret.second;
        if (t > mx) {
            mx = t; cnt = s;
        } else if (t == mx) {
            cnt += s;
        }
        return ret;
    }
    //cout << pos << ":: " << d1.first << " " << d2.first << '\n';
    ll t = (dep + d1.first) * d2.first, s = d1.second;
    //cout << t << " " << s << '\n';
    if (t > mx) {
        mx = t; cnt = s; 
    } else if (t == mx) {
        cnt += s;
    }
    t = (dep + d2.first) * d1.first, s = d2.second;
    //cout << t << " " << s << '\n';
    if (t > mx) {
        mx = t; cnt = s;
    } else if (t == mx) {
        cnt += s;
    }

    return ret;
}
int main () {
    ios::sync_with_stdio(0); cin.tie(0);
    cin >> n;
    for (int i = 1; i < n; i++) {
        int a, b; cin >> a >> b;
        adj[a].push_back(b);
        adj[b].push_back(a);
    }
    for (int i = 1; i <= n; i++) {
        if ((int)adj[i].size() == 1) {
            dfs(i, -1, 0);
        }
        //cout << i << ": " << mx << '\n';
    }
    if (mx == 0) {
        cout << "0 1\n";
        return 0;
    }
    cout << mx << " " << cnt / 2 << '\n';
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 1 ms 344 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 1 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 1 ms 348 KB Output is correct
23 Correct 1 ms 348 KB Output is correct
24 Correct 1 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 1 ms 344 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 1 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 1 ms 348 KB Output is correct
23 Correct 1 ms 348 KB Output is correct
24 Correct 1 ms 348 KB Output is correct
25 Correct 68 ms 1020 KB Output is correct
26 Correct 68 ms 856 KB Output is correct
27 Correct 72 ms 860 KB Output is correct
28 Correct 67 ms 1036 KB Output is correct
29 Correct 99 ms 1108 KB Output is correct
30 Correct 99 ms 856 KB Output is correct
31 Correct 112 ms 860 KB Output is correct
32 Correct 104 ms 860 KB Output is correct
33 Correct 2 ms 1112 KB Output is correct
34 Correct 2 ms 1116 KB Output is correct
35 Correct 2 ms 1072 KB Output is correct
36 Correct 2 ms 1116 KB Output is correct
37 Correct 2 ms 1116 KB Output is correct
38 Correct 2 ms 1116 KB Output is correct
39 Correct 2 ms 888 KB Output is correct
40 Correct 3 ms 860 KB Output is correct
41 Correct 3 ms 600 KB Output is correct
42 Correct 6 ms 604 KB Output is correct
43 Correct 9 ms 788 KB Output is correct
44 Correct 17 ms 600 KB Output is correct
45 Correct 33 ms 600 KB Output is correct
46 Correct 48 ms 600 KB Output is correct
47 Correct 94 ms 600 KB Output is correct
48 Correct 90 ms 788 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 1 ms 344 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 1 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 1 ms 348 KB Output is correct
23 Correct 1 ms 348 KB Output is correct
24 Correct 1 ms 348 KB Output is correct
25 Correct 68 ms 1020 KB Output is correct
26 Correct 68 ms 856 KB Output is correct
27 Correct 72 ms 860 KB Output is correct
28 Correct 67 ms 1036 KB Output is correct
29 Correct 99 ms 1108 KB Output is correct
30 Correct 99 ms 856 KB Output is correct
31 Correct 112 ms 860 KB Output is correct
32 Correct 104 ms 860 KB Output is correct
33 Correct 2 ms 1112 KB Output is correct
34 Correct 2 ms 1116 KB Output is correct
35 Correct 2 ms 1072 KB Output is correct
36 Correct 2 ms 1116 KB Output is correct
37 Correct 2 ms 1116 KB Output is correct
38 Correct 2 ms 1116 KB Output is correct
39 Correct 2 ms 888 KB Output is correct
40 Correct 3 ms 860 KB Output is correct
41 Correct 3 ms 600 KB Output is correct
42 Correct 6 ms 604 KB Output is correct
43 Correct 9 ms 788 KB Output is correct
44 Correct 17 ms 600 KB Output is correct
45 Correct 33 ms 600 KB Output is correct
46 Correct 48 ms 600 KB Output is correct
47 Correct 94 ms 600 KB Output is correct
48 Correct 90 ms 788 KB Output is correct
49 Runtime error 1 ms 860 KB Execution killed with signal 11
50 Halted 0 ms 0 KB -