CF 1029E Tree with Small Distances

小胖守皇宫!

又双叒叕水了一篇

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<algorithm>
 4 using namespace std;
 5 int n,m,cnt,ans;
 6 int fa[200005];
 7 int dep[200005];
 8 int dp[200005][3];
 9 int head[200005];
10 struct Edge{
11     int fr;
12     int to;
13     int nxt;
14 }edge[400005];
15 void init(){
16     memset(head,-1,sizeof(head));
17 }
18 void addedge(int f,int t){
19     cnt++;
20     edge[cnt].fr=f;
21     edge[cnt].to=t;
22     edge[cnt].nxt=head[f];
23     head[f]=cnt;
24 }
25 void dfs(int u){
26     int tmp=0x3f3f3f3f;
27     for(int i=head[u];i!=-1;i=edge[i].nxt){
28         int v=edge[i].to;
29         if(v==fa[u])continue;
30         fa[v]=u;dep[v]=dep[u]+1;
31         dfs(v);
32         dp[u][1]+=min(dp[v][0],min(dp[v][2],dp[v][1]));
33         dp[u][2]+=min(dp[v][2],dp[v][1]);
34         tmp=min(dp[v][1]-dp[v][2],tmp);
35         dp[u][0]+=min(dp[v][1],dp[v][2]);
36     }
37     if(tmp>0)dp[u][2]+=tmp;
38     if(dep[u]>1)dp[u][1]+=1;
39 }
40 int main(){
41     init();
42     scanf("%d",&n);
43     for(int i=1;i<n;i++){
44         int u,v;
45         scanf("%d%d",&u,&v);
46         addedge(u,v);
47         addedge(v,u);
48     }
49     dfs(1);
50     printf("%d\n",dp[1][1]);
51     return 0;
52 }

原文地址:https://www.cnblogs.com/lnxcj/p/9874018.html

时间: 2024-08-01 07:57:27

CF 1029E Tree with Small Distances的相关文章

codeforces 1029E Tree with Small Distances【思维+贪心】 【非原创】

题目:戳这里 学习博客:戳这里 题意:给一个树加最少的边,使得1到所有点的距离小于等于2. 解题思路:分析样例3可以看出,如果一个点到1的距离大于2,那么建立1到该点的父亲节点的边将比直接与该点建边更优.官方题解的做法是把所有与1距离大于2的点放到大顶堆里维护,每次取出最远的点,染色与该点父节点以及与父节点相接的所有点.也就是相当于与父节点建边,距离为1,与父节点的子节点的距离就为2了,于是把这些点弹出.从大佬博客中学到一个很妙的写法,是用dfs实现这个思路,具体看代码. 附ac代码: 1 #i

CF 375D. Tree and Queries【莫队】

题意: 一棵树,询问一个子树内出现次数≥k≥k的颜色有几种 强制在线见上一道 用莫队不知道比分块高到哪里去了,超好写不用调7倍速度!!! 可以用分块维护出现次数这个权值,实现$O(1)-O(\sqrt{N})$修改查询 #pragma GCC optimize ("O2") #include <iostream> #include <cstdio> #include <algorithm> #include <cstring> #incl

CF.911F.Tree Destruction(构造 贪心)

题目链接 \(Description\) 一棵n个点的树,每次可以选择树上两个叶子节点并删去一个,得到的价值为两点间的距离 删n-1次,问如何能使最后得到的价值最大,并输出方案 \(Solution\) 树上距离,求最大,可以考虑下树的直径 假如已知树的直径u->v,那么任意一点x到达其他点的最远距离就是u,v中一点(如果不是这样,那直径一定可以更长而不是uv) 假设x距u最远,那肯定是删x 删直径上的点(直径端点)会导致一些点取不到最远距离 既然这样按顺序删非直径上的点,最后删直径端点 #in

USACO 2007 December Contest, Silver Problem 2. Building Roads Kruskal最小生成树算法

PROBLEM: (ENGLISH VERSION) Farmer John had just acquired several new farms! He wants to connect the farms with roads so that he can travel from any farm to any other farm via a sequence of roads; roads already connect some of the farms. Each of the N

yyb要填的各种总结的坑

已经写好啦的 莫比乌斯反演 杜教筛 动态点分治 斜率优化 Splay 莫队 凸包 旋转卡壳 Manacher算法 Trie树 AC自动机 高斯消元 KMP算法 可以填的坑 [CF???] [Link-Cut Tree] [树链剖分] 要我填坑就催我把,要不然保证不填坑 我还不会的东西 [SA] [SAM] [带花树] 原文地址:https://www.cnblogs.com/cjyyb/p/8321222.html

Codeforces Round #506 (Div. 3) D-F

Codeforces Round #506 (Div. 3) (中等难度) 自己的做题速度大概只尝试了D题,不过TLE D. Concatenated Multiples 题意 数组a[],长度n,给一个数k,求满足条件的(i,j)(i!=j) a[i],a[j]连起来就可以整除k 连起来的意思是 20,5连起来时205; 5,20连起来时520 n<=2*1e5,k<=1e9,ai<=1e9 愚蠢的思路是像我一样遍历(i,j)可能性,然后TLE,因为这是O(n^2) 可以先思考一简单问

Given a tree, find the node with the minimum sum of distances to other nodes

O(n) complexity, have a traversal for the tree. Get the information of all children, then traverse the tree again. #include <iostream> #include <vector> #include <algorithm> #include <utility> using namespace std; class Node { publ

834. Sum of Distances in Tree —— weekly contest 84

Sum of Distances in Tree An undirected, connected tree with N nodes labelled 0...N-1 and N-1 edges are given. The ith edge connects nodes edges[i][0] and edges[i][1] together. Return a list ans, where ans[i] is the sum of the distances between node i

[Swift]LeetCode834. 树中距离之和 | Sum of Distances in Tree

An undirected, connected tree with N nodes labelled 0...N-1 and N-1 edges are given. The ith edge connects nodes edges[i][0]and edges[i][1] together. Return a list ans, where ans[i] is the sum of the distances between node i and all other nodes. Exam