HDU 6228 tree 简单思维树dp

一、前言

前两天沈阳重现,经过队友提点,得到3题的成绩,但是看到这题下意识觉得题目错了,最后发现实际上是题目读错了。。。。GG

感觉自己前所未有的愚蠢了。。。。不过题目读对了也是一道思维题,但是很好理解。

二、题意

对于一个无相无环图,要求找出若干边,满足“这些边被至少K个不同的点集在互相联通的时候访问到”。或者说“这些边都包含在K个不同的点集个字组成的联通快里面”。

三、题解

考虑如何表示一个边,以及这条边两边的点的数量?(这是一棵树)
作为一颗树,就有树边概念,因而可以认为“该树包括他自己在内在树的一边”,“其他节点在树边的另一边”

因而,统计下,有多少符合要求的树边就可以了。具体实现见代码:

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3
 4 const long long MAXN=200000+2333;
 5 int child[MAXN];
 6 vector<int>G[MAXN];
 7 long long n,k;
 8 long long ans=0;
 9
10 void dfs(int now,int last)
11 {
12     int len=G[now].size();
13     for(int i=0;i<len;++i)
14     {
15         int tar=G[now][i];
16         if(tar==last)continue;
17         dfs(tar,now);
18         child[now]+=child[tar];
19     }    child[now]++;
20     if(child[now]>=k&&n-child[now]>=k)ans++;
21
22 }
23
24 void init()
25 {
26 //    memset(child,0,sizeof(child));
27     ans=0;
28     cin>>n>>k;
29     for(int i=0;i<=n;++i)G[i].clear(),child[i]=0;
30     for(int i=1;i<n;++i)
31     {
32         int a,b;
33         cin>>a>>b;
34         G[a].push_back(b);
35         G[b].push_back(a);
36
37     }
38     dfs(1,0);
39     cout<<ans<<endl;
40
41 }
42
43 int main()
44 {
45     cin.sync_with_stdio(false);
46     int ca;cin>>ca;
47     while(ca--)init();
48
49
50     return 0;
51 } 
时间: 2024-08-29 07:34:36

HDU 6228 tree 简单思维树dp的相关文章

HDU 6228 Tree(思维 DFS)

Consider a un-rooted tree T which is not the biological significance of tree or plant, but a tree as an undirected graph in graph theory with n nodes, labelled from 1 to n. If you cannot understand the concept of a tree here, please omit this problem

hdu 3016 Man Down (线段树 + dp)

题目大意: 是男人就下一般层...没什么可以多说的吧. 注意只能垂直下落. 思路分析: 后面求最大值的过程很容易想到是一个dp的过程 . 因为每一个plane 都只能从左边 从右边下两种状态. 然后我们所需要处理的问题就是 ,你如何能快速知道往左边下到哪里,往右边下到哪里. 这就是线段树的预处理. 讲线段按照高度排序. 然后按照高度从小到大加入到树中. 然后去寻找左端点 和 右端点最近覆盖的线段的编号. #include <cstdio> #include <iostream> #

hdu 5370 Tree Maker(catalan+dp)

题目链接:hdu 5370 Tree Maker n个节点的二叉树种类为Catalan数的第n项 对于一棵子树而言,被移动过的节点就是确定的位置,所以只要知道已经确定位置的K个节点有多少个空孩子指针M,和就该子树下的N个未确定位置的节点,等于是说用N个节点构造M个可为空的子树的种类数.对于整个树的形态数即为若干棵独立的子树形态数的乘积. 定义dp[i][j]为用i个节点构造j棵树的形态数,dp[i][j] = sum{ dp[i-1][j-k] * catalan[k] | 0 ≤ k ≤j }

hdu 6228 Tree

hdu 6228 题意:一棵 n 个点的树,要你把这些树上的节点用 k 种颜色染色,问你在最优的染色方案下,相同颜色点连接的最小边集的交集最大是多少 Tags: dfs,  貌似读懂题就好做了.. #include<bits/stdc++.h> using namespace std; #pragma comment(linker, "/STACK:102400000,102400000") #define rep(i,a,b) for (int i=a; i<=b;

HDU 5293 Tree chain problem 树形dp+dfs序+树状数组+LCA

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5293 题意: 给你一些链,每条链都有自己的价值,求不相交不重合的链能够组成的最大价值. 题解: 树形dp, 对于每条链u,v,w,我们只在lca(u,v)的顶点上处理它 让dp[i]表示以i为根的指数的最大值,sum[i]表示dp[vi]的和(vi为i的儿子们) 则i点有两种决策,一种是不选以i为lca的链,则dp[i]=sum[i]. 另一种是选一条以i为lca的链,那么有转移方程:dp[i]=

HDU 5125 magic balls(线段树+DP)

magic balls Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 323    Accepted Submission(s): 90 Problem Description The town of W has N people. Each person takes two magic balls A and B every day.

HDU 5002 Tree(动态树LCT)(2014 ACM/ICPC Asia Regional Anshan Online)

Problem Description You are given a tree with N nodes which are numbered by integers 1..N. Each node is associated with an integer as the weight. Your task is to deal with M operations of 4 types: 1.Delete an edge (x, y) from the tree, and then add a

HDU - 6228 Tree (dfs)

Consider a un-rooted tree T which is not the biological significance of tree or plant, but a tree as an undirected graph in graph theory with n nodes, labelled from 1 to n. If you cannot understand the concept of a tree here, please omit this problem

HDU-6035:Colorful Tree(虚树+DP)

这里有三道长得像的题: 一:HDU6036: There is a tree with nn nodes, each of which has a type of color represented by an integer, where the color of node ii is cici. The path between each two different nodes is unique, of which we define the value as the number of