10分 wa re 求调
查看原帖
10分 wa re 求调
821939
zhi_hui_kan_ti_jie楼主2023/4/23 23:28
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int maxn = 1e5 + 10;
inline pair<int, int>max(pair<int, int>& a, pair<int, int>& b)
{
	
	if (a.first == b.first)
	{
		if (a.first == 0)return{ 0, 0 };
		else return a.second < b.second ? a : b;
	}
	return a.first>b.first ? a : b;
}
struct node
{
	int l, r;
	pair<int, int>info = { 0, 0 }; //sum, id
	node* lson=NULL;
	node* rson=NULL;
}tree[maxn << 6];
node* root[maxn]; int pos = 1;
inline int max(int a, int b){ return a > b ? a : b; }
inline int min(int a, int b){ return a < b ? a : b; }
inline void build(int now,int l, int r)
{
	root[now] = &tree[pos++];
	root[now]->l = l; root[now]->r = r;
}
inline void push_up(node* p)
{
	if (!(p->lson)) p->info = p->rson->info;
	else if (!(p->rson)) p->info = p->lson->info;
	else p->info = max(p->lson->info, p->rson->info);
}
void update(node* p, int x, int v)
{
	int l = p->l, r = p->r;
	if (l == r)
	{
		p->info.first += v;
		p->info.second = l;
		return;
	}
	int mid = l + r >> 1;
	if (x <= mid)
	{
		if (!(p->lson))
		{
			p->lson = &tree[pos++];
			p->lson->l = l;
			p->lson->r = mid;
		}
		update(p->lson, x, v);
	}
	else
	{
		if (!(p->rson))
		{
			p->rson = &tree[pos++];
			p->rson->l = mid+1;
			p->rson->r = r;
		}
		update(p->rson, x, v);
	}
	push_up(p);
}
void merge(node* a, node* b)
{
	if (a->l == a->r)
	{
		a->info.first += b->info.first;
		return;
	}
	if (!(a->lson)) a->lson = b->lson;
	else if (!(b->lson));
	else { merge(a->lson, b->lson); }

	if (!(a->rson)) a->rson = b->rson;
	else if (!(b->rson));
	else { merge(a->rson, b->rson); }
	push_up(a);
}
struct edge
{
	int to;
	int nex;
}e[maxn << 1];

int first[maxn], tot;
void add(int a, int b)
{
	e[++tot].to = b;
	e[tot].nex = first[a];
	first[a] = tot;
}
int sz[maxn], wson[maxn], top[maxn], dfn[maxn], rdfn[maxn], dep[maxn], fa[maxn];
int vistim;
int n, m;
void dfs1(int now, int f, int d)
{
	sz[now] = 1;
	dep[now] = d;
	fa[now] = f;
	build(now, 1, n);
	for (int i = first[now]; i; i = e[i].nex)
	{
		int to = e[i].to;
		if (to == f)continue;
		dfs1(to, now, d + 1);
		if (sz[to] > sz[wson[now]]) wson[now] = to;
		sz[now] += sz[to];
	}
}

void dfs2(int now, int fa, int lst)
{
	dfn[now] = ++vistim;
	rdfn[vistim] = now;
	if (wson[now])
		dfs2(wson[now], now, lst);
	top[now] = lst;
	for (int i = first[now]; i; i = e[i].nex)
	{
		int to = e[i].to;
		if (to == fa || to == wson[now])continue;
		dfs2(to, now, to);
	}
}
int LCA(int a, int b)
{
	while (top[a] != top[b])
	{
		if (dep[top[a]] > dep[top[b]])
		{
			a = fa[top[a]];
		}
		else
		{
			b = fa[top[b]];
		}
	}
	return dep[a]<dep[b] ? a : b;
}

struct query
{
	int z;
	int v;
};

vector<query>q[maxn];
int ans[maxn];
void dfs(int now, int fa)
{
	for (int i = 0; i < q[now].size(); i++)
	{
		update(root[now], q[now][i].z, q[now][i].v);
	}
	for (int i = first[now]; i; i = e[i].nex)
	{
		int to = e[i].to;
		if (to == fa) continue;
		dfs(to, now);
		merge(root[now], root[to]);
	}
	ans[now] = root[now]->info.second;
}

signed main()
{
	ios::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);
	cin >> n >> m;
	int a, b, c;
	for (int i = 1; i < n; i++)
	{
		cin >> a >> b;
		add(a, b); add(b, a);
	}
	dfs1(1, 0, 0); dfs2(1, 0, 1);
	for (int i = 1; i <= m; i++)
	{
		cin >> a >> b >> c;
		int anc = LCA(a, b);
		q[a].push_back({ c, 1 });
		q[b].push_back({ c, 1 });
		q[anc].push_back({ c, -1 });
		q[fa[anc]].push_back({ c, -1 });
	}
	dfs(1, 0);
	for (int i = 1; i <= n; i++)
		cout << ans[i] << '\n';
	return 0;
}
2023/4/23 23:28
加载中...