青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品

oyjpArt ACM/ICPC算法程序設計空間

// I am new in programming, welcome to my blog
I am oyjpart(alpc12, 四城)
posts - 224, comments - 694, trackbacks - 0, articles - 6

中南賽A題 Accumulation Degree

Posted on 2008-05-05 20:59 oyjpart 閱讀(3069) 評論(9)  編輯 收藏 引用 所屬分類: ACM/ICPC或其他比賽
Accumulation Degree
Time Limit: 5000MS
Memory Limit: 65536K
Total Submissions: 248
Accepted: 30

Description

Trees are an important component of the natural landscape because of their prevention of erosion and the provision of a specific ather-sheltered ecosystem in and under their foliage. Trees have also been found to play an important role in producing oxygen and reducing carbon dioxide in the atmosphere, as well as moderating ground temperatures. They are also significant elements in landscaping and agriculture, both for their aesthetic appeal and their orchard crops (such as apples). Wood from trees is a common building material.

Trees also play an intimate role in many of the world's mythologies. Many scholars are interested in finding peculiar properties about trees, such as the center of a tree, tree counting, tree coloring. A(x) is one of such properties.

A(x) (accumulation degree of node x) is defined as follows:

  1. Each edge of the tree has an positive capacity.
  2. The nodes with degree of one in the tree are named terminals.
  3. The flow of each edge can't exceed its capacity.
  4. A(x) is the maximal flow that node x can flow to other terminal nodes.

Since it may be hard to understand the definition, an example is showed below:


A(1)=11+5+8=24
Details: 1->2 11
  1->4->3 5
  1->4->5 8(since 1->4 has capacity of 13)
A(2)=5+6=11
Details: 2->1->4->3 5
  2->1->4->5 6
A(3)=5
Details: 3->4->5 5
A(4)=11+5+10=26
Details: 4->1->2 11
  4->3 5
  4->5 10
A(5)=10
Details: 5->4->1->2 10

The accumulation degree of a tree is the maximal accumulation degree among its nodes. Here your task is to find the accumulation degree of the given trees.

Input

The first line of the input is an integer T which indicates the number of test cases. The first line of each test case is a positive integer n. Each of the following n - 1 lines contains three integers x, y, z separated by spaces, representing there is an edge between node x and node y, and the capacity of the edge is z. Nodes are numbered from 1 to n.
All the elements are nonnegative integers no more than 200000. You may assume that the test data are all tree metrics.

Output

For each test case, output the result on a single line.
 

Sample Input

1
5
1 2 11
1 4 13
3 4 5
4 5 10

Sample Output

26

Source


這道題的基本思想是樹形DP,如果不能理解的話請試圖把雙向邊看成兩個單向邊,再比劃比劃就出來了。
當然不一定非要以邊做為DP的單元,也可以歸到邊上(如果你有那份心的話)。
比賽的時候因為數據量大而Stack Overflow,一直想寫人工模擬棧,但因為沒寫過,在比賽中寫不出來。

五一節虛心的跟alpc62學習了怎么寫人工模擬棧,核心思想就是將同一個DFS內的不同DFS做個標記,這樣在出棧的時候就可以判斷自己所處的位置,也就知道自己該采取什么行動了。
比如
void DFS(int x) {
    for(int i = 0; i < head[x].size(); ++i) {
       DFS(head[x][i]);
    }
}
如果把(x, i)這個2元組壓入棧也就知道自己現在所處的地方了。
如果有更多的內部DFS,同樣是加對應的標記。

當然,BFS也是一種很好的選擇(應該說大多數隊伍會選擇BFS而不是人工模擬棧)

//Accumulation Degree in BFS

#include <vector>
#include <algorithm>
#include <iostream>
using namespace std;

#define Min(a, b) (a<b?a:b)
#define Max(a, b) (a>b?a:b)

struct Node
{
    int x, i, pre;
    Node() {}
    Node(int xx, int ii, int pp) {x=xx, i = ii, pre=pp;}
};

