他说的对,他说的好
#include <iostream>
#include <cstdio>
#include <iomanip>
#include <string>
#include <cstdlib>
#include <cstring>
#include <queue>
#include <set>
#include <vector>
#include <map>
#include <algorithm>
#include <cmath>
#include <stack>
#define INF 0x3f3f3f3f
#define LINF 0x3f3f3f3f3f3f3f3f
#define ll long long
#define ull unsigned long long
#define uint unsigned int
using namespace std;
vector<int> v[1111];
bool bug[1111];
int n;
struct Node {
int leaf, s0, s1;
}node[1111];
bool cmp(int a, int b) {
return (node[a].s0 + 2)*node[b].leaf < (node[b].s0 + 2)*node[a].leaf;
}
void dfs(int u) {
if (v[u].size() == 0) {
node[u].leaf = 1;
node[u].s1 = node[u].s0 = 0;
return;
}
for (int i = 0; i < v[u].size(); i++) {
int now = v[u][i];
dfs(now);
node[u].leaf += node[now].leaf;
if (bug[now]) node[now].s0 = 0;
node[u].s0 += node[now].s0 + 2;
}
sort(v[u].begin(), v[u].end(), cmp);
int leaf = 0, sum = 0;
for (int i = 0; i < v[u].size(); i++) {
int now = v[u][i];
node[u].s1 += node[now].s1 + (sum + 1)*node[now].leaf;
sum += node[now].s0 + 2;
}
}
int main() {
int n, x;
char ch;
while (scanf("%d", &n), n){
memset(bug, 0, sizeof(bug));
memset(node, 0, sizeof(node));
for (int i = 0; i <= n; i++)v[i].clear();
for (int i = 1; i <= n; i++) {
scanf("%d %c", &x, &ch);
if (x != -1)v[x].push_back(i);
if (ch == 'Y') bug[i] = 1;
}
dfs(1);
double ans = (double)node[1].s1 / node[1].leaf;
printf("%.4lf\n", ans);
}
return 0;
}