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

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 閱讀(3083) 評論(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>
            亚洲精品乱码久久久久| 最新日韩在线| 欧美一区二区在线| 国产区欧美区日韩区| 久久久久久精| 久久久精品国产一区二区三区| 精品白丝av| 亚洲欧洲日产国码二区| 欧美日韩亚洲一区三区 | 国产精品视频成人| 欧美一区精品| 久久女同精品一区二区| 亚洲欧洲精品天堂一级| 日韩午夜视频在线观看| 国产欧美一区二区三区在线看蜜臀| 久久久不卡网国产精品一区| 麻豆精品视频在线观看| 亚洲性av在线| 久久精品国产999大香线蕉| 亚洲国产精品一区二区三区| 99xxxx成人网| 在线成人中文字幕| 亚洲看片一区| 黄色成人在线免费| 亚洲激情黄色| 国产午夜精品美女毛片视频| 亚洲黄色视屏| 国产一区二区三区在线播放免费观看| 欧美高清hd18日本| 国产精品你懂的| 欧美成人午夜影院| 国产精品一国产精品k频道56| 欧美 日韩 国产在线| 欧美午夜影院| 亚洲国产免费| 国产一区二区成人| 一本色道久久88综合亚洲精品ⅰ | 91久久精品美女高潮| 99精品免费视频| 亚洲高清色综合| 亚洲宅男天堂在线观看无病毒| 91久久在线| 午夜一区二区三视频在线观看| 亚洲精品在线免费| 久久久一二三| 久久精品国产亚洲5555| 欧美香蕉视频| 亚洲国产一区在线| 在线观看av不卡| 亚洲免费影视| 亚洲欧美一区二区三区久久| 欧美理论电影网| 欧美bbbxxxxx| 亚洲高清一二三区| 久久精品国产99精品国产亚洲性色| 亚洲尤物在线视频观看| 欧美国产三区| 欧美激情欧美狂野欧美精品| 亚洲第一黄色| 久久久久久穴| 欧美va日韩va| 在线精品国精品国产尤物884a| 欧美主播一区二区三区美女 久久精品人 | 久久在线免费| 蜜臀久久99精品久久久久久9| 国产亚洲一区二区三区在线观看 | 欧美专区在线观看一区| 亚洲欧美一区二区三区极速播放| 欧美日韩一区成人| 在线视频你懂得一区| 亚洲一区视频在线| 国产精品二区三区四区| 亚洲午夜视频| 欧美专区日韩视频| 国产亚洲视频在线观看| 久久大香伊蕉在人线观看热2| 久久久欧美精品| 狠狠色香婷婷久久亚洲精品| 久久美女性网| 亚洲欧洲视频在线| 亚洲综合第一| 国产一区二区三区在线观看精品 | 欧美有码在线视频| 免费成人网www| 亚洲精品影院| 国产精品国产三级国产aⅴ9色| 亚洲一区二区在线播放| 久久久精品欧美丰满| 在线观看的日韩av| 欧美精品成人| 亚洲免费视频观看| 免费观看日韩| 亚洲视频中文| 国产日韩欧美二区| 免费美女久久99| 正在播放亚洲| 媚黑女一区二区| 亚洲午夜羞羞片| 黑人一区二区三区四区五区| 欧美激情一区二区三区成人| 亚洲尤物影院| 免费欧美电影| 午夜精品三级视频福利| 亚洲国产成人精品久久久国产成人一区| 欧美国产日韩一二三区| 在线视频亚洲| 欧美激情aⅴ一区二区三区| 中文国产一区| 在线成人小视频| 国产精品午夜国产小视频| 久久一区二区三区四区| 亚洲一区免费视频| 亚洲国产一二三| 久久久精品999| 一区二区三区 在线观看视频| 国内精品视频一区| 欧美色图一区二区三区| 久色婷婷小香蕉久久| 午夜精品久久一牛影视| 亚洲风情在线资源站| 久久久久国产精品麻豆ai换脸| 一区二区三区欧美| 亚洲二区免费| 好男人免费精品视频| 国产精品久久久久久久久借妻 | 香蕉成人伊视频在线观看| 91久久久久久久久久久久久| 久久嫩草精品久久久久| 亚洲欧美日韩另类| 一个色综合导航| 亚洲人成网站777色婷婷| 激情综合激情| 国产一区二区三区免费观看| 国产精品一区二区三区成人| 欧美日韩一区二区国产| 欧美成人免费播放| 美女脱光内衣内裤视频久久网站| 亚洲综合色自拍一区| 一区二区av在线| 一二三区精品| 99人久久精品视频最新地址| 亚洲精品欧美专区| 亚洲精品日韩综合观看成人91| 欧美a级一区| 欧美激情 亚洲a∨综合| 欧美大片免费看| 欧美+日本+国产+在线a∨观看| 久久亚洲私人国产精品va| 久久久综合免费视频| 久久久999精品| 久久亚洲精品网站| 免费欧美电影| 欧美激情无毛| 亚洲精品三级| 亚洲香蕉网站| 亚洲欧美变态国产另类| 欧美一区三区三区高中清蜜桃 | 校园激情久久| 欧美在线免费观看| 久久视频在线看| 蜜臀av在线播放一区二区三区| 欧美成在线视频| 欧美三级视频在线观看| 国产伦精品一区二区三区视频孕妇 | 欧美在线黄色| 久久综合成人精品亚洲另类欧美 | 亚洲一线二线三线久久久| 亚洲伊人一本大道中文字幕| 欧美一区二区三区视频免费| 久久中文精品| 欧美日韩一区自拍| 国产日韩欧美综合精品| 亚洲国产免费看| 亚洲一级二级在线| 久久久久久久久久久成人| 欧美高清日韩| 亚洲一区观看| 美女黄毛**国产精品啪啪 | 久久国产福利| 欧美激情亚洲综合一区| 国产精品自拍小视频| 亚洲国产欧美久久| 亚洲免费在线播放| 欧美jjzz| 亚洲一区二区三区四区视频| 久久字幕精品一区| 国产精品扒开腿做爽爽爽视频 | 国产精品久久国产愉拍| 国内精品久久久久影院色| 一本色道久久综合狠狠躁篇的优点 | 欧美日在线观看| 极品少妇一区二区三区精品视频| 一本色道久久99精品综合| 久久久久久9| 一本大道久久a久久精二百| 久久人人精品| 国产色爱av资源综合区| 中文av字幕一区| 亚洲高清不卡av| 久久久久久久一区|