求图中最短的欧拉路。
题解:因为是一棵树,因此当从某一个节点遍历其子树的时候,如果还没有遍历完整个树,一定还需要再回到这个节点再去遍历其它子树,因此除了从起点到终点之间的路,其它路都被走了两次,而我们要求总的路程最短,那么我们就让从起点到终点的路最长即可,也就是树的直径。所以答案就是所有边权的两倍再减去树的直径

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <vector>
using namespace std;
typedef long long LL;
const int maxn=;
int n;
struct Edge
{
int from,to;
LL val;
};
vector<Edge>edge[maxn];
LL dp[maxn][];
void dfs(int u,int fa)
{
dp[u][]=;dp[u][]=;
int len=edge[u].size();
for(int i=;i<len;i++)
{
int v=edge[u][i].to;
LL w=edge[u][i].val;
if(v==fa)
continue;
dfs(v,u);
if(dp[v][]+w>dp[u][])//如果以u为根节点的最长链可以被它的儿子v更新
{
dp[u][]=dp[u][];//最长链变次长链
dp[u][]=dp[v][]+w;//最长链被更新
}
else if (dp[v][]+w>dp[u][])//如果不能更新最长链但是却可以更新次长链
dp[u][]=dp[v][]+w;
}
}
int main()
{
scanf("%d",&n);
LL ans=;
int f,t;LL v;
Edge temp;
for(int i=;i<n;i++)
{
scanf("%d %d %lld",&f,&t,&temp.val);
temp.from=f;temp.to=t;
edge[f].push_back(temp);
temp.from=t;temp.to=f;
edge[t].push_back(temp);
ans+=(temp.val*);
}
dfs(,);
LL maxx=;
for(int i=;i<=n;i++)
maxx=max(maxx,dp[i][]+dp[i][]);
printf("%lld\n",ans-maxx);
return ;
}

网看到了一个bfs 求树的直径的解法:贴一下

#include <iostream>
#include <cstring>
#include <queue>
using namespace std;
typedef long long ll;
const int MAX = 1e6+;
struct hh{
int u,v,w;
int nt;
}a[MAX];
int dis[MAX],head[MAX];
bool vis[MAX];
int n,tot,point,len;
ll ans;//注意要用long long
void add(int u,int v,int w){
a[tot].v=v;
a[tot].u=u;
a[tot].w=w;
a[tot].nt=head[u];
head[u]=tot++;
}
void bfs(int s){//找树的直径模板
memset(vis,false,sizeof(vis));//第二次要初始化,第一次一块带上了,嘻嘻~~不超时
memset(dis,,sizeof(dis));//第二次要初始化,第一次一块带上了,嘻嘻~~不超时
queue<int> q;
q.push(s);
vis[s]=;
while(!q.empty()){
int x=q.front();
q.pop();
for (int i = head[x]; ~i; i = a[i].nt){
int y=a[i].v;
if(!vis[y]){
dis[y]=dis[x]+a[i].w;
if(len<dis[y]){
len=dis[y];
point=y;
}
vis[y]=true;
q.push(y);
}
}
}
}
int main(){
cin >> n;
memset(head,-,sizeof(head));
for (int i = ; i < n-;i++){
int u,v,w;
cin >> u >> v >> w;
add(u,v,w);//无向边,为双向的
add(v,u,w);
ans+=w<<;//树的权值*2
}
len=;
bfs();//找到最远点
len=;//len为树的直径~~,记住要初始化!
bfs(point);//找直径,必须跑两边,记住!!!
cout << ans-len << endl;
return ;
}

