跳到正文

题解

CF1904F

Alvin1228约 8 分钟
原文
文章信息与版本

文章信息

发布
2026年9月20日
收录
2026年9月20日
更新
2026年9月20日
篇幅
约 8 分钟

版本

  1. v1当前
C++
#include <iostream>
#include <map>
#include <queue>
#include <algorithm>
#include <vector>
using namespace std;

typedef pair<int, int> PII;
using Function = void (*) (int, int, int);
const int N = 800005, Inf = 2e9;
int Ls[N], Rs[N], Ord[N], Ans[N], Degree[N], Cnt, AnsCnt, OrdCnt;
int F[N], Dfn[N], Top[N], Son[N], Size[N], Depth[N], DfnCnt;
vector<PII> G[N], H[N], Idx;

class OutTree {
	int Root, n, *Ls = nullptr, *Rs = nullptr;
	vector<PII> *G;
	
	int Build(int l, int r) {
		if(l==r)	return l;
		int u=++Cnt, Mid=(l+r)/2;
		Ls[u]=Build(l, Mid), Rs[u]=Build(Mid+1, r);
		G[Ls[u]].push_back({0, u}), G[Rs[u]].push_back({0, u});
		return u;
	}
	
	void AddEdge(int L, int R, int k, int w, int l, int r, int u) {
		if(L>r || l>R)	return ;
		if(L<=l && r<=R)	return void(G[u].push_back({w, k}));
		int Mid=(l+r)/2;
		AddEdge(L, R, k, w, l, Mid, Ls[u]);
		AddEdge(L, R, k, w, Mid+1, r, Rs[u]);
	}
public:
	void Build(int m, vector<PII> *F, int *l, int *r) {
		n=m, G=F, Ls=l, Rs=r, Root=Build(1, n);
	}
	
	void AddEdge(int l, int r, int k, int w) {
		AddEdge(l, r, k, w, 1, n, Root);
	}
} Out;

class InTree {
	int Root, n, *Ls = nullptr, *Rs = nullptr;
	vector<PII> *G;
	int Build(int l, int r) {
		if(l==r)	return l;
		int u=++Cnt, Mid=(l+r)/2;
		Ls[u]=Build(l, Mid), Rs[u]=Build(Mid+1, r);
		G[u].push_back({0, Ls[u]}), G[u].push_back({0, Rs[u]});
		return u;
	}
	
	void AddEdge(int k, int L, int R, int w, int l, int r, int u) {
		if(L>r || l>R)	return ;
		if(L<=l && r<=R)	return void(G[k].push_back({w, u}));
		int Mid=(l+r)/2;
		AddEdge(k, L, R, w, l, Mid, Ls[u]);
		AddEdge(k, L, R, w, Mid+1, r, Rs[u]);
	}
public:
	void Build(int m, vector<PII> *F, int *l, int *r) {
		n=m, G=F, Ls=l, Rs=r, Root=Build(1, n);
	}
	
	void AddEdge(int k, int l, int r, int w) {
		AddEdge(k, l, r, w, 1, n, Root);
	}
} In;

namespace TreeChainDecomposer {
	vector<PII>* G;
	
	void Dfs1(int u, int f) {
		F[u]=f, Depth[u]=Depth[f]+1, Size[u]=1;
		for(PII E:G[u]) if(E.second!=f){
			Dfs1(E.second, u), Size[u]+=Size[E.second];
			if(Size[E.second]>Size[Son[u]])	Son[u]=E.second;
		}
	}
	
	void Dfs2(int u, int top) {
		Dfn[u]=++DfnCnt, Top[u]=top;
		if(Son[u])	Dfs2(Son[u], top);
		
		for(PII E:G[u]) if(E.second!=F[u] && E.second!=Son[u])
			Dfs2(E.second, E.second);
	}
	
	void TCD() {Dfs1(1, 1), Dfs2(1, 1);}
}

void UpdateLCA(int u, int v, int k, Function Func) {
	for(k=Dfn[k];Top[u]!=Top[v];u=F[Top[u]]) {
		if(Depth[Top[u]]<Depth[Top[v]])	swap(u, v);
		if(Dfn[Top[u]]<=k && k<=Dfn[u])	Func(k+1, Dfn[u], k), Func(Dfn[Top[u]], k-1, k);
		else	Func(Dfn[Top[u]], Dfn[u], k);
	}
	
	if(Depth[u]<Depth[v])	swap(u, v);
	if(Dfn[v]<=k && k<=Dfn[u])	Func(k+1, Dfn[u], k), Func(Dfn[v], k-1, k);
	else	Func(Dfn[v], Dfn[u], k);
}

bool TopoDP(vector<PII> *G, int n) {
	for(int u=1;u<=Cnt;u++) for(PII E:G[u])	Degree[E.second]++;
	
	queue<int> Q;
	for(int i=1;i<=Cnt;i++) if(!Degree[i])	Q.push(i);
	
	int Tot=0;
	while(!Q.empty()) {
		int u=Q.front();Q.pop(), Tot++, Ord[u]=++OrdCnt;
		for(PII E:G[u]) if(!--Degree[E.second])	Q.push(E.second);
	}
	return Tot==Cnt;
}

int main() {
	ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); 
	int n, m;	cin>>n>>m;
	for(int i=1, u, v;i<n;i++)
		cin>>u>>v, H[u].push_back({0, v}), H[v].push_back({0, u});
	
	TreeChainDecomposer::G = H;
	TreeChainDecomposer::TCD();
	
	Cnt=n, Out.Build(n, G, Ls, Rs), In.Build(n, G, Ls, Rs);
	for(int i=1;i<=m;i++) {
		int Opt, u, v, k;	cin>>Opt>>u>>v>>k;
		if(Opt==1)
			UpdateLCA(u, v, k, [](int l, int r, int k) {
				if(l<=r)	In.AddEdge(k, l, r, -1);
			});
		else
			UpdateLCA(u, v, k, [](int l, int r, int k) {
				if(l<=r)	Out.AddEdge(l, r, k, -1);
			});
	}
	
	if(!TopoDP(G, n)) {
		cout<<-1<<endl;
		return 0;
	}
	for(int i=1;i<=n;i++)	Idx.push_back({Ord[Dfn[i]], i});
	sort(Idx.begin(), Idx.end());
	for(int i=0;i<Idx.size();i++)	Ans[Idx[i].second]=i+1;
	
	for(int i=1;i<=n;i++)	cout<<Ans[i]<<' ';
	return 0;
}
原文