先预处理出每个点的最近点。。。。然后考虑固定根,对于每个根,从根到子树的路径中任选两条,如果dis[i] + dis[j] < w[j]。那么i就是j的一个合法点。。。。然后递归处理子树即可。。。。扩栈才过的。。。
#include <iostream>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <bitset>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <climits>
#include <cstdlib>
#include <cmath>
#include <time.h>
#define maxn 100005
#define maxm 400005
#define eps 1e-12
#define mod 1000000007
#define INF 0x3f3f3f3f
#define PI (acos(-1.0))
#define lowbit(x) (x&(-x))
#define mp make_pair
#define ls o<<1
#define rs o<<1 | 1
#define lson o<<1, L, mid
#define rson o<<1 | 1, mid+1, R
#pragma comment(linker, "/STACK:102400000,102400000")
#define pii pair<int, int>
typedef long long LL;
typedef unsigned long long ULL;
//typedef int LL;
using namespace std;
LL qpow(LL a, LL b){LL res=1,base=a;while(b){if(b%2)res=res*base;base=base*base;b/=2;}return res;}
LL powmod(LL a, LL b){LL res=1,base=a;while(b){if(b%2)res=res*base%mod;base=base*base%mod;b/=2;}return res;}
// head
struct Edge
{
int v, w;
Edge *next;
}E[maxm], *H[maxn], *edges;
vector<pii> dis, ww;
queue<int> q;
int size[maxn];
bool done[maxn];
int mart[maxn];
int res[maxn];
int inq[maxn];
int mx[maxn];
pii w[maxn];
int n, nsize, root;
void addedges(int u, int v, int w)
{
edges->v = v;
edges->w = w;
edges->next = H[u];
H[u] = edges++;
}
void init()
{
edges = E;
memset(H, 0, sizeof H);
memset(res, 0, sizeof res);
memset(done, 0, sizeof done);
}
pii operator - (pii a, pii b) {
return mp(a.first - b.first, a.second);
}
void getroot(int u, int fa)
{
size[u] = 1, mx[u] = 0;
for(Edge *e = H[u]; e; e = e->next) if(e->v != fa && !done[e->v]) {
int v = e->v;
getroot(v, u);
size[u] += size[v];
mx[u] = max(mx[u], size[v]);
}
mx[u] = max(mx[u], nsize - size[u]);
if(mx[u] < mx[root]) root = u;
}
void getdis(int u, int fa, int dist)
{
dis.push_back(mp(dist, u));
for(Edge *e = H[u]; e; e = e->next) if(e->v != fa && !done[e->v]) getdis(e->v, u, dist + e->w);
}
void calc(int u, int fa, int dist, int flag)
{
dis.clear();
ww.clear();
getdis(u, fa, dist);
for(int i = 0; i < dis.size(); i++)
ww.push_back(w[dis[i].second] - dis[i]);
sort(dis.begin(), dis.end());
sort(ww.begin(), ww.end());
for(int i = 0, j = 0; i < dis.size(); i++) {
while(j < dis.size() && dis[i] >= ww[j]) j++;
res[dis[i].second] += (dis.size() - j) * flag;
}
}
void solve(int u)
{
calc(u, 0, 0, 1);
done[u] = true;
for(Edge *e = H[u]; e; e = e->next) if(!done[e->v]) {
int v = e->v;
calc(v, u, e->w, -1);
mx[0] = nsize = size[v];
getroot(v, root = 0);
solve(root);
}
}
void spfa()
{
for(int i = 1; i <= n; i++) {
w[i] = mp(INF, 0);
if(mart[i]) inq[i] = true, q.push(i), w[i] = mp(0, i);
}
while(!q.empty()) {
int u = q.front();
q.pop();
inq[u] = false;
for(Edge *e = H[u]; e; e = e->next)
if(w[e->v] > mp(w[u].first + e->w, w[u].second)) {
w[e->v] = mp(w[u].first + e->w, w[u].second);
if(!inq[e->v]) q.push(e->v), inq[e->v] = true;
}
}
}
void work()
{
int u, v, W;
for(int i = 1; i < n; i++) {
scanf("%d%d%d", &u, &v, &W);
addedges(u, v, W);
addedges(v, u, W);
}
for(int i = 1; i <= n; i++) scanf("%d", &mart[i]);
spfa();
mx[0] = nsize = n;
getroot(1, root = 0);
solve(root);
int ans = 0;
for(int i = 1; i <= n; i++) if(!mart[i]) ans = max(ans, res[i]);
printf("%d\n", ans);
}
int main()
{
while(scanf("%d", &n)!=EOF) {
init();
work();
}
return 0;
}