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的更多相关文章

  1. 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 ...

  2. 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 ...

  3. 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 ...

  4. 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/ ...

  5. 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 ...

  6. 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 ...

  7. Codeforces Round #200 (Div. 1) D Water Tree 树链剖分 or dfs序

    Water Tree 给出一棵树,有三种操作: 1 x:把以x为子树的节点全部置为1 2 x:把x以及他的所有祖先全部置为0 3 x:询问节点x的值 分析: 昨晚看完题,马上想到直接树链剖分,在记录时 ...

  8. 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 ...

  9. 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 ...

随机推荐

  1. MB/s与Mbit/s的区别

    数据传输率的单位一般采用MB/s或Mbit/s,尤其在内部数据传输率上官方数据中更多的采用Mbit/s为单位.此处有必要讲解一下两个单位二者之间的差异: MB/s的含义是兆字节每秒,Mbit/s的含义 ...

  2. (九)Pycharm异常、模块

    异常:     当Python检测到一个错误时,解释器就无法继续执行了,反而出现了一些错误的提示,这就是所谓的"异常"   捕获异常:      格式:try+执行代码······ ...

  3. loushang框架的开发中关于BSP的使用,将写好的功能模块部署到主页界面结构上

    前言: 当我们已经开发好相应的模块或者功能的时候,需要将这个功能部署在index主页上作为可点击直接使用的模块,而不是每次需要去浏览对应的url地址. 这时候就需要运用到L5的BSP. 作为刚刚入门l ...

  4. vue项目中缓存问题

    单页面应用总是存在缓存问题,特别是在微信端,更新页面之后访问的还是老页面,缓存的问题是因为用户访问的脚本地址并没有改变,浏览器就会读取原来的脚本 网上有几种解决办法,首先列举一下 1.加meta,禁止 ...

  5. Centos在虚拟机VMware12上的安装

    欢迎评论和更正 虚拟机12的安装 看教程https://blog.csdn.net/yhj19920417/article/details/72891766 centos6.5镜像下载(选minima ...

  6. eclipse全选包

    按住shift键,点击第一个jar包,然后点击最后一个jar包,就全选了所有jar包,然后添加build path 添加到类路径

  7. python之三元运算

    三元运算(三目运算):用于较简单的判断(if   else). if True: return s = "aaaa" else: return s = "bbbb&quo ...

  8. ABAP CDS ON HANA-(12)ODATA Service

    Create a CDS view and we have the view type as ‘BASIC’ view To publish this as oData, add the annota ...

  9. 3 python3 编码解码问题 upd接受数据

    1.python3下的中文乱码:send_data.encode("utf-8") from socket import * udp_socket = socket(AF_INET ...

  10. ip4addr_ntoa和不可重入函数

    在网络中,有一个转换IP地址到ASIIC字符串的函数,该函数的返回值所指向的ASIIC字符串驻留在静态内存中,所以该函数不可重入. 通俗的讲,在多任务系统中,一个任务执行在调用运行这个函数的时候,其他 ...