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

oyjpArt ACM/ICPC算法程序設(shè)計(jì)空間

// 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 閱讀(3089) 評(píng)論(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,如果不能理解的話請(qǐng)?jiān)噲D把雙向邊看成兩個(gè)單向邊,再比劃比劃就出來了。
當(dāng)然不一定非要以邊做為DP的單元,也可以歸到邊上(如果你有那份心的話)。
比賽的時(shí)候因?yàn)閿?shù)據(jù)量大而Stack Overflow,一直想寫人工模擬棧,但因?yàn)闆]寫過,在比賽中寫不出來。

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

當(dāng)然,BFS也是一種很好的選擇(應(yīng)該說大多數(shù)隊(duì)伍會(huì)選擇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  回復(fù)  更多評(píng)論   

2008-05-06 14:41 by wlzb
不錯(cuò)呀,上原創(chuàng)精華了

# re: 中南賽A題 Accumulation Degree  回復(fù)  更多評(píng)論   

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

# re: 中南賽A題 Accumulation Degree  回復(fù)  更多評(píng)論   

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

# re: 中南賽A題 Accumulation Degree  回復(fù)  更多評(píng)論   

2008-05-13 22:52 by ecnu_zp
哦~~
學(xué)習(xí)學(xué)習(xí)·~

公網(wǎng)能進(jìn)你們的oj系統(tǒng)嗎??

# re: 中南賽A題 Accumulation Degree  回復(fù)  更多評(píng)論   

2008-05-13 22:52 by ecnu_zp
哦~~
學(xué)習(xí)學(xué)習(xí)·~

公網(wǎng)能進(jìn)你們的oj系統(tǒng)嗎??
教育網(wǎng)

# re: 中南賽A題 Accumulation Degree  回復(fù)  更多評(píng)論   

2008-05-13 23:50 by oyjpart
我們是軍網(wǎng) 外網(wǎng)應(yīng)該不能訪問

# re: 中南賽A題 Accumulation Degree  回復(fù)  更多評(píng)論   

2008-05-14 17:15 by ecnu_zp
我還是不太明白啊~
我想的dp是N^2A的,因?yàn)橐獙?duì)所有點(diǎn)執(zhí)行一次~~
我弱,能不能教我一下啊。

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

^_^

# re: 中南賽A題 Accumulation Degree  回復(fù)  更多評(píng)論   

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

