![](https://img-blog.csdnimg.cn/20201014180756780.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
树的直径
Love_xyh
这个作者很懒,什么都没留下…
展开
-
CF1092E Minimal Diameter Forest
和CF455C Civilization貌似挺像。 我们求出每棵树的直径和中点,找到直径最大的树后,将别的树的中点与直径最大的树的中点相连,即可完成构造。 #include <bits/stdc++.h> using namespace std; const int N=1e5+5; int n,m,u,v,ans,p,mid,pos; int f[N]; bool vis[N],jay; int cnt,head[N]; struct edge{int next,to;}e[N<<原创 2020-09-28 23:50:34 · 290 阅读 · 0 评论 -
CF1000E We Need More Bosses
对无向图进行缩点后求树直径即为答案。 #include <bits/stdc++.h> using namespace std; const int N=3e5+5; int n,m,u[N],v[N],ans,p; int now,top,col,dfn[N],low[N],sta[N],color[N]; int cnt=1,head[N]; struct edge{int next,to; bool vis;}e[N<<1]; inline void add(int u,int原创 2020-09-21 20:14:13 · 156 阅读 · 0 评论 -
[SDOI2013]直径
性质一:树的直径必须经过一个点,且这个点为这个直径的中点(中点可以为一条路径的中点) 性质二:修改一条已知直径的权值,若其它直径与该直径有相交部分,则其它直径权值也会更改(好像比性质一更显然些) 所以我们就用性质二来做。 前后求两次直径,差即为答案。 #include <bits/stdc++.h> #define int long long using namespace std; const int N=2e5+5; int n,u,v,w,p,ans,d1,d2,last; int d[N原创 2020-07-23 16:05:32 · 101 阅读 · 0 评论 -
CF592D Super M
我们可以发现只有一条路径能只走一次,(就是从出生地到最后遍历的那个点的路径)而其他的路径都需要走两次。 所以我们让这条 “只走一次的路径” 为直径即可使得行走总长度最小。 那么出生地即为树的直径的标号最小的端点。 #include <bits/stdc++.h> using namespace std; const int N=2e5+5; int n,k,u,v,p,ans,ans1...原创 2020-03-27 23:48:12 · 159 阅读 · 0 评论 -
树的直径
求树的直径: #include <bits/stdc++.h> using namespace std; const int N=3e5+5; int n,m,u,v,ans,p,cnt; int d[N],head[N]; struct edge{int next,to;}e[N<<1]; inline void add(int u,int v) { cnt++; ...原创 2020-03-14 13:25:07 · 153 阅读 · 1 评论