struct Edge
{
    int x, w, dp;
    Edge() {}
    Edge(int xx, int ww, int dd=0) { x=xx,w=ww,dp=dd;}
};

const int N = 200010;
vector<Edge> e[N];
bool chk[N];
int n, flow[N];

void solve() {
    int i, j, k;
    vector<Node> Q;

    fill(chk, chk + n, 0);
    fill(flow, flow + n, 0);

    for(i = 0; i < n && e[i].size()!=1; ++i);
    int st = 0, end = 0;
    chk[i] = 1;
    for(j = 0; j < e[i].size(); ++j) {
        Q.push_back(Node(i, j, -1));
        end++;
        chk[e[i][j].x] = 1;
    }
    while(st < end) {
        int x = e[Q[st].x][Q[st].i].x, pre = Q[st].pre;
        for(i = 0; i < e[x].size(); ++i) {
            if(!chk[e[x][i].x]) {
                Q.push_back(Node(x, i, st));
                end++;
                chk[e[x][i].x] = 1;
            }
        }
        ++st;
    }
    for(i = end-1; i >= 0; --i) {
        int x = Q[i].x, pre = Q[i].pre, idx = Q[i].i;
        if(e[e[x][idx].x].size() == 1) e[x][idx].dp = e[x][idx].w;
        else e[x][idx].dp = Min(e[x][idx].dp, e[x][idx].w);
        if(pre == -1) continue;
        int prex = Q[pre].x, preidx = Q[pre].i;
        e[prex][preidx].dp += e[x][idx].dp;
    }


    for(i = 0; i < e[Q[0].x].size(); ++i) {
        flow[Q[0].x] += e[Q[0].x][i].dp;
    }
    for(i = 0; i < end; ++i) {
        int x = Q[i].x, pre = Q[i].pre, idx = Q[i].i;
        int y = e[x][idx].x, xx;
        for(xx = 0; xx < e[y].size() && e[y][xx].x != x; ++xx);
        if(pre == -1) {
            e[y][xx].dp = e[y][xx].w;
        }
        else {
            e[y][xx].dp = Min(e[y][xx].dp, e[y][xx].w);
        }
        for(j = 0; j < e[y].size(); ++j) {
            flow[y] += e[y][j].dp;
        }
        for(j = 0; j < e[y].size(); ++j) {
            int yy = e[y][j].x;
            if(yy == x) continue;
            for(k = 0; k < e[yy].size() && e[yy][k].x != y; ++k);
            e[yy][k].dp = flow[y] - e[y][j].dp;
        }
    }

    int max = 0;
    for(i = 0; i < n; ++i)
        max = Max(max, flow[i]);
    printf("%d\n", max);
}

int main() {
    int ntc;
    int i;
    int x, y, w;
    scanf("%d", &ntc);
    while(ntc--) {
        scanf("%d", &n);
        for(i = 0; i < n; ++i) e[i].clear();
        for(i = 0; i < n-1; ++i) {
            scanf("%d %d %d", &x, &y, &w);
            --x; --y;
            e[x].push_back(Edge(y, w));
            e[y].push_back(Edge(x, w));
        }
        solve();
    }
    return 0;
}


Feedback

# re: 中南賽A題 Accumulation Degree  回復  更多評論   

2008-05-06 14:41 by wlzb
不錯呀,上原創精華了

# re: 中南賽A題 Accumulation Degree  回復  更多評論   

2008-05-06 18:00 by oyjpart
哦?

# re: 中南賽A題 Accumulation Degree  回復  更多評論   

2008-05-12 21:15 by alpc55
太強了,你竟然模擬棧……

# re: 中南賽A題 Accumulation Degree  回復  更多評論   

2008-05-13 22:52 by ecnu_zp
哦~~
學習學習·~

公網能進你們的oj系統嗎??

# re: 中南賽A題 Accumulation Degree  回復  更多評論   

