Language: C++

#include <bits/stdc++.h>

#define MAXN 200005
#define INF 0x3f3f3f3f

typedef long long lint;

using namespace std;

struct data {
	int next, to;
	data(int next = 0, int to = 0):next(next), to(to) {}
}edge[MAXN << 1];

struct node {
	int maxd, state;
	node(int maxd = 0, int state = 0):maxd(maxd), state(state) {}

	bool operator < (const node b) const {
		return maxd < b.maxd;
	}
};

int n; 
int fa[MAXN], state[MAXN], maxd[MAXN], maxu[MAXN], se[MAXN], semaxd[MAXN];
int stated[MAXN], stateu[MAXN], target[MAXN], times[MAXN], deg[MAXN];
int head[MAXN], cnt;
lint ans = 0;

void connect(int u, int v) {
	edge[++cnt] = data(head[u], v);
	head[u] = cnt;
}

void DFS1(int u, int pre) {
	fa[u] = pre;
	times[u] = stated[u] = state[u];
	for (int i = head[u]; i; i = edge[i].next) {
		int v = edge[i].to;
		++deg[u];
		if (v == pre)
			continue;
		DFS1(v, u);
		stated[u] |= stated[v];
		if (stated[v]) {
			++times[u];
			target[u] = v;
		}
		if (maxd[v] + 1 > maxd[u]) {
			semaxd[u] = maxd[u];
			maxd[u] = maxd[v] + 1;
		}
		else if (maxd[v] + 1 > semaxd[u]) 
			semaxd[u] = maxd[v] + 1;
	}
}

void DFS2(int u, int pre) {
	if (u != 1) {
		if (maxd[fa[u]] == maxd[u] + 1)
			maxu[u] = semaxd[fa[u]] + 1;
		else 
			maxu[u] = maxd[fa[u]] + 1;
		maxu[u] = max(maxu[u], maxu[fa[u]] + 1);
		if ((stated[fa[u]] && times[fa[u]] > 1) || (stated[fa[u]] && !stated[fa[u]]))
			stateu[u] = 1;
	}
	for (int i = head[u]; i; i = edge[i].next) {
		int v = edge[i].to;
		if (v == pre)
			continue;
		DFS2(v, u);
	}
}

int main() {
	int size=256<<20;
//申请空间
char *p=(char*)malloc(size)+size;
__asm__("movl %0,%%esp\n"::"r"(p));
	freopen("1_01.txt", "r", stdin);
	freopen("a.txt", "w", stdout);
	scanf("%d", &n);
	for (int i = 1; i < n; ++i) {
		int u, v;
		scanf("%d%d", &u, &v);
		connect(u, v);
		connect(v, u);
	}
	for (int i = 1; i <= n; ++i)
		scanf("%1d", &state[i]);
	DFS1(1, 0);
	DFS2(1, 0);
	for (int u = 1; u <= n; ++u) {
		if ((maxd[u] < maxu[u] - 1 && stated[u]) || (maxd[u] > maxu[u] - 1 && stateu[u]) || (maxd[u] == maxu[u] - 1 && (stateu[u] || stated[u]))) {
			++ans;
			// cout << u << " type1\n";
		}
		int up = 0;
		if (maxu[u] > maxd[u])
			up = maxd[u];
		else 
			up = max(maxu[u], semaxd[u]);
		if (state[u]) {
			ans += up + 1;
			// cout << u << " type2\n";
		}
		else {
			int down = stateu[u] ? maxu[u] : INF;
			for (int i = head[u]; i; i = edge[i].next) {
				int v = edge[i].to;
				if (v == fa[u])
					continue;
				if (stated[v])
					down = min(down, maxd[v] + 1);
			}
			// cout << u << " type3\n";
			ans += max(0, up - down + 1);
		}
		// cout  << deg[u] << " " << maxd[u] << " " << maxu[u] << endl;
	}
	printf("%lld\n", ans);
}

Compiler output:
a.cc: In function 'void connect(int, int)':
a.cc:31:23: error: reference to 'data' is ambiguous
   31 |         edge[++cnt] = data(head[u], v);
      |                       ^~~~
In file included from /usr/include/c++/14/string:53,
                 from /usr/include/c++/14/bitset:52,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:52,
                 from a.cc:1:
/usr/include/c++/14/bits/range_access.h:344:5: note: candidates are: 'template<class _Tp> constexpr const _Tp* std::data(initializer_list<_Tp>)'
  344 |     data(initializer_list<_Tp> __il) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:334:5: note:                 'template<class _Tp, long unsigned int _Nm> constexpr _Tp* std::data(_Tp (&)[_Nm])'
  334 |     data(_Tp (&__array)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:323:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(const _Container&)'
  323 |     data(const _Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:312:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(_Container&)'
  312 |     data(_Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
a.cc:10:8: note:                 'struct data'
   10 | struct data {
      |        ^~~~