답안 #243700

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
243700 2020-07-01T15:09:14 Z SamAnd Roadside Advertisements (NOI17_roadsideadverts) C++17
7 / 100
73 ms 15072 KB
#include <bits/stdc++.h>
using namespace std;
#define m_p make_pair
#define all(x) (x).begin(),(x).end()
#define sz(x) ((int)(x).size())
#define fi first
#define se second
typedef long long ll;
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
mt19937 rnf(2106);
const int N = 100005;

int n;
vector<int> g[N], gd[N];

const int m = 18;
int p[N][m];
int d[N];

int tin[N], tout[N], ti;

void dfs(int x, int p0)
{
    tin[x] = ++ti;
    p[x][0] = p0;
    for (int i = 1; i < m; ++i)
        p[x][i] = p[p[x][i - 1]][i - 1];
    for (int i = 0; i < g[x].size(); ++i)
    {
        int h = g[x][i];
        if (h == p0)
            continue;
        d[h] = d[x] + gd[x][i];
        dfs(h, x);
    }
    tout[x] = ti;
}

bool isp(int x, int y)
{
    return (tin[x] <= tin[y] && tout[y] <= tout[x]);
}

int lca(int x, int y)
{
    if (isp(x, y))
        return x;
    if (isp(y, x))
        return y;
    for (int i = m - 1; i >= 0; --i)
    {
        if (!isp(p[x][i], y))
            x = p[x][i];
    }
    return p[x][0];
}

bool so(int x, int y)
{
    return tin[x] < tin[y];
}

void solv()
{
    scanf("%d", &n);
    for (int i = 0; i < n - 1; ++i)
    {
        int x, y, d;
        scanf("%d%d%d", &x, &y, &d);
        g[x].push_back(y);
        g[y].push_back(x);
        gd[x].push_back(d);
        gd[y].push_back(d);
    }
    dfs(0, 0);
    int qq;
    scanf("%d", &qq);
    while (qq--)
    {
        int x[5];
        for (int i = 0; i < 5; ++i)
        {
            scanf("%d", &x[i]);
        }
        sort(x, x + 5, so);
        reverse(x, x + 5);
        int ans = 0;
        int y = x[0];
        for (int i = 1; i < 5; ++i)
        {
            int z = lca(x[i], y);
            ans += (d[y] + d[x[i]] - 2 * d[z]);
            y = z;
        }
        printf("%d\n", ans);
    }
}

int main()
{
    #ifdef SOMETHING
    freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
    #endif // SOMETHING
    //ios_base::sync_with_stdio(false), cin.tie(0);
    solv();
    return 0;
}

//while ((double)clock() / CLOCKS_PER_SEC <= 0.9){}

Compilation message

roadsideadverts.cpp: In function 'void dfs(int, int)':
roadsideadverts.cpp:28:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for (int i = 0; i < g[x].size(); ++i)
                     ~~^~~~~~~~~~~~~
roadsideadverts.cpp: In function 'void solv()':
roadsideadverts.cpp:65:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d", &n);
     ~~~~~^~~~~~~~~~
roadsideadverts.cpp:69:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d%d%d", &x, &y, &d);
         ~~~~~^~~~~~~~~~~~~~~~~~~~~~
roadsideadverts.cpp:77:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d", &qq);
     ~~~~~^~~~~~~~~~~
roadsideadverts.cpp:83:18: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
             scanf("%d", &x[i]);
             ~~~~~^~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 4992 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Incorrect 73 ms 15072 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 53 ms 13176 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 4992 KB Output is correct
2 Incorrect 73 ms 15072 KB Output isn't correct
3 Halted 0 ms 0 KB -