답안 #1015987

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1015987 2024-07-07T08:13:55 Z codefox Hard route (IZhO17_road) C++14
100 / 100
733 ms 242624 KB
#include <bits/stdc++.h>

using namespace std;

#define int long long
#define pii pair<int, int>

vector<vector<int>> graph;
vector<pii> depth;
pii sol = {0,0};

void add(pii p, pii &s)
{
    if (p.first > s.first)
    {
        s = p;
    }
    else if (p.first == s.first)
    {
        s.second += p.second;
    }
}

void dfsup(int i, int p, pii d)
{
    vector<pii> conn;
    vector<pii> conns;
    for (int ele:graph[i])
    {
        if (ele == p) continue;
        pii m = depth[ele];
        conn.push_back(m);
        conns.push_back(m);
    }
    int b = 0;
    if (conns.empty())
    {
        b = 1;
        conns.push_back({0, 1});
        conn.push_back({0, 1});
    }
    if (d.second) conns.push_back(d);
    if (d.second) conn.push_back(d);
    sort(conns.begin(), conns.end(), greater<pii>());
    #define arr array<int, 4>
    vector<arr> conns2;
    arr curr = {conns[0].first, 1, conns[0].second, 0};
    for (int j = 1; j < conns.size(); j++)
    {
        pii ele = conns[j];
        if (ele.first == curr[0])
        {
            curr[3] += ele.second*curr[2];
            curr[2] += ele.second;
            curr[1]++;
        }
        else
        {
            conns2.push_back(curr);
            curr = {{ele.first, 1, ele.second, 0}};
        }
    }
    conns2.push_back(curr);
    int j = 0;
    for (int ele:graph[i])
    {
        if (ele==p) continue;
        pii pp = {conns2[0][0], conns2[0][2]};
        if (pp.first == conn[j].first) pp.second -= conn[j].second;
        if (pp.second == 0) pp = {conns2[1][0], conns2[1][2]};
        pp.first++;
        dfsup(ele, i, pp);
        j++;
    }
    for (pii ele:conn)
    {
        int k = 0;
        arr p1 = conns2[k];
        int y = (p1[1]>2);
        if (p1[0] == ele.first && p1[1]<=2)
        {
            p1[1]--;
            p1[2]-=ele.second;
            p1[3]-= ele.second*p1[2];
        }
        if (p1[1] == 0)
        {
            k++;
            p1 = conns2[k];
        }
        if (p1[1]>1)
        {
            add({p1[0]*2*ele.first, p1[3]}, sol);
            if (y) break;
            continue;
        }
        k++;
        if (k==conns2.size())
        {
            if (b && p1[0]) add({p1[0]*ele.first, p1[2]}, sol);
            continue;
        }
        arr p2  = conns2[k];
        if (p2[0] == ele.first)
        {
            p2[1]--;
            p2[2]-=ele.second;
            p2[3]-= ele.second*p2[2];
        }
        if (p2[1] == 0)
        {
            k++;
            p2 = conns2[k];
        }
        if (k==conns2.size())
        {
            if (b && p1[0]) add({p1[0]*ele.first, p1[2]}, sol);
            continue;
        }
        add({ele.first*(p1[0]+p2[0]), p1[2]*p2[2]}, sol);
    }
}

pii dfsdown(int i, int p)
{
    pii mx = {0,0};
    if (graph[i].size()==1 && p >= 0) mx = {0, 1};
    for (int ele:graph[i])
    {
        if (ele == p) continue;
        pii m = dfsdown(ele, i);
        add(m, mx);
    }
    mx.first++;
    depth[i] = mx;
    return mx;
}

int32_t main()
{
    int n;
    cin >> n;
    if (n==2)
    {
        cout << 0 << " " << 1;
        return 0;
    }

    graph.assign(n, vector<int>());
    depth.assign(n, {0,0});

    for (int i = 1; i < n; i++)
    {
        int a, b;
        cin >> a >> b;
        a--; b--;
        graph[a].push_back(b);
        graph[b].push_back(a);
    }
    int start = 0;
    for (int i = 0; i < n; i++)
    {
        if (graph[i].size()>1) start = i;
    }

    dfsdown(start, -1);
    dfsup(start, -1, {0,0});


    //get rid of double counting!

    if (sol.first==0) cout << 0 << " " << 1;
    else cout << sol.first << " " << sol.second;
    return 0;
}

Compilation message

road.cpp: In function 'void dfsup(long long int, long long int, std::pair<long long int, long long int>)':
road.cpp:48:23: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   48 |     for (int j = 1; j < conns.size(); j++)
      |                     ~~^~~~~~~~~~~~~~
road.cpp:98:14: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::array<long long int, 4> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   98 |         if (k==conns2.size())
      |             ~^~~~~~~~~~~~~~~
