首页 > 其他分享 >做题记录整理图论1 P3629 [APIO2010] 巡逻(2022/10/3)

做题记录整理图论1 P3629 [APIO2010] 巡逻(2022/10/3)

时间:2022-10-04 20:00:21浏览次数:80  
标签:10 return P3629 int APIO2010 st fa now hd

P3629 [APIO2010] 巡逻

写一道题顶写三道题系列,
为了写这道题专门去学习了树的直径的两种求法,可以说是血赚了
https://www.luogu.com.cn/blog/lscsznmhw/solution-p3629
由于没学过类似题目,还是看了题解。。。

#include<bits/stdc++.h>
#define for1(i,a,b) for(ll i = a;i<=b;i++)
#define ll long long
#define mp(a,b) make_pair(a,b)
using namespace std;

struct node{
	int nex;
	int to;
	int w;
} a[500010];
int hd[100010],cnt;
int n,k;
int mx,ji,d1,d2,st,ed,fr[100010],dis[100010];
bool kg;
map<int,bool>ma;

void ru(int x,int y) 
{
	a[++cnt].to=y;
	a[cnt].nex=hd[x];
	a[cnt].w=1;
	hd[x]=cnt;
}

void dfs(int x,int fa,int we) 
{
	if(we>=mx)
	{
		mx=we;
		ji=x;
	}
	for(int i=hd[x];i;i=a[i].nex)
		if(a[i].to!=fa)
			dfs(a[i].to,x,we+a[i].w);
}

void dfs2(int now,int fa,int t) 
{
	if(kg)  return;
	for(int i=hd[now]; i; i=a[i].nex) 
	{
		if(kg)  return;
		if(a[i].to==fa)  continue;
		if(a[i].to==t) 
		{
			fr[now]=a[i].to;
			kg=1;
			return;
		}
		fr[now]=a[i].to;
		dfs2(a[i].to,now,t);
		if(kg)  return;
	}
}

void dfs3(int now,int fa)
 {
	for(int i=hd[now]; i; i=a[i].nex) 
	{
		if(a[i].to==fa)  continue;
		dfs3(a[i].to,now);
		d2=max(d2,dis[now]+dis[a[i].to]+a[i].w);
		dis[now]=max(dis[now],dis[a[i].to]+a[i].w);
	}
}

int main() 
{
    int x,y;
	cin>>n>>k;
	for1(i,1,n-1)
	{
		cin>>x>>y;
		ru(x,y);
		ru(y,x);
	}
	dfs(1,0,0);
	st=ji,mx=0;
	dfs(st,0,0);
	d1=mx,ed=ji;
	if(k==1) 
	{
		printf("%d",(2*(n-1)-d1+1));
		return 0;
	}
	dfs2(st,0,ed);
	ma[ed]=1,ma[st]=1;
	for(int i=st;i!=ed;i=fr[i]) ma[i]=1;
	for1(i,1,n)
		if(ma.count(i)==1)
			for(int j=hd[i];j;j=a[j].nex)
				if(ma.count(a[j].to)==1)
					a[j].w=-1;
	dfs3(1,0);
	printf("%d",2*n-d1-d2);
	return 0;
}

标签:10,return,P3629,int,APIO2010,st,fa,now,hd
From: https://www.cnblogs.com/yyx525jia/p/16754328.html

相关文章

  • 2022.10.4
    考试,大概7、8名,基本是按流程来的了。还是有些问题,感觉很多题莫名奇妙没转过弯,拿了很高的部分分但距离正解还有距离。CF做少了QaQTodo:考试,改题(至少前三道)。把CF的E题和......
  • 10.4 第三问
    (1)统计每天各个机场的销售数量和销售金额。要求的输出字段day_id,sale_nbr,,cnt,round日期编号,卖出方代码,数量,金额。命令:查询语句:selectday_id,sale_nbr,sum(cnt),sum......
  • 10.4训练
    输入schematool-initSchema-dbTypemysql-verbose初始化hive元数据库hive建表createtabletest0(day_idstring,sale_nbrstring,buy_nbrstring,cntint,roun......
  • 10.4
    bin/sqoopexport>--connectjdbc:mysql://master:3306/mysql>--usernameroot>--password000000>--tabletable3>--num-mappers1>--export-dir/user/hive/......
  • 43rd 2022/10/4 模拟赛总结30
    这次还行?rank5,其实也不是多高不可攀,就是认真打,暑假时就上过前五好多次其实比赛历程也很简单第一题很忽悠,思路乱的一批,但是这次冷静下来把思路理清就切了很简单的概率D......
  • English words1004
    ......
  • 初学C语言笔记221004动态内存管理
    constint*consta=&b;//3intconst*consta=&b;//4第三个a是静态的指针(第二个const修饰),指向int,这个int是静态的(第一个const修饰)第四个a是静态的......
  • 2022.10.04考试总结
    2022.10.04考试总结得分:\(110/300\)总结:今天的第一题比较简单,第二题因为看错题意并且思考了比较长的时间导致爆零,第三题在考场上没有一个比较完整并且容易实现的思路题......
  • Python 教程之控制流(10)在Python中有效地使用迭代
    下面是使用迭代器的不同方法。C风格的方法:这种方法需要事先知道迭代的总次数。#访问列表元素的C风格方式cars=["Aston","Audi","McLaren"]i=0while(i<len(cars)......
  • 恶意代码分析实战 windbg内核恶意代码分析 lab 10-1 10-2 10-3
    Lab10-01本实验包括一个驱动程序和一个可执行文件。你可以从任意位置运行可执行文件,但为了使程序能够正常运行,必须将驱动程序放到C:\Windows\System32目录下,这个目录在......