Submission #94294

# Submission time Handle Problem Language Result Execution time Memory
94294 2019-01-17T11:42:10 Z Kastanda Hard route (IZhO17_road) C++11
0 / 100
12 ms 12152 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] = {D2[v], -1, -1, -1};
    int Cnt[4] = {0, 0, 0, 0};
    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]);
    }

    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];
    if (Mxd[2] == -1)
        return ;

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

    if (Mxd[1] == Mxd[2])
    {
        if (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];
    }

    if (Mxd[1] == Mxd[2])
        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");

    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");
    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 'void DFS3(int, int)':
road.cpp:96:15: warning: unused variable 'sum' [-Wunused-variable]
     long long sum = 0;
               ^~~
road.cpp: In function 'int main()':
road.cpp:118:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d", &n);
     ~~~~~^~~~~~~~~~
road.cpp:122: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 11 ms 12024 KB Output is correct
2 Correct 10 ms 12024 KB Output is correct
3 Correct 10 ms 12152 KB Output is correct
4 Correct 11 ms 12024 KB Output is correct
5 Correct 12 ms 12024 KB Output is correct
6 Correct 11 ms 12024 KB Output is correct
7 Correct 12 ms 12152 KB Output is correct
8 Correct 12 ms 12068 KB Output is correct
9 Incorrect 12 ms 12024 KB Output isn't correct
10 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 11 ms 12024 KB Output is correct
2 Correct 10 ms 12024 KB Output is correct
3 Correct 10 ms 12152 KB Output is correct
4 Correct 11 ms 12024 KB Output is correct
5 Correct 12 ms 12024 KB Output is correct
6 Correct 11 ms 12024 KB Output is correct
7 Correct 12 ms 12152 KB Output is correct
8 Correct 12 ms 12068 KB Output is correct
9 Incorrect 12 ms 12024 KB Output isn't correct
10 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 11 ms 12024 KB Output is correct
2 Correct 10 ms 12024 KB Output is correct
3 Correct 10 ms 12152 KB Output is correct
4 Correct 11 ms 12024 KB Output is correct
5 Correct 12 ms 12024 KB Output is correct
6 Correct 11 ms 12024 KB Output is correct
7 Correct 12 ms 12152 KB Output is correct
8 Correct 12 ms 12068 KB Output is correct
9 Incorrect 12 ms 12024 KB Output isn't correct
10 Halted 0 ms 0 KB -