牛客练习赛40 C-小A与欧拉路的更多相关文章

  1. 牛客练习赛40 C 小A与欧拉路(树的直径)

    链接:https://ac.nowcoder.com/acm/contest/369/C 题目描述 小A给你了一棵树,对于这棵树上的每一条边,你都可以将它复制任意(可以为0)次(即在这条边连接的两个点 ...

  2. 牛客练习赛40 A 小D的剧场 (思维dp)

    链接:https://ac.nowcoder.com/acm/contest/369/A 题目描述 若你摘得小的星星 你将得到小的幸福  若你摘得大的星星 你将得到大的财富  若两者都能摘得 你将得到 ...

  3. 牛客练习赛48 C 小w的糖果 (数学,多项式,差分)

    牛客练习赛48 C 小w的糖果 (数学,多项式) 链接:https://ac.nowcoder.com/acm/contest/923/C来源:牛客网 题目描述 小w和他的两位队友teito.toki ...

  4. 牛客练习赛48 A· 小w的a+b问题 (贪心,构造,二进制)

    牛客练习赛48 A· 小w的a+b问题 链接:https://ac.nowcoder.com/acm/contest/923/A来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C ...

  5. 牛客练习赛44 C 小y的质数 (数论,容斥定理)

    链接:https://ac.nowcoder.com/acm/contest/634/C 来源:牛客网 题目描述 给出一个区间[L,R],求出[L,R]中孪生质数有多少对. 由于这是一个区间筛质数的模 ...

  6. 牛客练习赛44 B 小y的线段 (思维)

    链接:https://ac.nowcoder.com/acm/contest/634/B 来源:牛客网 题目描述 给出n条线段,第i条线段的长度为a_ia i ​ ,每次可以从第i条线段的j位置跳到第 ...

  7. 牛客练习赛44 A 小y的序列 (模拟,细节)

    链接:https://ac.nowcoder.com/acm/contest/634/A 来源:牛客网 小y的序列 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32768K,其他语 ...

  8. 牛客练习赛48 D 小w的基站网络

    链接:https://ac.nowcoder.com/acm/contest/923/D来源:牛客网 时间限制:C/C++ 2秒,其他语言4秒 空间限制:C/C++ 262144K,其他语言52428 ...

  9. 5.15 牛客挑战赛40 C 小V和字符串 数位dp 计数问题

    LINK:小V和字符串 容易想到只有1个数相同的 才能有贡献. 知道两个01串 那么容易得到最小步数 大体上就是 第一个串的最前的1和第二个串最前的1进行匹配. 容易想到设f[i][j]表示 前i位1 ...

  10. 5.15 牛客挑战赛40 E 小V和gcd树 树链剖分 主席树 树状数组 根号分治

    LINK:小V和gcd树 时限是8s 所以当时好多nq的暴力都能跑过. 考虑每次询问暴力 跳父亲 这样是nq的 4e8左右 随便过. 不过每次跳到某个点的时候需要得到边权 如果直接暴力gcd的话 nq ...

随机推荐

  1. dp专题复习

    背包: 1.bzoj2287:[POJ Challenge]消失之物 2.bzoj2748:[HAOI2012]音量调节 3.bzoj2794:[Poi2012]Cloakroom 4.bzoj119 ...

  2. Flask (一) 简介

    Flask简介 Flask是一个基于Python实现的Web开发‘微’框架 'MicroFramework' Django是一个重型框架 官方文档: http://flask.pocoo.org/do ...

  3. Restful API官方文档

    理解Restful架构:http://www.ruanyifeng.com/blog/2011/09/restful RESTful设计指南:http://www.ruanyifeng.com/blo ...

  4. Codeforces Round #390 (Div. 2) B

    Ilya is an experienced player in tic-tac-toe on the 4 × 4 field. He always starts and plays with Xs. ...

  5. dubbo-spring

    一.需求 某个电商系统,订单服务需要调用用户服务获取某个用户的所有地址:创建两个服务模块进行测试 测试预期结果:订单服务web模块在A服务器,用户服务模块在B服务器,A可以远程调用B的功能. 二.工程 ...

  6. ZOJ Saddle Point 数学思维题

    http://acm.zju.edu.cn/onlinejudge/showContestProblem.do?problemId=5564   根据它的定义是行最小,列最大. 可以证明鞍点是唯一的. ...

  7. C#私有的构造函数的作用

    C#私有的构造函数的作用:当类的构造函数是私有的时候,也已防止C1 c1=new C1();实例化类.常见的应用是工具类和单例模式. using System;using System.Collect ...

  8. MongoDB内置文档查看和修改

    MongoDB设计的时候,有时候会设计内置文档,方便某个对象的统一.在这里略写了查看内置文档和更新内置文档. 1.查看  表为:realtimelogin   realName为:123 realpa ...

  9. main函数与命令行参数

    main函数的概念 C语言中main函数称之为主函数 一个c程序从main函数开始执行的 下面的main函数定义正确吗? main函数的本质 main函数是操作系统调用的函数 操作系统总是将main函 ...

  10. uvm_reg_sequence——寄存器模型(六)

    寄存器模型 uvm_reg_sequence是UVM自带所有register sequence 的基类. 该类包含model, adapter, reg_seqr(uvm_sequencer). 感觉 ...