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. (九)Pycharm异常、模块

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

  2. jquery点击按钮复制内容

    做移动端的项目遇到一个需求要点击按钮复制dom里的内容,看了很多资料显示必须textarea或者input里的内容才能简单复制,还有就是用插件的了,最终都因为遇到各种问题放弃,最终选择了最简单的点击复 ...

  3. Cacti 学习笔记

    Cacti是用php语言实现的一个软件,它的主要功能是用snmp服务获取数据,然后用rrdtool储存和更新数据,当用户需要查看数据的时候用rrdtool生成图表呈现给用户.因此,snmp和rrdto ...

  4. PHP 序列化与反序列化简单理解

    序列化就是把保存在内存中的各种对象状态,保存起来,在需要的时候还原出来. string serialize ,产生一个可以储存的表示 mixed unserialize 从已储存的表示中创建php值( ...

  5. 用状态机表示SFC中的并行分支

    过去一直认为,状态机表示SFC会不会是任务复杂化,这次简单实验了一下,感觉还可以.请看下面的控制. 在SFC中,A和B是一对并行分支,汇合后转移到C分支中,怎么了用状态机表示呢?这里我们在状态机里分别 ...

  6. ant + jmeter 自动化接口测试环境部署

    1.jdk下载安装 下载地址:http://www.oracle.com/technetwork/java/javase/downloads/index.html 2.jmeter下载 jmeter官 ...

  7. HDU暑假多校第六场K-werewolf

    一.题意 好人必然说真话,坏人不一定说真话,给定N个人的言论<每人一个发言.不谈及自己>,要求指出有多少个人一定是好人,有多少个人一定是坏人.#define 狼人 坏人#define 村民 ...

  8. Quartz,启动不立即执行问题

    我的Quartz 是2.2版本, 在java程序中写了两个加入计划方法 //// 添加简单计划任务 author:iresearch.com.cn -- jackical public static ...

  9. c#获取当前运行程序所在的目录

    C#获取项目程序及运行路径的方 1.asp.net webform用“Request.PhysicalApplicationPath获取站点所在虚拟目录的物理路径,最后包含“\”:   2.c# wi ...

  10. Python字符串处理:过滤字符串中的英文与符号,保留汉字

    使用Python 的re模块,re模块提供了re.sub用于替换字符串中的匹配项. re.sub(pattern, repl, string, count=0) 参数说明: pattern:正则重的模 ...