bzoj1912 巡逻

题目描述

在一个地区中有 n 个村庄,编号为 1, 2, ..., n。有 n – 1 条道路连接着这些村 庄,每条道路刚好连接两个村庄,从任何一个村庄,都可以通过这些道路到达其 他任一个村庄。每条道路的长度均为 1 个单位。 为保证该地区的安全,巡警车每天要到所有的道路上巡逻。警察局设在编号 为 1 的村庄里,每天巡警车总是从警察局出发,最终又回到警察局。 下图表示一个有 8 个村庄的地区,其中村庄用圆表示(其中村庄 1 用黑色的 圆表示),道路是连接这些圆的线段。为了遍历所有的道路,巡警车需要走的距 离为 14 个单位,每条道路都需要经过两次。

为了减少总的巡逻距离,该地区准备在这些村庄之间建立 K 条新的道路, 每条新道路可以连接任意两个村庄。两条新道路可以在同一个村庄会合或结束 (见下面的图例(c))。 一条新道路甚至可以是一个环,即,其两端连接到同一 个村庄。 由于资金有限,K 只能是 1 或 2。同时,为了不浪费资金,每天巡警车必须 经过新建的道路正好一次。 下图给出了一些建立新道路的例子:

在(a)中,新建了一条道路,总的距离是 11。在(b)中,新建了两条道路,总 的巡逻距离是 10。在(c)中,新建了两条道路,但由于巡警车要经过每条新道路 正好一次,总的距离变为了 15。 试编写一个程序,读取村庄间道路的信息和需要新建的道路数,计算出最佳 的新建道路的方案使得总的巡逻距离最小,并输出这个最小的巡逻距离。

输入输出格式

输入格式:

第一行包含两个整数 n, K(1 ≤ K ≤ 2)。接下来 n – 1 行,每行两个整数 a, b, 表示村庄 a 与 b 之间有一条道路(1 ≤ a, b ≤ n)。

输出格式:

输出一个整数,表示新建了 K 条道路后能达到的最小巡逻距离。

 

输入输出样例

输入样例#1:
8 1 
1 2 
3 1 
3 4 
5 3 
7 5 
8 5 
5 6 
输出样例#1:
11
输入样例#2:
8 2 
1 2 
3 1 
3 4 
5 3 
7 5 
8 5 
5 6 
输出样例#2:
10
输入样例#3:
5 2 
1 2 
2 3 
3 4 
4 5 
输出样例#3:
6

说明

10%的数据中,n ≤ 1000, K = 1;

30%的数据中,K = 1;

80%的数据中,每个村庄相邻的村庄数不超过 25;

90%的数据中,每个村庄相邻的村庄数不超过 150; 100%的数据中,3 ≤ n ≤ 100,000, 1 ≤ K ≤ 2。

 

一道简单的求树上最长链的题,我竟然debug了2小时!!!

k=1时,最优解为在树的直径x到y之间连一条边,这样答案为2 * n - 1 - len1

k=2时,将树的直径上的所有边权全部改成-1,再求一次直径len2,答案为2 * n - len1 - len2。

注意,dfs两次求直径的方法在k=2的时候并不能用!!!(除非你愿意和我一样debug2小时)

关于dfs两次求直径的方法:

先从1开始dfs,找到深度最大的点x,再从x开始dfs,找到距离x最大的点y,x到y就是树的直径。

但是在一些边权改变为-1的情况下,这样并不能正确的算出答案。这样做wa掉的代码(可以卡掉它的数据在代码底下):

//Serene
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cstdio>
#include<cmath>
using namespace std;
const int maxn=1e5+10;
int n,k;

int aa;char cc;
int read() {
	aa=0;cc=getchar();
	while(cc<'0'||cc>'9') cc=getchar();
	while(cc>='0'&&cc<='9') aa=aa*10+cc-'0',cc=getchar();
	return aa;
}

int fir[maxn],nxt[2*maxn],to[2*maxn],e=1,v[2*maxn];
void add(int x,int y) {
	to[++e]=y;nxt[e]=fir[x];fir[x]=e;v[e]=1;
	to[++e]=x;nxt[e]=fir[y];fir[y]=e;v[e]=1;
}

