重要意义:复习好久没写的邻接表了。

Network, Seoul 2007, LA3902

Consider a tree network with n nodes where the internal nodes correspond to servers and the terminal nodes correspond to clients. The nodes are numbered from 1 to n .
Among the servers, there is an original server Swhich provides VOD (Video On Demand) service. To ensure the quality of service for the clients, the distance from each client to the VOD server S should
not exceed a certain value k . The distance from a node u to a node v in the tree is defined to be the number of edges on the path from u to v .
If there is a nonempty subset C of clients such that the distance from each u in C to S is greater than k ,
then replicas of the VOD system have to be placed in some servers so that the distance from each client to the nearest VOD server (the original VOD system or its replica) is k or less.

Given a tree network, a server S which has VOD system, and a positive integer k , find the minimum number of replicas necessary so that each
client is within distance k from the nearest server which has the original VOD system or its replica.

For example, consider the following tree network.

In the above tree, the set of clients is {1, 6, 7, 8, 9, 10, 11, 13}, the set of servers is {2, 3, 4, 5, 12, 14}, and the original VOD server is located at node 12.

For k = 2 , the quality of service is not guaranteed with one VOD server at node 12 because the clients in {6, 7, 8, 9, 10} are away from VOD server at distance > k .
Therefore, we need one or more replicas. When one replica is placed at node 4, the distance from each client to the nearest server of {12, 4} is less than or equal to 2. The minimum number of the needed replicas is one for this example.

Input

Your program is to read the input from standard input. The input consists of T test cases. The number of test cases (T ) is given in the first
line of the input. The first line of each test case contains an integer n (3n1,
000) which is the number of nodes of the tree network. The next line contains two integers s (1sn)and k (k1) where s is
the VOD server and k is the distance value for ensuring the quality of service. In the following n - 1 lines, each line contains a pair of nodes which represent an edge of the tree network.

Output

Your program is to write to standard output. Print exactly one line for each test case. The line should contain an integer that is the minimum number of the needed replicas.

Sample Input

2 14
12 2
1 2
2 3
3 4
4 5
5 6
7 5
8 5
4 9
10 3
2 12
12 14
13 14
14 11
14
3 4
1 2
2 3
3 4
4 5
5 6
7 5
8 5
4 9
10 3
2 12
12 14
13 14
14 11

Sample Output

1
0

1.以S为根建树

2.从最底层的叶子开始思考(受其他因素影响小),发现必须要在 最底层的 k*father上安装。

3.用一个叶子表来保存叶子节点,方便选择,否则会超时;

白书题解:

接下来,我们考虑深度最大的结点。比如结点8,应该在哪里放新的服务器来覆盖(“覆盖”一个叶子是指到该叶子的距离不超过k)它呢?只有结点5和结点4满足条件。显然,结点4比结点5划算,因为结点5所覆盖的叶子(6, 7, 8)都能被结点4所覆盖。一般的,对于深度最大的结点u,选择uk级祖先是最划算的(父亲是1级祖先,父亲的父亲是2级祖先,以此类推)。证明过程留给读者自行思考。

下面给出上述算法的一种实现方法:每放一个新服务器,进行一次DFS,覆盖与它距离不超过k的所有结点。注意,本题只需要覆盖叶子,而不需要覆盖中间结点,而且深度不超过k的叶子已经被原始服务器覆盖,所以我们只需要处理深度大于k的叶结点即可。为了让程序更简单,我们可用nodes表避开“按深度排序”的操作

#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <ctime>
#include <algorithm>
#define uns unsigned
#define int64 long long
#ifdef WIN32
#define fmt64 "%I64d"
#else
#define fmt64 "%lld"
#endif
#define oo 0x13131313
#define FOR(i,l,r) for(int i=(l);i<=(r);i++)
#define ROF(i,r,l) for(int i=(r);i>=(l);i--)
#define maxn 1010
using namespace std;
struct edge { int to; edge *next; };
struct node { int ok;int deep;int father; edge *first;};
edge E[maxn*3],*EE=E+1;
node TREE[maxn];
int s,k,n,ynode;
struct YEZI { int num;int deep;};
YEZI yezi[maxn];
int numyezi=0;
void LINK(int u,int v)
{
*EE=(edge) {v,TREE[u].first},TREE[u].first=EE++;
*EE=(edge) {u,TREE[v].first},TREE[v].first=EE++;
}
int inputtree()
{
memset(TREE,0,sizeof(TREE));
memset(E,0,sizeof(E));
memset(yezi,0,sizeof(yezi));
EE=E+1;ynode=0;numyezi=0;
int u,v;
scanf("%d",&n);
scanf("%d%d",&s,&k);
TREE[s].ok=1;
FOR(i,1,n-1)
{
scanf("%d%d",&u,&v);
LINK(u,v);
}
}
void deepdfs(int now,int father,int deep)
{
edge *mm=TREE[now].first;
TREE[now].deep=deep;
TREE[now].father=father;
if(mm->next==NULL) { ynode++;yezi[++numyezi].num=now;yezi[numyezi].deep=deep;}
for(edge *p=TREE[now].first;p;p=p->next)
{
if(p->to!=father)
deepdfs(p->to,now,deep+1);
}
}
int markdfs(int now,int father,int deep)
{
edge *mm=TREE[now].first;
if(mm->next==NULL&&TREE[now].ok==0)
{
ynode--;
TREE[now].ok=1;
}
if(deep==k) return 0;
for(edge *p=TREE[now].first;p;p=p->next)
{
if(p->to!=father)
markdfs(p->to,now,deep+1);
}
return 0;
}
int cmp(const void *i,const void *j)
{
YEZI *ii=(YEZI *)i,*jj=(YEZI *)j;
return jj->deep-ii->deep;
}
int main()
{
//freopen("a.in","r",stdin);
//freopen("a.out","w",stdout);
int T,max=-1,maxnode=0,ans=0,t;
scanf("%d",&T);
while(T--)
{
ans=0;
inputtree();
deepdfs(s,-1,1);
markdfs(s,-1,0);
qsort(yezi+1,numyezi,sizeof(yezi[1]),cmp);
FOR(i,1,numyezi)
if(TREE[yezi[i].num].ok==0)
{
t=yezi[i].num;
FOR(i,1,k)
markdfs(t,-1,0);
ans++;
}
printf("%d\n",ans);
}
return 0;
}

