跳到正文

题解

题解:P3665 [USACO17OPEN] Switch Grass P

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

文章信息

发布
2025年8月22日
收录
2026年2月2日
更新
2026年11月14日
篇幅
约 8 分钟
题目
P3665

版本

  1. v2当前
  2. v1

P3665 [USACO17OPEN] Switch Grass P

这是一道为数不多的能独自写出来的紫题。

首先,我们能够发现性质一:对于全图中最短的距离,一定是相邻两个点所连的边的长度。

如图:

  • 如果点 22 和 11 是不同的种类,则它产生的贡献是 x1x_1,全图中其它路线一定不会比这个长。
  • 如果点 22 和 11 是同种,对于另外一点 33,假设它是不同种,则其到 22 和 11 的最小距离一定是 min⁡(x2,x3)\min(x_2,x_3),绝对不可能是 x3+x1x_3+x_1 或 x2+x1x_2+x_1。

于是我们发现对于全图中能产生的贡献的,一定是其最小生成树中的边,我们假设已经建出了最小生成树,我们继续利用上面的图来举一环的例子(即利用原边加在树上)。

不妨令 x1<x2<x3x_1 < x_2 < x_3。

  • 如果这三个点都是不同种类,则贡献一定是最小的 x1x_1。
  • 如果这三个点中有两个是同种的,不管怎样,我们最小的距离都与 x3x_3 无关。
  • 都是同种的就不用说了。

依此类推,任意一个连通图中只有最小生成树的边才能够产生贡献。

有了这两个性质,题目就变成了考虑在一棵树上求两个结点都是不同种类的一条边的最小值,支持结点种类的更改。

首先我们建出这棵树,将边上的信息下传给儿子结点作为权值,考虑在每一个结点建一个线段树存每种种类权值的最小值,同时用 multiset 记录每种种类出现了的权值,方便我们修改,同时用 set 维护每一个结点的答案。修改时,我们就分别在用 multiset 与 set 去删除原来的值与更新当前的最小值。

因为用普通线段树会炸空间,我们使用动态开点线段树。

时间复杂度是 O(nlog⁡2n)O(n \log^2 n)

C++
#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int N=2e5+5;
const int INF=1e9;
int n,m,k,q;
int fat[N],a[N],val[N];
struct EDGE{int u,v,w;}e[N];
bool cmp(EDGE px,EDGE py){
	return px.w<py.w;
}
int find(int pos){
	if(fat[pos]==pos) return pos;
	return fat[pos]=find(fat[pos]);
}
struct Data{int v,w;};
vector<Data> to[N];
int root[N];
struct Segment_Tree{
	int cnt=0,tree[N*40],ls[N*40],rs[N*40];
	multiset<int> s[N<<1];
	int h[N*40],ct=0;
	void up(int pos){
		if(!ls[pos]||!rs[pos]) tree[pos]=tree[rs[pos]]+tree[ls[pos]];
		else tree[pos]=min(tree[ls[pos]],tree[rs[pos]]);
	}
	void insert(int &pos,int l,int r,int x,int w)
	{
		if(!pos) pos=++cnt;
		if(l==r){
			if(!h[pos]){
				h[pos]=++ct;
				s[ct].insert(INF);
			}
			int now=h[pos];
			s[now].insert(w);
			tree[pos]=(*s[now].begin());
			return;
		}
		int mid=(l+r)>>1;
		if(x<=mid) insert(ls[pos],l,mid,x,w);
		else insert(rs[pos],mid+1,r,x,w);
		up(pos);
	}
	void update(int pos,int l,int r,int x,int w)
	{
		if(l==r){
			int now=h[pos];
			s[now].erase(s[now].find(w));
			tree[pos]=(*s[now].begin());
			return;
		}
		int mid=(l+r)>>1;
		if(x<=mid) update(ls[pos],l,mid,x,w);
		else update(rs[pos],mid+1,r,x,w);
		up(pos);
	}
	int query(int l,int r,int ql,int qr,int pos)
	{
		if(ql>qr) return INF;
		if(!pos) return INF;
		if(ql<=l&&r<=qr) return tree[pos];
		int mid=(l+r)>>1;
		int res=INF;
		if(ql<=mid) res=query(l,mid,ql,qr,ls[pos]);
		if(qr>mid) res=min(res,query(mid+1,r,ql,qr,rs[pos]));
		return res;
	}
}T;
struct Dat{
	int x,num;
	bool friend operator < (Dat px,Dat py){
		if(px.x!=py.x) return px.x<py.x;
		return px.num<py.num;
	}
};
set<Dat> ans;
int lst[N],mn[N];
void dfs(int pos,int fa)
{
	lst[pos]=fa;
	for(auto now:to[pos])
	{
		int v=now.v,w=now.w;
		if(v==fa) continue;
		dfs(v,pos);
		val[v]=w;
		T.insert(root[pos],1,k,a[v],w);
	}
	int ct1=T.query(1,k,1,a[pos]-1,root[pos]);
	int ct2=T.query(1,k,a[pos]+1,k,root[pos]);
	mn[pos]=min(ct1,ct2);
	ans.insert(Dat{mn[pos],pos});
}
int main(){
	ios::sync_with_stdio(0);
	cin.tie(0);cout.tie(0);
	cin>>n>>m>>k>>q;
	for(int i=1;i<=m;i++)
		cin>>e[i].u>>e[i].v>>e[i].w;
	for(int i=1;i<=n;i++)
		cin>>a[i];
	sort(e+1,e+m+1,cmp);
	for(int i=1;i<=n;i++) fat[i]=i;
	for(int i=1;i<=m;i++)
	{
		int x=find(e[i].u),y=find(e[i].v);
		if(x==y) continue;
		fat[x]=y;
		to[e[i].u].push_back(Data{e[i].v,e[i].w});
		to[e[i].v].push_back(Data{e[i].u,e[i].w});
	}
	for(int i=1;i<=n;i++)
		if(fat[i]==i) dfs(i,0);
	while(q--)
	{
		int x,y;
		cin>>x>>y;
		if(lst[x]==0)
		{
			ans.erase(Dat{mn[x],x});
			a[x]=y;
			int ct1=T.query(1,k,1,a[x]-1,root[x]);
			int ct2=T.query(1,k,a[x]+1,1,root[x]);
			mn[x]=min(ct1,ct2);
			ans.insert(Dat{mn[x],x});
		}
		else
		{
			ans.erase(Dat{mn[x],x});
			ans.erase(Dat{mn[lst[x]],lst[x]});
			T.update(root[lst[x]],1,k,a[x],val[x]);
			a[x]=y;
			T.insert(root[lst[x]],1,k,a[x],val[x]);
			int ct1=T.query(1,k,1,a[x]-1,root[x]);
			int ct2=T.query(1,k,a[x]+1,k,root[x]);
			mn[x]=min(ct1,ct2);
			ans.insert(Dat{mn[x],x});
			ct1=T.query(1,k,1,a[lst[x]]-1,root[lst[x]]);
			ct2=T.query(1,k,a[lst[x]]+1,k,root[lst[x]]);
			mn[lst[x]]=min(ct1,ct2);
			ans.insert(Dat{mn[lst[x]],lst[x]});
		}
		int now=(*ans.begin()).x;
		cout<<now<<"\n";
	}
	return 0;
}
/*
*/
原文
存档评论2 条抓取于