五彩缤纷的记录:
#include <bits/stdc++.h>
using namespace std;
const int N = 5e5 + 5;
int a[N];
vector <int> G[N];
int n, high = 0;
string ans;
void addedge(int u, int v)
{
G[u].push_back(v);
}
string to_str(int n)
{
string f = "";
while (n)
{
char c = n % 10 + '0';
f += c;
n /= 10;
}
string a = "";
for (int i = f.size() - 1; i >= 0; i --) a += f[i];
return a;
}
void dfs(int fa, int x, int cost, string answ)
{
if (cost > high)
{
high = cost;
ans = answ;
}
for (int i = 0; i < G[x].size(); i ++)
{
if (G[x][i] == fa || a[G[x][i]] == -1) continue;
dfs(x, G[x][i], cost + a[G[x][i]], answ + " " + to_str(G[x][i]));
}
}
int main()
{
int n;
cin >> n >> a[1];
if (a[1] == -1) return 0;
for (int i = 2; i <= n; i ++) scanf("%d", &a[i]);
for (int i = 1; i < n; i ++)
{
int u, v;
scanf("%d%d", &u, &v);
addedge(u, v);
addedge(v, u);
}
dfs(-1, 1, a[1], "1");
cout << ans;
return 0;
}