Submission #94299

# Submission time Handle Problem Language Result Execution time Memory
94299 2019-01-17T11:53:41 Z Kastanda Hard route (IZhO17_road) C++11
100 / 100
1149 ms 102028 KB
#include<bits/stdc++.h>
#define pb push_back
using namespace std;
const int N = 500005;
int n, D[N], C[N], D2[N], C2[N];
long long Max, cnt;
vector < int > Adj[N];
void DFS(int v, int p)
{
    for (int i = 0; i < Adj[v].size(); i++)
        if (Adj[v][i] == p)
            swap(Adj[v][i], Adj[v].back()),
            Adj[v].pop_back();

    if (!Adj[v].size())
    {
        D[v] = 0;
        C[v] = 1;
        return ;
    }
    for (int &u : Adj[v])
        DFS(u, v), D[v] = max(D[v], D[u] + 1);
    for (int &u : Adj[v])
        if (D[v] == D[u] + 1)
            C[v] += C[u];
}
void DFS2(int v, int p)
{
    int Mxd[2] = {D2[v], -1};
    int Cnt[2] = {0 , 0};

    for (int &u : Adj[v])
    {
        if (D[u] + 1 > Mxd[1])
            Mxd[1] = D[u] + 1;
        if (Mxd[1] > Mxd[0])
            swap(Mxd[0], Mxd[1]);
    }
    if (D2[v] == Mxd[0])
        Cnt[0] += C2[v];
    if (D2[v] == Mxd[1])
        Cnt[1] += C2[v];

    for (int &u : Adj[v])
    {
        if (D[u] + 1 == Mxd[0])
            Cnt[0] += C[u];
        if (D[u] + 1 == Mxd[1])
            Cnt[1] += C[u];
    }

    for (int &u : Adj[v])
    {
        if (D[u] + 1 < Mxd[0])
        {
            D2[u] = Mxd[0] + 1;
            C2[u] = Cnt[0];
            DFS2(u, v);
            continue;
        }
        D2[u] = Mxd[1] + 1;
        C2[u] = Cnt[1];
        if (D[u] + 1 == Mxd[1])
            C2[u] -= C[u];
        DFS2(u, v);
    }
}
void DFS3(int v, int p)
{
    for (int &u : Adj[v])
        DFS3(u, v);

    int Mxd[4] = {-1, -1, -1, -1};
    int Cnt[4] = {0, 0, 0, 0};

    if (p != 0) Mxd[0] = D2[v];

    for (int &u : Adj[v])
    {
        Mxd[3] = D[u] + 1;
        for (int i = 2; i >= 0; i --)
            if (Mxd[i + 1] > Mxd[i])
                swap(Mxd[i + 1], Mxd[i]);
    }
    if (Mxd[2] == -1)
        return ;

    if (p != 0)
        for (int i = 0; i <= 2; i++)
            if (D2[v] == Mxd[i])
                Cnt[i] += C2[v];

    for (int &u : Adj[v])
        for (int i = 0; i <= 2; i++)
            if (D[u] + 1 == Mxd[i])
                Cnt[i] += C[u];

    long long dist = 1LL * Mxd[0] * (Mxd[1] + Mxd[2]);
    long long cc = 1LL * Cnt[1] * Cnt[2];

    if (Mxd[1] == Mxd[2])
    {
        if (p != 0 && D2[v] == Mxd[1])
            cc -= 1LL * C2[v] * C2[v];
        for (int &u : Adj[v])
            if (D[u] + 1 == Mxd[1])
                cc -= 1LL * C[u] * C[u];
        cc >>= 1;
    }

    if (Max < dist)
        Max = dist, cnt = 0;

    if (Max == dist)
        cnt += cc;
}
int main()
{
    scanf("%d", &n);
    for (int i = 1; i <= n - 1; i++)
    {
        int v, u;
        scanf("%d%d", &v, &u);
        Adj[v].pb(u);
        Adj[u].pb(v);
    }
    if (n <= 2)
        return !printf("0 1\n");

    int root = 0;
    for (int i = 1; i <= n; i++)
        if (Adj[i].size() > 1)
            root = i;

    DFS(root, 0);
    DFS2(root, 0);
    DFS3(root, 0);

    if (cnt == 0)
        return !printf("0 1\n");
    return !printf("%lld %lld\n", Max, cnt);
}