2008-05-13 22:52 by ecnu_zp
哦~~
學習學習·~

公網能進你們的oj系統嗎??
教育網

# re: 中南賽A題 Accumulation Degree  回復  更多評論   

2008-05-13 23:50 by oyjpart
我們是軍網 外網應該不能訪問

# re: 中南賽A題 Accumulation Degree  回復  更多評論   

2008-05-14 17:15 by ecnu_zp
我還是不太明白啊~
我想的dp是N^2A的,因為要對所有點執行一次~~
我弱,能不能教我一下啊。

ecnu_zp@yahoo.cn
QQ:345717212
MSN: arena_zp@live.cn

^_^

# re: 中南賽A題 Accumulation Degree  回復  更多評論   

2008-05-14 20:08 by oyjpart
每條邊拆成2條邊 。 然后對每條邊設一個DP值。
比如邊A->B. B連接的其他點的集合叫做S(S中去掉A)
dp[A->B] = Min(Capacity[A->B], 加合(dp[B->Ci]));
可以通過2次DFS來求出這些DP值。第一次求出一個方向的邊的DP值,再一次求出反向。
試著畫個圖來理解吧:)

# re: 中南賽A題 Accumulation Degree  回復  更多評論   

2008-07-26 06:06 by lengbufang
看看!!
青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            亚洲成色www久久网站| 在线亚洲国产精品网站| 亚洲国产精品电影在线观看| 男女精品视频| 亚洲一区www| 欧美激情视频一区二区三区免费| 亚洲精品在线免费观看视频| 久久国产免费| 欧美性理论片在线观看片免费| 欧美美女福利视频| 玉米视频成人免费看| 久久激情五月激情| 亚洲欧美综合国产精品一区| 欧美人体xx| 国产在线精品一区二区夜色| 欧美一区2区视频在线观看| 日韩亚洲在线| 国产欧美日韩精品在线| 欧美伊人影院| 亚洲欧美国产精品桃花| 国产精品午夜电影| 久久精品色图| 欧美成人亚洲成人| 亚洲欧洲日韩女同| 久久综合一区二区| 亚洲第一二三四五区| 欧美成人一二三| 欧美日韩国产综合视频在线观看中文 | 亚洲一区久久| 国产精品久久久亚洲一区| 亚洲一区视频在线| 欧美中文日韩| 亚洲巨乳在线| 欧美在线视频网站| 女生裸体视频一区二区三区| 最新高清无码专区| 亚洲欧美综合v| 日韩一级免费| 欧美一区二区三区喷汁尤物| 激情欧美丁香| 中文网丁香综合网| 亚洲国产婷婷香蕉久久久久久| 亚洲免费高清| 亚洲欧洲在线视频| 欧美一区二区免费| 尤物yw午夜国产精品视频明星| 一本大道久久精品懂色aⅴ| 伊人成人网在线看| 亚洲午夜伦理| 亚洲午夜在线| 欧美日韩性视频在线| 欧美+亚洲+精品+三区| 国产亚洲精品bt天堂精选| 夜久久久久久| 久久婷婷色综合| 久久久亚洲午夜电影| 国产精品综合av一区二区国产馆| 久久精品国产2020观看福利| 欧美四级剧情无删版影片| 99精品国产高清一区二区| 中文久久乱码一区二区| 欧美日韩免费一区| 亚洲无线观看| 久久久精品视频成人| 国产一区二区主播在线| 欧美在线一二三四区| 免费在线欧美视频| 合欧美一区二区三区| 久久婷婷丁香| 亚洲高清一区二区三区| 亚洲免费在线| 欧美午夜视频网站| 亚洲欧美精品在线观看| 麻豆九一精品爱看视频在线观看免费| 在线日韩成人| 欧美在线一区二区三区| 日韩一级成人av| 久久国产主播精品| 亚洲毛片一区| 1204国产成人精品视频| 国产精品一区二区三区成人| 欧美极品欧美精品欧美视频| 亚洲精品影院在线观看| 美日韩精品免费观看视频| 午夜精品一区二区三区在线视 | 午夜精品一区二区三区在线| 一区二区三区鲁丝不卡| 国产精品成人av性教育| 欧美成年人网站| 免费观看在线综合色| 欧美久久99| 性18欧美另类| 亚洲女女做受ⅹxx高潮| 亚洲欧洲精品一区二区三区不卡 | 亚洲午夜在线观看| 亚洲视频每日更新| 夜夜嗨av一区二区三区中文字幕 | 亚洲国产综合视频在线观看| 午夜亚洲伦理| 亚洲综合国产激情另类一区| 91久久久亚洲精品| 亚洲专区欧美专区| 久久久国产精品一区二区中文 | 亚洲电影免费观看高清完整版在线观看 | 久久青青草原一区二区| 亚洲欧美日韩精品久久久久| 亚洲在线播放电影| 亚洲一区二区在线免费观看视频| 亚洲综合三区| 亚洲乱码精品一二三四区日韩在线| 亚洲在线一区二区| 亚洲一区二区三区四区五区午夜| 久久国产精品网站| 久久成人精品电影| 黑人一区二区| 久久久亚洲高清| 欧美激情一区二区三区成人| 亚洲成色777777在线观看影院| 久久久亚洲高清| 欧美激情在线免费观看| 亚洲精品人人| 欧美午夜电影网| 欧美一区二区三区久久精品| 久热爱精品视频线路一| 亚洲精品裸体| 国产精品视频一区二区三区| 久久国产福利| 国产精品99久久久久久www| 亚洲精品一区二区三区不| 欧美不卡激情三级在线观看| 欧美日韩亚洲一区三区| 国产综合激情| 欧美与黑人午夜性猛交久久久| 久久久久网址| 亚洲精品久久久一区二区三区| 一本大道久久a久久综合婷婷| 欧美一级二级三级蜜桃| 国产婷婷一区二区| 欧美肥婆在线| 影音先锋久久精品| 欧美激情影音先锋| 午夜精品偷拍| 亚洲国产一区二区三区青草影视| 亚洲一区二区免费看| 一区二区三区无毛| 国产精品v欧美精品∨日韩| 久久精品国产一区二区三区| 99精品国产热久久91蜜凸| 免费中文日韩| 久久国产精品久久国产精品 | 亚洲黄色影院| 久久xxxx| 亚洲一区二区在线看| 亚洲国产日韩欧美在线图片| 国产精品一区二区三区四区五区 | 久久精品麻豆| 一区二区三区四区国产| 亚洲成色777777女色窝| 久久久综合网站| 午夜日韩福利| 亚洲午夜在线| 亚洲三级免费电影| 在线成人av| 狠狠入ady亚洲精品| 国产精品视频1区| 欧美日韩一区二区三区四区在线观看 | 欧美日韩一区二区欧美激情| 久久综合九色九九| 欧美综合国产| 午夜视频在线观看一区二区| 国产精品99久久99久久久二8| 亚洲精品国产精品国自产在线 | 蜜臀va亚洲va欧美va天堂| 欧美精品久久99久久在免费线| 亚洲精品日韩在线| 欧美诱惑福利视频| 亚洲一区在线免费观看| 久久噜噜噜精品国产亚洲综合| 99国产一区| 久久精品人人做人人爽| 欧美一区二区三区精品| 欧美伦理视频网站| 亚洲二区在线视频| 黄色亚洲精品| 韩国久久久久| 一区二区在线视频| 亚洲国产日韩综合一区| 欧美成人精品激情在线观看| 久久影院亚洲| 久久资源av| 欧美~级网站不卡| 亚洲成色www8888| 91久久精品一区二区三区| 亚洲黄色成人久久久| 亚洲人成7777| 在线视频精品一区| 午夜精品短视频| 久久国产精品99国产| 久久婷婷国产麻豆91天堂| 欧美成人日韩|