# re: 中南賽A題 Accumulation Degree  回復(fù)  更多評(píng)論   

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>
            国产精品日韩| 午夜精品久久久久久久蜜桃app | 亚洲综合999| 国产精品美女主播在线观看纯欲| 亚洲午夜久久久久久尤物| 欧美在线观看一二区| 黄色一区二区在线| 欧美精品九九| 中文在线不卡视频| 久久综合九色综合欧美就去吻| 亚洲国产高清在线| 欧美三级视频在线观看| 欧美伊人久久| 亚洲国产婷婷香蕉久久久久久| 一区二区三区鲁丝不卡| 国内精品国语自产拍在线观看| 男人的天堂亚洲| 亚洲制服少妇| 亚洲国产欧美日韩| 欧美一区视频| 999亚洲国产精| 国产欧美日韩三区| 欧美精品久久久久久久久老牛影院| 99国产精品私拍| 蜜桃久久av| 亚洲欧美一区二区在线观看| 亚洲第一区中文99精品| 国产精品乱人伦一区二区| 久热精品在线| 午夜综合激情| 亚洲精品在线免费| 老司机精品视频网站| 亚洲一区二区三区久久| 亚洲国内精品| 国产亚洲一级| 国产精品激情电影| 欧美黄色aaaa| 久久婷婷蜜乳一本欲蜜臀| 中文国产成人精品| 亚洲娇小video精品| 久久久久国产成人精品亚洲午夜| 一本大道久久a久久精二百| 在线国产日韩| 国产综合在线看| 国产精品尤物福利片在线观看| 欧美激情一区二区三区全黄| 久久久久成人精品| 欧美一区二区国产| 亚洲天堂网站在线观看视频| 亚洲人成网站777色婷婷| 男女精品视频| 麻豆精品精华液| 久久久久久久999| 久久福利毛片| 欧美一区二区精美| 香蕉久久夜色| 亚洲——在线| 亚洲一区二区在线免费观看| 亚洲精品中文字幕在线观看| 亚洲激情视频| 亚洲国产综合在线| 亚洲国产欧美一区二区三区久久 | 先锋a资源在线看亚洲| 99xxxx成人网| 一区二区日本视频| 一区二区三区www| 99精品国产在热久久婷婷| 亚洲精品午夜| 一本色道久久综合亚洲91| 日韩视频免费观看高清在线视频| 亚洲国产日韩精品| 亚洲精品久久| 99国产成+人+综合+亚洲欧美| 日韩亚洲精品视频| 在线视频亚洲一区| 亚洲欧美另类久久久精品2019| 亚洲一区免费在线观看| 亚洲欧美在线一区| 久久精品99国产精品日本| 久久九九热re6这里有精品| 久久成人精品| 蜜桃av一区二区在线观看| 欧美ed2k| 国产精品高潮呻吟久久av黑人| 国产精品进线69影院| 国产欧美一二三区| 在线播放视频一区| 亚洲精品一区二区在线观看| 99riav国产精品| 亚洲一区二区毛片| 欧美专区日韩视频| 欧美成人免费在线| 日韩视频在线观看免费| 亚洲一级片在线看| 久久精品成人一区二区三区| 麻豆成人av| 国产精品爱啪在线线免费观看| 国产精品女人毛片| 在线高清一区| 亚洲欧美激情视频| 免费观看日韩| 999亚洲国产精| 欧美伊久线香蕉线新在线| 欧美 日韩 国产在线| 国产精品都在这里| 在线欧美三区| 亚洲欧美日韩中文在线制服| 久久亚洲欧美| 一区二区免费在线视频| 久久精品免费观看| 欧美日韩另类综合| 伊人久久大香线蕉av超碰演员| 亚洲精品三级| 久久九九久精品国产免费直播| 亚洲第一毛片| 小处雏高清一区二区三区| 欧美不卡在线| 国产一区二区三区四区五区美女 | 一本色道**综合亚洲精品蜜桃冫| 午夜视频在线观看一区二区三区| 欧美成ee人免费视频| 亚洲一二三区精品| 欧美精品在线视频观看| 国产一区二区三区在线观看视频| 日韩视频一区| 欧美99在线视频观看| 亚洲一区二区在线免费观看| 欧美成人国产一区二区| 国产在线播放一区二区三区| 亚洲午夜免费视频| 欧美激情四色| 久久精品视频va| 国产精品资源在线观看| 一区二区三区四区精品| 欧美国产日韩一区| 久久国产精品久久国产精品| 国产精品欧美一区喷水| 一本久道综合久久精品| 欧美黑人在线播放| 久久精品五月婷婷| 国产一区二区精品| 欧美亚洲视频在线观看| 一区二区三区高清不卡| 欧美精品三级| 亚洲乱码国产乱码精品精可以看| 玖玖玖国产精品| 欧美在线免费观看| 国产视频一区在线| 欧美一区2区三区4区公司二百| 一本一本久久| 欧美私人网站| 中日韩午夜理伦电影免费| 亚洲精品一区二区三区99| 欧美二区视频| 亚洲精选中文字幕| 亚洲国产精品va在线看黑人动漫| 久久综合九色欧美综合狠狠| 伊人伊人伊人久久| 免费成人小视频| 久久九九国产精品怡红院| 黄网站免费久久| 猛干欧美女孩| 欧美gay视频激情| 亚洲美女视频网| 亚洲精品美女久久7777777| 欧美精品激情| 一区二区三区四区五区精品视频| 亚洲日本va午夜在线影院| 欧美日韩高清在线播放| 在线视频亚洲| 亚洲综合导航| 国产自产2019最新不卡| 久久这里有精品15一区二区三区| 久久精品成人| 亚洲精品一区二区三| 亚洲美女黄色| 国产日韩久久| 欧美激情一区二区三区在线| 欧美激情乱人伦| 亚洲永久免费观看| 久久国产欧美日韩精品| 亚洲国产精品t66y| 日韩午夜视频在线观看| 国产精品欧美风情| 美日韩在线观看| 欧美日韩国产成人在线| 亚洲欧美综合国产精品一区| 久久高清福利视频| 亚洲免费观看在线观看| 亚洲一区久久| 亚洲国产高清一区| 一区二区久久久久久| 韩日在线一区| 亚洲日本中文字幕区| 国产日韩精品一区二区| 欧美国产精品专区| 国产精品久久午夜夜伦鲁鲁| 久久这里只精品最新地址| 欧美日韩免费一区二区三区视频 | 国产伦精品一区二区|