Compilation message

road.cpp: In function 'void DFS(int, int)':
road.cpp:10:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for (int i = 0; i < Adj[v].size(); i++)
                     ~~^~~~~~~~~~~~~~~
road.cpp: In function 'int main()':
road.cpp:119:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d", &n);
     ~~~~~^~~~~~~~~~
road.cpp:123:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d%d", &v, &u);
         ~~~~~^~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 12 ms 12024 KB Output is correct
2 Correct 11 ms 12152 KB Output is correct
3 Correct 11 ms 12028 KB Output is correct
4 Correct 12 ms 12152 KB Output is correct
5 Correct 12 ms 12024 KB Output is correct
6 Correct 12 ms 12024 KB Output is correct
7 Correct 11 ms 12024 KB Output is correct
8 Correct 11 ms 12024 KB Output is correct
9 Correct 11 ms 12024 KB Output is correct
10 Correct 12 ms 12028 KB Output is correct
11 Correct 11 ms 12152 KB Output is correct
12 Correct 12 ms 12152 KB Output is correct
13 Correct 12 ms 12156 KB Output is correct
14 Correct 10 ms 12024 KB Output is correct
15 Correct 11 ms 12152 KB Output is correct
16 Correct 12 ms 12152 KB Output is correct
17 Correct 12 ms 12152 KB Output is correct
18 Correct 10 ms 12152 KB Output is correct
19 Correct 10 ms 12024 KB Output is correct
20 Correct 10 ms 12152 KB Output is correct
21 Correct 10 ms 12152 KB Output is correct
22 Correct 10 ms 12152 KB Output is correct
23 Correct 12 ms 12024 KB Output is correct
24 Correct 12 ms 12152 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 12 ms 12024 KB Output is correct
2 Correct 11 ms 12152 KB Output is correct
3 Correct 11 ms 12028 KB Output is correct
4 Correct 12 ms 12152 KB Output is correct
5 Correct 12 ms 12024 KB Output is correct
6 Correct 12 ms 12024 KB Output is correct
7 Correct 11 ms 12024 KB Output is correct
8 Correct 11 ms 12024 KB Output is correct
9 Correct 11 ms 12024 KB Output is correct
10 Correct 12 ms 12028 KB Output is correct
11 Correct 11 ms 12152 KB Output is correct
12 Correct 12 ms 12152 KB Output is correct
13 Correct 12 ms 12156 KB Output is correct
14 Correct 10 ms 12024 KB Output is correct
15 Correct 11 ms 12152 KB Output is correct
16 Correct 12 ms 12152 KB Output is correct
17 Correct 12 ms 12152 KB Output is correct
18 Correct 10 ms 12152 KB Output is correct
19 Correct 10 ms 12024 KB Output is correct
20 Correct 10 ms 12152 KB Output is correct
21 Correct 10 ms 12152 KB Output is correct
22 Correct 10 ms 12152 KB Output is correct
23 Correct 12 ms 12024 KB Output is correct
24 Correct 12 ms 12152 KB Output is correct
25 Correct 15 ms 12664 KB Output is correct
26 Correct 13 ms 12664 KB Output is correct
27 Correct 12 ms 12668 KB Output is correct
28 Correct 14 ms 12664 KB Output is correct
29 Correct 14 ms 12536 KB Output is correct
30 Correct 14 ms 12536 KB Output is correct
31 Correct 15 ms 12536 KB Output is correct
32 Correct 15 ms 12664 KB Output is correct
33 Correct 15 ms 12740 KB Output is correct
34 Correct 16 ms 12792 KB Output is correct
35 Correct 15 ms 12664 KB Output is correct
36 Correct 15 ms 12536 KB Output is correct
37 Correct 16 ms 12824 KB Output is correct
38 Correct 15 ms 12968 KB Output is correct
39 Correct 15 ms 12508 KB Output is correct
40 Correct 14 ms 12408 KB Output is correct
41 Correct 15 ms 12408 KB Output is correct
42 Correct 15 ms 12408 KB Output is correct
43 Correct 14 ms 12408 KB Output is correct
44 Correct 15 ms 12408 KB Output is correct
45 Correct 14 ms 12412 KB Output is correct
46 Correct 15 ms 12280 KB Output is correct
47 Correct 15 ms 12408 KB Output is correct
48 Correct 15 ms 12380 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 12 ms 12024 KB Output is correct
2 Correct 11 ms 12152 KB Output is correct
3 Correct 11 ms 12028 KB Output is correct
4 Correct 12 ms 12152 KB Output is correct
5 Correct 12 ms 12024 KB Output is correct
6 Correct 12 ms 12024 KB Output is correct
7 Correct 11 ms 12024 KB Output is correct
8 Correct 11 ms 12024 KB Output is correct
9 Correct 11 ms 12024 KB Output is correct
10 Correct 12 ms 12028 KB Output is correct
11 Correct 11 ms 12152 KB Output is correct
12 Correct 12 ms 12152 KB Output is correct
13 Correct 12 ms 12156 KB Output is correct
14 Correct 10 ms 12024 KB Output is correct
15 Correct 11 ms 12152 KB Output is correct
16 Correct 12 ms 12152 KB Output is correct
17 Correct 12 ms 12152 KB Output is correct
18 Correct 10 ms 12152 KB Output is correct
19 Correct 10 ms 12024 KB Output is correct
20 Correct 10 ms 12152 KB Output is correct
21 Correct 10 ms 12152 KB Output is correct
22 Correct 10 ms 12152 KB Output is correct
23 Correct 12 ms 12024 KB Output is correct
24 Correct 12 ms 12152 KB Output is correct
25 Correct 15 ms 12664 KB Output is correct
26 Correct 13 ms 12664 KB Output is correct
27 Correct 12 ms 12668 KB Output is correct
28 Correct 14 ms 12664 KB Output is correct
29 Correct 14 ms 12536 KB Output is correct
30 Correct 14 ms 12536 KB Output is correct
31 Correct 15 ms 12536 KB Output is correct
32 Correct 15 ms 12664 KB Output is correct
33 Correct 15 ms 12740 KB Output is correct
34 Correct 16 ms 12792 KB Output is correct
35 Correct 15 ms 12664 KB Output is correct
36 Correct 15 ms 12536 KB Output is correct
37 Correct 16 ms 12824 KB Output is correct
38 Correct 15 ms 12968 KB Output is correct
39 Correct 15 ms 12508 KB Output is correct
40 Correct 14 ms 12408 KB Output is correct
41 Correct 15 ms 12408 KB Output is correct
42 Correct 15 ms 12408 KB Output is correct
43 Correct 14 ms 12408 KB Output is correct
44 Correct 15 ms 12408 KB Output is correct
45 Correct 14 ms 12412 KB Output is correct
46 Correct 15 ms 12280 KB Output is correct
47 Correct 15 ms 12408 KB Output is correct
48 Correct 15 ms 12380 KB Output is correct
49 Correct 659 ms 74448 KB Output is correct
50 Correct 564 ms 74332 KB Output is correct
51 Correct 776 ms 73632 KB Output is correct
52 Correct 754 ms 63552 KB Output is correct
53 Correct 743 ms 62968 KB Output is correct
54 Correct 642 ms 73752 KB Output is correct
55 Correct 601 ms 73720 KB Output is correct
56 Correct 527 ms 59100 KB Output is correct
57 Correct 548 ms 78712 KB Output is correct
58 Correct 617 ms 70264 KB Output is correct
59 Correct 614 ms 76520 KB Output is correct
60 Correct 648 ms 85264 KB Output is correct
61 Correct 1149 ms 102028 KB Output is correct
62 Correct 1141 ms 91128 KB Output is correct
63 Correct 1132 ms 56640 KB Output is correct
64 Correct 1107 ms 54340 KB Output is correct
65 Correct 1093 ms 47756 KB Output is correct
66 Correct 1016 ms 44360 KB Output is correct
67 Correct 993 ms 43512 KB Output is correct
68 Correct 1020 ms 43404 KB Output is correct
69 Correct 1038 ms 43044 KB Output is correct
70 Correct 779 ms 42744 KB Output is correct
71 Correct 824 ms 42616 KB Output is correct
72 Correct 1016 ms 42908 KB Output is correct
73 Correct 1101 ms 42900 KB Output is correct
74 Correct 838 ms 43148 KB Output is correct
75 Correct 944 ms 43000 KB Output is correct
76 Correct 971 ms 43120 KB Output is correct
77 Correct 759 ms 43644 KB Output is correct
78 Correct 423 ms 44712 KB Output is correct