社区讨论
第三个点开O2re,不开wa,求大佬帮下忙
P1352没有上司的舞会参与者 3已保存回复 3
讨论操作
快速查看讨论及其快照的属性,并进行相关操作。
- 当前回复
- 3 条
- 当前快照
- 1 份
- 快照标识符
- @mi6yy4ra
- 此快照首次捕获于
- 2025/11/20 13:06 4 个月前
- 此快照最后确认于
- 2025/11/20 13:06 4 个月前
CPP
#include<iostream>
using namespace std;
const int N=1e4;
struct edge {
int v, next;
} e[N];
int n, r[N], dp[N][2];
int cnt, head[N];
bool vis[N];
void add(int a, int b) {
e[++cnt].v=b;
e[cnt].next=head[a];
head[a]=cnt;
return ;
}
void dfs(int u) {
for (int i=head[u]; i; i=e[i].next) {
int to=e[i].v;
if (vis[to]) continue;
vis[to]=true;
dfs(to);
dp[u][0]+=max(dp[to][1], dp[to][0]);
dp[u][1]+=dp[to][0];
}
dp[u][1]+=r[u];
return ;
}
int main() {
int x, y;
cin >> n;
for (int i=1; i<=n; i++)
cin >> r[i];
while (1) {
cin >> x >> y;
if (x==0 && y==0) break;
add(x, y);
add(y, x);
}
vis[1]=true;
dfs(1);
cout << max(dp[1][0], dp[1][1]);
return 0;
}
回复
共 3 条回复,欢迎继续交流。
正在加载回复...