int maxlen[maxn],ans[maxn];
void dfs(int pos,int x,int d) {
	maxlen[pos]=d;ans[pos]=pos;
	for(int y=fir[pos];y;y=nxt[y]) {
		if(to[y]==x) continue;
		dfs(to[y],pos,d+v[y]);
		if(maxlen[to[y]]>maxlen[pos]) {
			maxlen[pos]=maxlen[to[y]];
			ans[pos]=ans[to[y]];
		}
	}
}

void dfs2(int pos,int x) {
	for(int y=fir[pos];y;y=nxt[y]) {
		if(to[y]==x||ans[to[y]]!=ans[pos]) continue;
		v[y]=v[y^1]=-1;
		dfs2(to[y],pos); break;
	}
}

int main() {
	n=read();k=read(); int x,y;
	for(int i=1;i<n;++i) {
		x=read();y=read();
		add(x,y);
	}
	dfs(1,0,0);dfs(x=ans[1],0,0);
	y=maxlen[x];
	if(k==1) printf("%d",2*n-1-y);
	else {
		dfs2(x,0); 
		dfs(1,0,0); 
		dfs(x=ans[1],0,0);
		printf("%d",2*n-y-maxlen[x]);
	}
	return 0;
}
/*
10 2
1 2
3 1
4 2
5 4
6 5
7 3
8 1
9 4
10 4
*/

  

然后就只能用一般的方法来做树的直径了(注意,边权变为-1的时候,反向边也要变为-1)

AC代码:

 

//Serene
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cstdio>
#include<cmath>
using namespace std;
const int maxn=1e5+10;
int n,k;

int aa;char cc;
int read() {
	aa=0;cc=getchar();
	while(cc<'0'||cc>'9') cc=getchar();
	while(cc>='0'&&cc<='9') aa=aa*10+cc-'0',cc=getchar();
	return aa;
}

int fir[maxn],nxt[2*maxn],to[2*maxn],e=1,v[2*maxn];
void add(int x,int y) {
	to[++e]=y;nxt[e]=fir[x];fir[x]=e;v[e]=1;
	to[++e]=x;nxt[e]=fir[y];fir[y]=e;v[e]=1;
}

int lca,ans,maxlen[maxn],fr[2][maxn];
void dfs(int pos,int x) {
	int a=0,b=0;
	for(int y=fir[pos];y;y=nxt[y]) {
		if(to[y]==x) continue;
		dfs(to[y],pos);
		if(maxlen[to[y]]+v[y]>maxlen[b]+v[fr[1][pos]]) {
			b=to[y];fr[1][pos]=y;
			if(maxlen[b]+v[y]>maxlen[a]+v[fr[0][pos]]) swap(a,b),swap(fr[1][pos],fr[0][pos]);
		}
	}
	maxlen[pos]=maxlen[a]+v[fr[0][pos]];
	if(maxlen[a]+maxlen[b]+v[fr[0][pos]]+v[fr[1][pos]]>ans) {
		ans=maxlen[a]+maxlen[b]+v[fr[0][pos]]+v[fr[1][pos]];
		lca=pos;
	}
}

int main() {
	n=read();k=read(); int x,y;
	for(int i=1;i<n;++i) {
		x=read();y=read();
		add(x,y);
	}
	dfs(1,0);
	if(k==1) printf("%d",2*n-1-ans);
	else {
		for(int i=fr[0][lca];i;i=fr[0][to[i]]) 
		v[i]=v[i^1]=-1;
		for(int i=fr[1][lca];i;i=fr[0][to[i]]) 
		v[i]=v[i^1]=-1;
		memset(fr,0,sizeof(fr));
		x=ans; ans=0; dfs(1,0);
		printf("%d",2*n-ans-x);
	}
	return 0;
}

 

  

 

posted @ 2017-09-23 16:31  shixinyi  阅读(339)  评论(0编辑  收藏  举报