【树形贪心】【UVA1267】Network的更多相关文章

  1. BZOJ 2525 Poi2011 Dynamite 二分答案+树形贪心

    题目大意:给定一棵树,有一些点是关键点,要求选择不超过mm个点.使得全部关键点到近期的选择的点距离最大值最小 二分答案,问题转化为: 给定一棵树,有一些点是关键点,要求选择最少的点使得每一个关键点到选 ...

  2. uva1267 Network

    https://vjudge.net/problem/UVA-1267 题意: 有一棵树,上面有一个放着水源的点s,给出一个数k,这个水源可以覆盖路径长度到s不超过k的叶子节点.现在需要把所有的叶子节 ...

  3. 【Luogu】P3574FAR_FarmCraft(树形贪心)

    题解链接 想了一个错的贪心爆零了,气死. 题目链接 #include<cstdio> #include<cctype> #include<cstring> #inc ...

  4. D. Sum in the tree(树形+贪心)

    题目链接;http://codeforces.com/contest/1099/problem/D 题目大意:给出一棵树,每个节点到根节点的路径上经过的所有点的权值之和,其深度为偶数的节点的信息全部擦 ...

  5. SGU 149 树形DP Computer Network

    这道题搜了一晚上的题解,外加自己想了半个早上,终于想得很透彻了.于是打算好好写一写这题题解,而且这种做法比网上大多数题解要简单而且代码也比较简洁. 首先要把题读懂,把输入读懂,这实际上是一颗有向树.第 ...

  6. bzoj4027 [HEOI2015]兔子与樱花 树上贪心

    [HEOI2015]兔子与樱花 Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 1320  Solved: 762[Submit][Status][Di ...

  7. 【转载】图论 500题——主要为hdu/poj/zoj

    转自——http://blog.csdn.net/qwe20060514/article/details/8112550 =============================以下是最小生成树+并 ...

  8. POJ动态规划题目列表

    列表一:经典题目题号:容易: 1018, 1050, 1083, 1088, 1125, 1143, 1157, 1163, 1178, 1179, 1189, 1191,1208, 1276, 13 ...

  9. hdu图论题目分类

    =============================以下是最小生成树+并查集====================================== [HDU] 1213 How Many ...

随机推荐

  1. CreateFile使用方法和样例

    函数原型: HANDLE CreateFile( LPCTSTR lpFileName, //指向文件名称的指针 DWORD dwDesiredAccess, //訪问模式(写/读) DWORD dw ...

  2. CBitmap,HBitmap,Bitmap区别及联系

    加载一位图,可以使用LoadImage: HANDLE LoadImage(HINSTANCE hinst,LPCTSTR lpszName,UINT uType,int cxDesired,int ...

  3. Java中的编码格式

    Java中的编码 gbk编码 中文占用2个字节,英文占1个字节; utf-8编码 中文占用3个字节.,英文占用1个字节; Java是双字节编码 (utf-16be) utf -16be 中文占2个字节 ...

  4. 导出word文档

    string id = Request["id"];            if (string.IsNullOrEmpty(id))            {           ...

  5. javascript创建自定义对象和prototype

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  6. Ext tpl 造成 store不能正确加载

    最近维护别人写的代码的时候,遇到了这么个情况 找原因找到了这行代码的身上 tpl: '<tpl for="."><div ext:qtip="{name ...

  7. [转载]Linux 性能监控、测试、优化工具

    Linux 平台上的性能工具有很多,眼花缭乱,长期的摸索和经验发现最好用的还是那些久经考验的.简单的小工具.系统性能专家 Brendan D. Gregg 在最近的 LinuxCon NA 2014 ...

  8. 关于很简单的设计模式,ui-dao-database,以及JavaBean和util工具类

    关于很简单的设计模式,ui-dao-database,以及JavaBean和util工具类 24. 三 / J2EE / 没有评论   简单的j2ee设计模式, UI通过DAO层访问数据库或者xml文 ...

  9. mac下 WebStorm下主题包安装

    mac下: 主题包 1.mac下,点击桌面,使用shift+command+G 输入:~/Library/Preferences 前往(mac查找安装目录的方法,因为默认这些文件夹是隐藏的),进入We ...

  10. 快速下单!简化EcStore的购物结算流程

    EcStore拥有完善的购物车功能,方便顾客浏览挑选商品,但是在提交订单时必须要求用户先登录注册 如果是未注册用户还多出一个注册用户的步骤这些多出来的步骤和操作会影响购物下单的流畅性,降低了用户购物下 ...