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

#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. struts2与struts1的比较

    struts2相对于struts1来说简单了很多,并且功能强大了很多,我们可以从几个方面来看: 从体系结构来看:struts2大量使用拦截器来出来请求,从而允许与业务逻辑控制器 与 servlet-a ...

  2. Codeforces Round #547 (Div. 3) D. Colored Boots

    链接:https://codeforces.com/contest/1141/problem/D 题意: 给连个n长度的字符串. 求两个字符串相同字符对应位置的对数,并挨个打印. 字符:?可以代替任何 ...

  3. Codeforces Round #396 (Div. 2) E

    Mahmoud and Ehab live in a country with n cities numbered from 1 to n and connected by n - 1 undirec ...

  4. 转 错误:ORA-28002/ORA-65162 : the password will expire within 7 days 解决方法

    今天在使用sqlplus时出现 =============================================== ERROR:ORA-28002: the password will e ...

  5. js判断网页访问设备类型

    有时候我们会需要来根据不同的设备访问进行不同的操作,在网上找了一下,主要是根据Navigator对象, if(/Android|Windows Phone|webOS|iPhone|iPod|Blac ...

  6. printf 遇到bash重定向

    在printf之前添加:setvbuf(stdout,NULL,_IONBF,0);设置缓冲区为空. 在每句printf之后添加:fflush(stdout); 方法一: 1 2 3 4 5 6 7 ...

  7. [jQuery] Cannot read property ‘msie’ of undefined错误的解决方法 --转

    初用Yii的srbac模块.出现 Cannot read property ‘msie’ of undefined 错误.上网查询,找到如下的文章.使用文末的打补丁的方法,成功搞定.感谢. ===== ...

  8. APP弱网测试点

  9. bat 符号说明

    netstat -an|findstr 139 ipconfig/all findstr IP ipconfig/all |findstr   物理地址             定值选行 ipconf ...

  10. COGS 133. [USACO Mar08] 牛跑步

    ★★★   输入文件:cowjog.in   输出文件:cowjog.out   简单对比时间限制:1 s   内存限制:128 MB Bessie准备用从牛棚跑到池塘的方法来锻炼. 但是因为她懒,她 ...