Untitled

mail@pastecode.io avatar
unknown
plain_text
19 days ago
719 B
3
Indexable
Never
#include <bits/stdc++.h>

using namespace std;

const int MAXN = 4005; 

vector<int> g[MAXN];
string flags;
bool visited[MAXN];
int ans, u, v;

int DFS(int cur, int par) {
    int sum = 1;
    for (auto nbr: g[cur]) {
        if (nbr != par) sum += DFS(nbr, cur);
    }
    if (flags[cur-1] == 'R') ans = min(ans, sum);
    return sum;
}

int main() {
    ios::sync_with_stdio(false); cin.tie(0);
    int N;
    cin >> N;
    for (int i = 1; i <= N-1; ++i) {
        cin >> u >> v;
        g[u].push_back(v);
        g[v].push_back(u);
    }

    cin >> flags;

    for (int i = 1; i <= N; ++i) {
        ans = INT_MAX;
        DFS(i, -1);
        cout << N - ans << "\n";
    }
}
Leave a Comment