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. 【php学习-5】

    mutil_query($result)){ //多查询 where } //执行查询 /* $result=$cone->query("SELECT * from test" ...

  2. CodeTimer 代码性能计数器

    收集整理老赵 的”CodeTimer“. 用于测试代码性能.详见可参考 老赵原文 代码如下: using System; using System.Diagnostics; using System. ...

  3. python学习之控制流2

    配置环境:python 3.6 python编辑器:pycharm 代码如下: #!/usr/bin/env python #-*- coding: utf-8 -*- # 控制流语句: # if语句 ...

  4. C语言实现计算二进制数字1的个数

    #include<stdio.h> #include<stdlib.h> int print_one_bits01(unsigned int value){ //0000 11 ...

  5. uva 509 RAID!(磁盘数据)

    来自 https://blog.csdn.net/su_cicada/article/details/80085318 习题4-7 RAID技术(RAID!, ACM/ICPC World Final ...

  6. R语言绘图:时间序列分析 ggplot2绘制ACF PACF

    R语言真是博大精深 方法一 Acf(gold[,2], type = "correlation",lag.max = 100) Acf(gold[,2], type = " ...

  7. 2018 ccpc final I. Cockroaches

    I. Cockroaches time limit per test6. s memory limit per test256 MB inputstandard input outputstandar ...

  8. python2.7练习小例子(五)

        5):题目:输入三个整数x,y,z,请把这三个数由小到大输出.     程序分析:我们想办法把最小的数放到x上,先将x与y进行比较,如果x>y则将x与y的值进行交换,然后再用x与z进行比 ...

  9. 15 GIL 全局解释器锁 C语言解决 top ps

    1.GIL 全局解释器锁:保证同一时刻只有一个线程在运行. 什么是全局解释器锁GIL(Global Interpreter Lock) Python代码的执行由Python 虚拟机(也叫解释器主循环, ...

  10. LeetCode:18. 4Sum(Medium)

    1. 原题链接 https://leetcode.com/problems/4sum/description/ 2. 题目要求 给出整数数组S[n],在数组S中是否存在a,b,c,d四个整数,使得四个 ...