跳到正文

题解

题解 P6976 【[NEERC2015]Distance on Triangulation】

Soulist约 7 分钟
原文

这是 2025年11月17日 存档的第 1 版,不是当前版本。查看当前版本

文章信息与版本

文章信息

收录
2025年12月12日
更新
2026年7月24日
篇幅
约 7 分钟
题目
P6976

版本

  1. v2当前
  2. v1

写了太久了就水篇题解纪念一下这个题吧。

考虑分治。

将所有查询离线,枚举一条重心边使得其尽可能的将图切割成两个均匀的部分。

然后预处理两个端点到这张图的最短路,对两个部分当作子问题递归(带着查询)。

如果某查询的两个端点在图的两端就不递归。

tips:这份代码处理不了递归到 n≤3n\le 3 的情况,不过这个情况特判一下就 ok 了。

Code:Code:

C++
#include<bits/stdc++.h>
using namespace std ;
#define Next( i, x ) for( int i = head[x]; i; i = e[i].next )
#define rep( i, s, t ) for( int i = (s); i <= (t); ++ i )
#define drep( i, s, t ) for( int i = (t); i >= (s); -- i )
#define mp make_pair
#define pi pair<int, int>
#define pb push_back
#define vi vector<int>
int gi() {
	char cc = getchar() ; int cn = 0, flus = 1 ;
	while( cc < '0' || cc > '9' ) {  if( cc == '-' ) flus = - flus ; cc = getchar() ; }
	while( cc >= '0' && cc <= '9' )  cn = cn * 10 + cc - '0', cc = getchar() ;
	return cn * flus ;
}
const int N = 1e5 + 5 ; 
int n, t, f[N], g[N] ; 
vector<vector<int>> E ; 
struct node {
	int x, y, ans ; 
} qus[N] ;
queue<int> q ; 
void solve(vector<int> d, vector<int> Id, vector<vector<int>> G) {
	int ua = 0, ub = 0, uz = 0, hf = Id.size() / 2, m = Id.size() ;
	vector<int> dis1, dis2 ; dis1.resize(m), dis2.resize(m) ; -- m ;
	if( m <= 1 ) return ; 
	rep( i, 1, m ) dis1[i] = dis2[i] = -1 ;  
	int cnt = 0, num = 0 ; 
	for(int v : Id) if( v ) f[v] = ++ cnt ;
	for(int i = 1; i < (int)G.size(); ++ i) {
		for( auto v : G[i] ) 
		if( (abs(i - v) > uz) && (abs(i - v) <= hf) ) uz = abs(i - v), ua = i, ub = v ; 
	} if( ua > ub ) swap( ua, ub ) ; 
	for(int i = ua; i <= ub; ++ i) g[i] = 1 ; 
	q.push(ua) ; dis1[ua] = 0 ; 
	while( !q.empty() ) {
		int u = q.front() ; q.pop() ; 
		for(int v : G[u]) if( !(~dis1[v]) ) 
			dis1[v] = dis1[u] + 1, q.push(v) ;  
	}
	q.push(ub) ; dis2[ub] = 0 ; 
	while( !q.empty() ) {
		int u = q.front() ; q.pop() ; 
		for(int v : G[u]) if( !(~dis2[v]) ) 
			dis2[v] = dis2[u] + 1, q.push(v) ;  
	}
	num = ub - ua + 1 ; 
	vector<vector<int>> lG, rG ; 
	vector<int> lq, rq, lId, rId ; 
	lG.resize(num + 1), lId.resize(num + 1) ; //注意有序 
	for(int id : d) {
		if( !id ) continue ; 
		int x = f[qus[id].x], y = f[qus[id].y] ; 
		int z = min(min(dis1[x] + dis2[y], dis1[y] + dis2[x]) + 1, min(dis1[x] + dis1[y], dis2[x] + dis2[y])) ;
		qus[id].ans = min( qus[id].ans, z ) ;  
		if( g[x] ^ g[y] ) continue ; //1 -> l, 0 -> r
		(g[x]) ? lq.pb(id) : rq.pb(id) ; 
	}
	rep( i, 1, m ) g[i] = 0 ;  
	if( m <= 3 ) return ; 
	num = 0 ; 
	for(int i = ua; i <= ub; ++ i) g[i] = ++ num, lId[num] = Id[i] ; 
	for(int i = ua; i <= ub; ++ i) 
		for(int v : G[i]) if( g[v] ) lG[g[i]].pb(g[v]) ; 
	rep( i, ua, ub ) g[i] = 0 ; num = 0 ; 
	for(int i = 1; i <= ua; ++ i) g[i] = ++ num ; 
	for(int i = ub; i <= m; ++ i) g[i] = ++ num ; 
	rId.resize(num + 1), rG.resize(num + 1) ; 
	rep( i, 1, m ) if(g[i]) rId[g[i]] = Id[i] ; 
	for(int i = 1; i <= m; ++ i) {
		if(!g[i]) continue ; 
		for(int v : G[i]) if( g[v] ) rG[g[i]].pb(g[v]) ; 
	}
	rep( i, 1, m ) g[i] = 0 ;  
	solve(lq, lId, lG), solve(rq, rId, rG) ; 
}
signed main()
{
	n = gi() ; int x, y ; E.resize(n + 1) ; 
	rep( i, 4, n ) x = gi(), y = gi(), E[x].pb(y), E[y].pb(x) ; 
	rep( i, 2, n ) E[i].pb(i - 1), E[i - 1].pb(i) ;
	E[n].pb(1), E[1].pb(n) ; 
	t = gi() ; vi d, Id ; d.resize(t + 1) ; 
	rep( i, 1, t ) qus[i].x = gi(), qus[i].y = gi(), d[i] = i ;
	rep( i, 1, t ) qus[i].ans = abs(qus[i].x - qus[i].y) ; 
	Id.resize(n + 1) ; 
	rep( i, 1, n ) Id[i] = i ; 
	solve(d, Id, E) ;
	rep( i, 1, t ) printf("%d\n", qus[i].ans ) ; 
	return 0 ;
}