Codeforces Round #392 (div.2) E:Broken Tree
orz一开始想不画图做这个题(然后脑袋就炸了,思维能力有待提高)
我的做法是动态规划+贪心+构造
首先把题目给的树变成一个可行的情况,同时weight最小
这个可以通过动态规划解决 dp[x]表示以x为结点的子树,它的最小weight是多少
接着我们就只需要考虑每条边增加多少就可以了,这里可以用贪心的做法
ddfs(int x, int fa, int v) 这里v是表示给x结点最大多少增量,然后慢慢加就可以,返回没用掉的增量
其实这个做法有点奇怪,应该有更简便的做法(我觉得可以直接贪心做)
#include <iostream>
#include <cstdio>
#include <vector>
using namespace std;
const int maxn = *;
const long long Give = 2e18;
struct Edge
{
int from, to;
long long w, v;
};
vector<Edge> edges;
vector<int> G[maxn];
long long dp[maxn], delta[maxn], dd[maxn], Fail;
void addedge(int from, int to, int w, int v)
{
edges.push_back((Edge){from, to, w, v});
edges.push_back((Edge){to, from, w, v});
int m = edges.size();
G[from].push_back(m-);
G[to].push_back(m-);
} void dfs(int x, int fa)
{
dp[x] = ;
for(int i = ; i < G[x].size(); i++)
{
Edge &e = edges[G[x][i]];
if(e.to == fa) continue;
dfs(e.to, x);
if(e.v - dp[e.to] < ) Fail = ;
delta[G[x][i]] = min(e.w-, e.v - dp[e.to]);
dp[x] += (e.w - delta[G[x][i]] + dp[e.to]);
}
} long long ddfs(int x, int fa, long long v)
{
long long ans = ;
//cout<<x<<endl<<endl;
for(int i = ; i < G[x].size(); i++)
{
Edge &e = edges[G[x][i]];
if(e.to == fa) continue;
long long t = min(v, delta[G[x][i]]);
ans += t; v -= t;
dd[G[x][i]] = delta[G[x][i]] - t;
dd[G[x][i]^] = dd[G[x][i]];
long long dt = ddfs(e.to, x, min(v, e.v-dd[G[x][i]]-dp[e.to]));
ans += dt; v -= dt;
}
return ans;
} int n, x, y, w, v;
int main()
{
//freopen("a.txt", "r", stdin);
cin.sync_with_stdio(false);
cin>>n;
for(int i = ; i < n; i++)
{
cin>>x>>y>>w>>v;
addedge(x, y, w, v);
}
dfs(, );
//for(int i = 1; i <= n; i++) cout<<i<<" "<<dp[i]<<endl;
if(Fail) cout<<"-1";
else
{
ddfs(, , Give);
cout<<n<<endl;
for(int i = ; i < *(n-); i += )
{
Edge &e = edges[i];
cout<<e.from<<" "<<e.to<<" "<<e.w - dd[i]<<" "<<e.v - dd[i]<<endl;
}
}
}
Codeforces Round #392 (div.2) E:Broken Tree的更多相关文章
- Codeforces Round #329 (Div. 2) D. Happy Tree Party 树链剖分
D. Happy Tree Party Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/593/p ...
- Codeforces Round #329 (Div. 2) D. Happy Tree Party LCA/树链剖分
D. Happy Tree Party Bogdan has a birthday today and mom gave him a tree consisting of n vertecie ...
- Codeforces Round #225 (Div. 1) C. Propagating tree dfs序+树状数组
C. Propagating tree Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/383/p ...
- Codeforces Round #200 (Div. 1)D. Water Tree dfs序
D. Water Tree Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/343/problem/ ...
- Codeforces Round #375 (Div. 2) F. st-Spanning Tree 生成树
F. st-Spanning Tree 题目连接: http://codeforces.com/contest/723/problem/F Description You are given an u ...
- Codeforces Round #225 (Div. 1) C. Propagating tree dfs序+ 树状数组或线段树
C. Propagating tree Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/383/p ...
- Codeforces Round #200 (Div. 1) D Water Tree 树链剖分 or dfs序
Water Tree 给出一棵树,有三种操作: 1 x:把以x为子树的节点全部置为1 2 x:把x以及他的所有祖先全部置为0 3 x:询问节点x的值 分析: 昨晚看完题,马上想到直接树链剖分,在记录时 ...
- Codeforces Round #200 (Div. 1) D. Water Tree 树链剖分+线段树
D. Water Tree time limit per test 4 seconds memory limit per test 256 megabytes input standard input ...
- Codeforces Round #225 (Div. 2) E. Propagating tree dfs序+-线段树
题目链接:点击传送 E. Propagating tree time limit per test 2 seconds memory limit per test 256 megabytes inpu ...
随机推荐
- Sencha Visual Studio(IDE插件)
Sencha Visual Studio(IDE插件) 首先从官网上下载Visual Studio插件,注意不是VSCode编辑器,下载完后安装打开Visual Studio提示你去注册,输入你的se ...
- angularjs 自定义服务(serive,factory,provder) 以及三者的区别
1.Serive 服务:通过service方式创建自定义服务,相当于new的一个对象:var s = new myService();,只要把属性和方法添加到this上才可以在controller里调 ...
- keil5 mdk调用外部编辑器notepad++、sublime3、VSCode总结
1.打开keil主界面,点击菜单栏Tools菜单,选择如下图所示的选项. 2.点击如下图所示的菜单上红笔标注的地方,给这个工具命名,如notepad++.sublime3.vscode等,如下图, 并 ...
- Jupyter Notebook里面使用Matplotlib画图 图表中文乱码问题
可查看以下链接: https://blog.csdn.net/ccblogger/article/details/79613335
- vuejs中的计算属性和监视
计算属性 1.在computed属性对象中定义计算属性的方法,在页面上使用{{方法名}}来显示计算结果 2.通过getter/setter实现对属性数据的显示和监视 3.计算属性存在缓存,多次读取只执 ...
- R语言绘图:时间序列分析
ggplot2绘制 arima诊断图 library(ggfortify) autoplot(acf(gold[,2], plot = FALSE)) ggtsdiag(auto.arima(gold ...
- NO-ZERO(空格补全)
The NO-ZERO command follows the DATA statement REPORT Z_Test123_01. DATA: W_NUR(10) TYPE N. MOVE 50 ...
- P2678 跳石头(二分答案)
P2678 跳石头 题目背景 一年一度的“跳石头”比赛又要开始了! 题目描述 这项比赛将在一条笔直的河道中进行,河道中分布着一些巨大岩石.组委会已经选择好了两块岩石作为比赛起点和终点.在起点和终点之间 ...
- jsp 添加jstl标签
jsp页面中添加下列代码即可使用jstl标签. <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix=" ...
- C++ 学习笔记之——文件操作和文件流
1. 文件的概念 对于用户来说,常用到的文件有两大类:程序文件和数据文件.而根据文件中数据的组织方式,则可以将文件分为 ASCII 文件和二进制文件. ASCII 文件,又称字符文件或者文本文件,它的 ...