이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include "crocodile.h"
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
const int maxn = 100005;
int n, m, k;
vector<pair<int, int>> g[maxn];
bool is_leaf[maxn];
bool is_escape[maxn];
int subtree[maxn];
void dfs1(int node, int par)
{
    subtree[node] = is_escape[node];
    for (auto [v, w] : g[node])
    {
        if (v != par)
        {
            dfs1(v, node);
            subtree[node] += subtree[v];
        }
    }
}
int dfs2(int node, int par)
{
    if (is_escape[node] == true)
    {
        return 0;
    }
    int ans = 0;
    for (auto [v, w] : g[node])
    {
        if (v != par)
        {
            if (subtree[v] > 1)
            {
                ans = max(ans, w + dfs2(v, node));
            }
            else if (is_leaf[v] == true)
            {
                ans = max(ans, w);
            }
        }
    }
    return ans;
}
int travel_plan(int N, int M, int R[][2], int L[], int K, int P[])
{
    n = N;
    m = M;
    k = K;
    for (int i = 0; i < m; ++i)
    {
        int x = R[i][0];
        int y = R[i][1];
        int w = L[i];
        g[x].push_back(make_pair(y, w));
        g[y].push_back(make_pair(x, w));
    }
    for (int i = 0; i < n; ++i)
    {
        if (g[i].size() == 1)
        {
            is_leaf[i] = true;
        }
    }
    for (int i = 0; i < k; ++i)
    {
        is_escape[P[i]] = true;
    }
    if (is_escape[0] == true)
    {
        return 0;
    }
    dfs1(0, -1);
    return dfs2(0, -1);
}
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... |