답안 #94291

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
94291 2019-01-17T10:59:09 Z Kastanda Hard route (IZhO17_road) C++11
0 / 100
12 ms 12156 KB
#include<bits/stdc++.h>
#define pb push_back
using namespace std;
const int N = 500005;
int n, D[N], C[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 ;
    }

    int Mxd[4] = {-1, -1, -1, -1};
    int Cnt[4] = {0, 0, 0, 0};
    for (int &u : Adj[v])
    {
        DFS(u, 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]);
    }
    long long dist = 1LL * Mxd[0] * (Mxd[1] + Mxd[2]);
    long long cc = 0;
    long long sum = 0;

    for (int &u : Adj[v])
    {
        int d = D[u] + 1;
        if (d < Mxd[2])
            continue;

        if (d == Mxd[0])
        {
            cc += 1LL * C[u] * Cnt[1] * Cnt[2];
            if (Mxd[1] == Mxd[2])
                cc -= 1LL * C[u] * sum;
        }
        if (d == Mxd[1])
        {
            cc += 1LL * C[u] * Cnt[0] * Cnt[2];
            if (Mxd[0] == Mxd[2])
                cc -= 1LL * C[u] * sum;
        }
        if (d == Mxd[2])
        {
            cc += 1LL * C[u] * Cnt[0] * Cnt[1];
            if (Mxd[0] == Mxd[1])
                cc -= 1LL * C[u] * sum;
        }

        if (Mxd[0] == Mxd[1] || Mxd[1] == Mxd[2])
            sum += 1LL * C[u] * C[u];

        for (int i = 0; i <= 2; i++)
            if (d == Mxd[i])
                Cnt[i] += C[u];
    }
    D[v] = Mxd[0];
    C[v] = Cnt[0];
    if (Mxd[2] == -1)
        return ;

    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);

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

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:81:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d", &n);
     ~~~~~^~~~~~~~~~
road.cpp:85:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d%d", &v, &u);
         ~~~~~^~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 12024 KB Output is correct
2 Correct 12 ms 12152 KB Output is correct
3 Correct 12 ms 12024 KB Output is correct
4 Correct 12 ms 12152 KB Output is correct
5 Correct 12 ms 12152 KB Output is correct
6 Correct 12 ms 12124 KB Output is correct
7 Incorrect 12 ms 12156 KB Output isn't correct
8 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 12024 KB Output is correct
2 Correct 12 ms 12152 KB Output is correct
3 Correct 12 ms 12024 KB Output is correct
4 Correct 12 ms 12152 KB Output is correct
5 Correct 12 ms 12152 KB Output is correct
6 Correct 12 ms 12124 KB Output is correct
7 Incorrect 12 ms 12156 KB Output isn't correct
8 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 12024 KB Output is correct
2 Correct 12 ms 12152 KB Output is correct
3 Correct 12 ms 12024 KB Output is correct
4 Correct 12 ms 12152 KB Output is correct
5 Correct 12 ms 12152 KB Output is correct
6 Correct 12 ms 12124 KB Output is correct
7 Incorrect 12 ms 12156 KB Output isn't correct
8 Halted 0 ms 0 KB -