This commit is contained in:
2025-03-22 21:12:39 +08:00
parent e59ed31afe
commit 504cf57178
5 changed files with 162 additions and 0 deletions

49
ACM/CQUPT-19/I.cpp Normal file
View File

@@ -0,0 +1,49 @@
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int n;
vector<int> credit;
vector<vector<int>> graph;
vector<bool> visited;
pair<long long, long long> dfs(int u, int parent) {
visited[u] = true;
long long notTaken = 0, taken = credit[u];
for (int v : graph[u]) {
if (v == parent) continue;
if (!visited[v]) {
auto child = dfs(v, u);
notTaken += max(child.first, child.second);
taken += child.first;
}
}
return {notTaken, taken};
}
int main(){
cin >> n;
credit.resize(n + 1);
graph.resize(n + 1);
visited.assign(n + 1, false);
for (int i = 1; i <= n; i++){
cin >> credit[i];
}
for (int i = 1; i <= n; i++){
int Ai;
cin >> Ai;
for (int j = 0; j < Ai; j++){
int v;
cin >> v;
graph[i].push_back(v);
graph[v].push_back(i);
}
}
long long ans = 0;
for (int i = 1; i <= n; i++){
if (!visited[i]){
auto res = dfs(i, -1);
ans += max(res.first, res.second);
}
}
cout<<ans<<endl;
return 0;
}