Loj#10159.旅游规划
2022/3/26 23:26:25
本文主要是介绍Loj#10159.旅游规划,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
Loj#10159.旅游规划
题目说的很明确了,问点是否在最长路径上,记录最长次长以及最长转移的位置。
怎么判断点在最长路径上,只要最长次长和向上走三者中较大的两个的和为最长路就能说明在最长路径上。
代码显然好写。
/* Knowledge : Rubbish Algorithm Work by :Gym_nastics Time : O(AC) */ #include<cmath> #include<queue> #include<cstdio> #include<cstdlib> #include<cstring> #include<iostream> #include<algorithm> using namespace std; const int INF=0x3f3f3f3f; const int Mod=1e9+7; const int N=1e6+6; int read() { int x=0,f=0;char ch=getchar(); for(;!isdigit(ch);ch=getchar()) f|=(ch=='-'); for(;isdigit(ch);ch=getchar()) x=(x<<1)+(x<<3)+(ch&15); return f?-x:x; } void print(int x) { if(x<0) putchar('-'),x=-x; if(x>9) print(x/10); putchar(x%10+48); } int n,cnt,head[N];struct node{int v,nxt;}e[N]; void Add_edge(int u,int v){e[++cnt]=(node){v,head[u]};head[u]=cnt;} int Max,d1[N],d2[N],c1[N]; void dfs1(int u,int fa){ for(int i=head[u];i;i=e[i].nxt){ int v=e[i].v; if(v==fa) continue; dfs1(v,u); if(d1[v]+1>d1[u])d2[u]=d1[u],d1[u]=d1[v]+1,c1[u]=v; else if(d1[v]+1>d2[u])d2[u]=d1[v]+1; } Max=max(d1[u]+d2[u],Max);return; } int up[N]; void dfs2(int u,int fa){ for(int i=head[u];i;i=e[i].nxt){ int v=e[i].v;if(v==fa) continue; if(c1[u]^v) up[v]=max(up[u],d1[u])+1; else up[v]=max(up[u],d2[u])+1;dfs2(v,u); }return; } signed main() { n=read(); for(int i=1;i<n;i++){ int u=read(),v=read(); Add_edge(u,v);Add_edge(v,u); }dfs1(0,-1);dfs2(0,-1); for(int i=0;i<n;i++){ int com[4]={0,up[i],d1[i],d2[i]}; sort(com+1,com+4); if(com[2]+com[3]==Max) print(i),putchar('\n'); } return 0; }
这篇关于Loj#10159.旅游规划的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-05-15鸿蒙生态设备数量超8亿台
- 2024-05-13TiDB + ES:转转业财系统亿级数据存储优化实践
- 2024-05-09“2024鸿蒙零基础快速实战-仿抖音App开发(ArkTS版)”实战课程已上线
- 2024-05-09聊聊如何通过arthas-tunnel-server来远程管理所有需要arthas监控的应用
- 2024-05-09log4j2这么配就对了
- 2024-05-09nginx修改Content-Type
- 2024-05-09Redis多数据源,看这篇就够了
- 2024-05-09Google Chrome驱动程序 124.0.6367.62(正式版本)去哪下载?
- 2024-05-09有没有大佬知道这种数据应该怎么抓取呀?
- 2024-05-09这种运行结果里的10.100000001,怎么能最快改成10.1?