Link:

POJ1935 传送门

Solution:

一道吓唬人的水题

注意这是一棵树,两点间仅有唯一的路径

于是每个“关键点”和起点只有一条路径,想去起点另一棵子树上的节点必须要回到起点

如果必须要回到起点,答案$res$就是除去无用边后整棵树总距离$*2$,

因为不必回到起点,最终结果为$res-mx$,$mx$为距起点的最远点的距离

除去无用边的方式:给每个“关键点”打上$vis$标记,这样最远点之后的点就不会被$dfs$到了

Code:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <utility>
#include <vector> using namespace std;
typedef pair<int,int> P;
#define X first
#define Y second
const int MAXN=5e4+;
vector<P> G[MAXN];
int n,k,root,dist[MAXN],vis[MAXN],mx,res,x,y,z; void dfs(int u,int anc)
{
for(int i=;i<G[u].size();i++)
{
int v=G[u][i].X;
if(v==anc) continue;
dist[v]=dist[u]+G[u][i].Y;
dfs(v,u);
if(vis[v]) vis[u]=true,res+=G[u][i].Y*; //传递标记
}
} int main()
{
scanf("%d%d",&n,&root);
for(int i=;i<n;i++)
{
scanf("%d%d%d",&x,&y,&z);
G[x].push_back(P(y,z));G[y].push_back(P(x,z));
}
scanf("%d",&k);
for(int i=;i<=k;i++)
scanf("%d",&x),vis[x]=true; //打上标记
dfs(root,);
for(int i=;i<=n;i++)
if(vis[i]) mx=max(mx,dist[i]);
printf("%d",res-mx);
return ;
}

Review:

(1)对“树”这个条件中“两点之间只有一条路径”这个性质不够敏感,

因此未能看出到达一棵子树的最远点后必须要回到起点再出发这一推论

(2)除去无用边的方法很妙:设置$vis=true$,将更远的点截去

同时传递$vis$的值来更新答案

[POJ 1935] Journey的更多相关文章

  1. 【POJ】1935 Journey(树形dp)

    题目 传送门:QWQ 分析 凉凉. 答案是所有要经过的点到根所经过的边权和减去最大的边权. 代码 vector好慢啊 #include <cstdio> #include <vect ...

  2. poj 1935(树形dp)

    题目链接:http://poj.org/problem?id=1935 思路:首先我们考虑从源点出发到所有自己想要经过的点然后在回到源点sum,显然每条边都必须经过源点(这个我们可以一次dfs求出), ...

  3. poj 3544 Journey with Pigs

    Journey with Pigs Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 3004   Accepted: 922 ...

  4. 树型DP(2)

    声明 https://blog.csdn.net/no1_terminator/article/details/77824790 参考课件和讲授来自Accelerator 找树的直径 树的直径定义为一 ...

  5. 广大暑假训练1(poj 2488) A Knight's Journey 解题报告

    题目链接:http://vjudge.net/contest/view.action?cid=51369#problem/A   (A - Children of the Candy Corn) ht ...

  6. poj 2488 A Knight&#39;s Journey(dfs+字典序路径输出)

    转载请注明出处:http://blog.csdn.net/u012860063?viewmode=contents 题目链接:http://poj.org/problem? id=2488 ----- ...

  7. POJ 1932 XYZZY (ZOJ 1935)SPFA+floyd

    http://poj.org/problem?id=1932 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1935 题目大 ...

  8. POJ 2488 -- A Knight's Journey(骑士游历)

    POJ 2488 -- A Knight's Journey(骑士游历) 题意: 给出一个国际棋盘的大小,判断马能否不重复的走过所有格,并记录下其中按字典序排列的第一种路径. 经典的“骑士游历”问题 ...

  9. A Knight's Journey 分类: POJ 搜索 2015-08-08 07:32 2人阅读 评论(0) 收藏

    A Knight's Journey Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 35564 Accepted: 12119 ...

随机推荐

  1. freemaker示例

    第一步  创建一个User.java文件 来两个变量 public class User {      private String userName;       private String us ...

  2. ASP.NET Core ---异常处理

    一.局部异常处理: 在Action里面catch 二.全局异常处理: 1.默认的异常处理配置: 默认配置在StartUp文件的Configure中注册错误处理,显示开发者错误页面: public vo ...

  3. 孤荷凌寒自学python第五十八天成功使用python来连接上远端MongoDb数据库

    孤荷凌寒自学python第五十八天成功使用python来连接上远端MongoDb数据库 (完整学习过程屏幕记录视频地址在文末) 今天是学习mongoDB数据库的第四天.今天的感觉是,mongoDB数据 ...

  4. Linux常用命令及工具记录(持续更新)

    一.命令 convmv   作用:文件名的编码转换   安装:sudo apt-get install convmv   使用:convmv * -f gbk -t utf8 --notest   c ...

  5. MVC学习笔记----@Helper标签(HelperMethod方法)和HtmlExtesion扩展

    1,HtmlHelper扩展 http://www.cnblogs.com/willick/p/3428413.html http://www.cnblogs.com/zengdingding/p/5 ...

  6. MapReduce架构

    主从结构 主节点:JobTracker(一个) 从节点:TaskTrackers(多个) JobTracker: 接收客户提交的计算任务 把计算任务分配给TaskTrackers执行 监控TaskTr ...

  7. Centos 6.5 HISTSIZE更改

    通过 更改 /etc/profile 中的HISTSIZE值,改完之后,执行source /etc/profile  和echo $HISTSIZE,结果还是之前的HISTSIZE值, 解决办法:执行 ...

  8. 【HDU 2594 Simpsons' Hidden Talents】

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission( ...

  9. (转)Ant使用例子

    文章来自:http://www.blogjava.net/feng0801/archive/2014/07/29/416297.html Ant是一个Apache基金会下的跨平台的构件工具,它可以实现 ...

  10. RHN Classic and Red Hat Subscription Management

    What's the difference between RHN Classic and Red Hat Subscription Management? Introduction With the ...