| 比赛 |
NOIP2025模拟赛4 |
评测结果 |
AAAAAAAAAA |
| 题目名称 |
Good Influencers |
最终得分 |
100 |
| 用户昵称 |
淮淮清子 |
运行时间 |
0.231 s |
| 代码语言 |
C++ |
内存使用 |
7.88 MiB |
| 提交时间 |
2025-11-27 10:52:23 |
显示代码纯文本
#include<iostream>
#include<ctime>
#include<cstring>
using namespace std;
const int MAXN = 2 * 1e5 + 5;
const int INF = 0x3f3f3f3f;
struct node{
int to, nxt;
}e[MAXN << 1];
int h[MAXN], tot = 0;
char color[MAXN];
int n, c[MAXN];
int f[MAXN][2][2];
void add(int x, int y){
e[++ tot] = {y, h[x]};
h[x] = tot;
}
/*
fa
\
u (blue)
/\
v v
f[u][0 / 1][0 / 1]
u | is blue or not | change or not
*/
void dfs(int u, int fa){
memset(f[u], INF, sizeof(f[u]));
if(color[u] == 'Y'){
f[u][1][0] = 0;
f[u][1][1] = c[u];
}
f[u][0][0] = 0;
f[u][0][1] = c[u];
for(int i = h[u];i;i = e[i].nxt){
int v = e[i].to;
if(v == fa) continue;
dfs(v, u);
int tmp[2][2]; memset(tmp, INF, sizeof(tmp));
for(int x = 0;x < 2;++ x){
for(int y = 0;y < 2;++ y){
if(f[u][x][y] == INF) continue;
for(int a = 0;a < 2;++ a){
if(a == 0 && y == 0) continue;
for(int b = 0;b < 2;++ b){
if(f[v][a][b] == INF) continue;
int nw = x | (a & b);
tmp[nw][y] = min(tmp[nw][y], f[u][x][y] + f[v][a][b]);
}
}
}
}
memcpy(f[u], tmp, sizeof(tmp));
}
}
int main(){
freopen("Influencers.in", "r", stdin);
freopen("Influencers.out", "w", stdout);
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n;
for(int i = 1;i < n;++ i){
int u, v; cin >> u >> v;
add(u, v), add(v, u);
}
string s; cin >> s;
for(int i = 1;i <= n;++ i){
color[i] = s[i - 1];
}
for(int i = 1;i <= n;++ i){
cin >> c[i];
}
memset(f, INF, sizeof(f));
dfs(1, 0);
cout << min(f[1][1][0], f[1][1][1]) << '\n';
// cerr << "Time : " << 1.0 * clock() / CLOCKS_PER_SEC << "s \n";
return 0;
}