road.cpp:115:14: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::array<long long int, 4> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  115 |         if (k==conns2.size())
      |             ~^~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 344 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 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 1 ms 600 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 0 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 0 ms 348 KB Output is correct
23 Correct 0 ms 348 KB Output is correct
24 Correct 0 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 344 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 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 1 ms 600 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 0 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 0 ms 348 KB Output is correct
23 Correct 0 ms 348 KB Output is correct
24 Correct 0 ms 348 KB Output is correct
25 Correct 4 ms 1624 KB Output is correct
26 Correct 6 ms 1880 KB Output is correct
27 Correct 5 ms 1628 KB Output is correct
28 Correct 3 ms 1628 KB Output is correct
29 Correct 3 ms 1372 KB Output is correct
30 Correct 3 ms 1628 KB Output is correct
31 Correct 3 ms 1752 KB Output is correct
32 Correct 3 ms 1628 KB Output is correct
33 Correct 3 ms 1880 KB Output is correct
34 Correct 3 ms 2140 KB Output is correct
35 Correct 4 ms 1628 KB Output is correct
36 Correct 3 ms 1372 KB Output is correct
37 Correct 4 ms 2140 KB Output is correct
38 Correct 3 ms 2396 KB Output is correct
39 Correct 3 ms 1372 KB Output is correct
40 Correct 3 ms 1116 KB Output is correct
41 Correct 3 ms 860 KB Output is correct
42 Correct 3 ms 604 KB Output is correct
43 Correct 3 ms 604 KB Output is correct
44 Correct 3 ms 604 KB Output is correct
45 Correct 2 ms 604 KB Output is correct
46 Correct 2 ms 604 KB Output is correct
47 Correct 3 ms 860 KB Output is correct
48 Correct 3 ms 1116 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 344 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 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 1 ms 600 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 0 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 0 ms 348 KB Output is correct
23 Correct 0 ms 348 KB Output is correct
24 Correct 0 ms 348 KB Output is correct
25 Correct 4 ms 1624 KB Output is correct
26 Correct 6 ms 1880 KB Output is correct
27 Correct 5 ms 1628 KB Output is correct
28 Correct 3 ms 1628 KB Output is correct
29 Correct 3 ms 1372 KB Output is correct
30 Correct 3 ms 1628 KB Output is correct
31 Correct 3 ms 1752 KB Output is correct
32 Correct 3 ms 1628 KB Output is correct
33 Correct 3 ms 1880 KB Output is correct
34 Correct 3 ms 2140 KB Output is correct
35 Correct 4 ms 1628 KB Output is correct
36 Correct 3 ms 1372 KB Output is correct
37 Correct 4 ms 2140 KB Output is correct
38 Correct 3 ms 2396 KB Output is correct
39 Correct 3 ms 1372 KB Output is correct
40 Correct 3 ms 1116 KB Output is correct
41 Correct 3 ms 860 KB Output is correct
42 Correct 3 ms 604 KB Output is correct
43 Correct 3 ms 604 KB Output is correct
44 Correct 3 ms 604 KB Output is correct
45 Correct 2 ms 604 KB Output is correct
46 Correct 2 ms 604 KB Output is correct
47 Correct 3 ms 860 KB Output is correct
48 Correct 3 ms 1116 KB Output is correct
49 Correct 511 ms 142676 KB Output is correct
50 Correct 479 ms 142672 KB Output is correct
51 Correct 562 ms 140576 KB Output is correct
52 Correct 464 ms 106100 KB Output is correct
53 Correct 468 ms 127568 KB Output is correct
54 Correct 522 ms 182100 KB Output is correct
55 Correct 537 ms 182208 KB Output is correct
56 Correct 454 ms 118608 KB Output is correct
57 Correct 504 ms 164180 KB Output is correct
58 Correct 525 ms 135248 KB Output is correct
59 Correct 496 ms 156688 KB Output is correct
60 Correct 554 ms 185936 KB Output is correct
61 Correct 733 ms 242624 KB Output is correct
62 Correct 713 ms 205396 KB Output is correct
63 Correct 687 ms 89428 KB Output is correct
64 Correct 710 ms 81432 KB Output is correct
65 Correct 618 ms 59532 KB Output is correct
66 Correct 598 ms 47780 KB Output is correct
67 Correct 547 ms 44884 KB Output is correct
68 Correct 642 ms 44624 KB Output is correct
69 Correct 519 ms 43060 KB Output is correct
70 Correct 678 ms 43232 KB Output is correct
71 Correct 692 ms 43088 KB Output is correct
72 Correct 635 ms 42928 KB Output is correct
73 Correct 579 ms 43348 KB Output is correct
74 Correct 644 ms 43908 KB Output is correct
75 Correct 640 ms 45604 KB Output is correct
76 Correct 540 ms 46576 KB Output is correct
77 Correct 433 ms 55744 KB Output is correct
78 Correct 342 ms 62620 KB Output is correct