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

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>
            亚洲欧美日韩精品| 亚洲欧美日韩区| 欧美成在线视频| 美女精品在线观看| 亚洲乱码久久| 一本久久综合亚洲鲁鲁五月天 | 欧美在线观看视频一区二区| 亚洲欧美日韩国产一区二区三区| 国产亚洲aⅴaaaaaa毛片| 老司机一区二区| 欧美精品激情在线观看| 午夜精品一区二区三区四区| 性欧美暴力猛交69hd| 亚洲国产精品久久91精品| 亚洲美女毛片| 国产亚洲精品自拍| 欧美激情在线狂野欧美精品| 欧美色区777第一页| 久久夜色精品国产欧美乱| 欧美成人影音| 久久精品一区二区三区不卡| 欧美成人资源| 久久国产一二区| 欧美精品久久99久久在免费线| 亚洲欧美成人一区二区三区| 久久久久久69| 亚洲视频999| 久久久www| 亚洲在线视频一区| 欧美插天视频在线播放| 久久爱www久久做| 欧美日韩精品一区二区三区四区| 久久久久国产精品一区三寸| 欧美人成在线| 欧美1级日本1级| 国产视频一区在线观看| 亚洲区欧美区| 在线观看91精品国产麻豆| 亚洲一区二区三区视频| 日韩午夜av| 免费在线观看日韩欧美| 久久精品视频网| 国产精品永久| 在线午夜精品自拍| 艳妇臀荡乳欲伦亚洲一区| 久久久亚洲国产天美传媒修理工| 欧美一级久久久| 国产精品v欧美精品v日韩精品| 亚洲丰满少妇videoshd| 在线播放日韩专区| 久久精品国产69国产精品亚洲| 欧美亚洲免费高清在线观看| 国产精品扒开腿爽爽爽视频| 亚洲国内精品在线| 亚洲精品国产精品国自产观看浪潮| 欧美诱惑福利视频| 久久久精品日韩欧美| 国产视频一区欧美| 欧美一区二区三区精品电影| 新狼窝色av性久久久久久| 国产精品高潮久久| 在线亚洲一区观看| 香蕉免费一区二区三区在线观看 | 亚洲男人av电影| 香蕉久久一区二区不卡无毒影院| 欧美性猛交视频| 亚洲综合色噜噜狠狠| 欧美怡红院视频| 国产一区观看| 久久久亚洲国产天美传媒修理工| 美国十次成人| 亚洲人午夜精品| 欧美精品一区二区在线播放| 亚洲精品社区| 午夜日韩电影| 一区免费观看视频| 你懂的视频欧美| 亚洲蜜桃精久久久久久久| 亚洲一级免费视频| 国产日韩欧美综合| 蜜月aⅴ免费一区二区三区| 亚洲欧洲精品天堂一级| 亚洲一区二区三区四区五区午夜 | 激情小说另类小说亚洲欧美| 久久免费高清| 亚洲美女视频网| 欧美一级片久久久久久久 | 蜜臀91精品一区二区三区| 亚洲欧洲一区二区在线观看| 亚洲综合成人在线| 精品99一区二区| 欧美三级网页| 久久精品日韩欧美| 日韩一级片网址| 久久综合九色综合久99| 一本色道久久综合亚洲精品不| 国产精品美腿一区在线看| 麻豆精品在线视频| 亚洲欧美日韩国产综合在线| 欧美国产精品人人做人人爱| 亚洲淫性视频| 亚洲人成欧美中文字幕| 国产精品色网| 欧美激情视频一区二区三区免费| 亚洲欧美综合另类中字| 亚洲黄一区二区三区| 久久久久国内| 中文欧美在线视频| 亚洲第一主播视频| 国产伦精品一区二区三区| 欧美夫妇交换俱乐部在线观看| 午夜精品美女久久久久av福利| 91久久久久久久久久久久久| 久久蜜桃av一区精品变态类天堂| 一区二区三区免费观看| 91久久国产精品91久久性色| 国产亚洲欧美另类一区二区三区| 欧美日韩在线亚洲一区蜜芽| 麻豆精品一区二区综合av| 欧美一区二区三区日韩| 在线一区亚洲| 99国产精品久久久久久久成人热| 欧美韩日高清| 欧美福利视频在线观看| 久热这里只精品99re8久| 久久国产精品久久久久久| 亚洲一区二区免费| 亚洲手机成人高清视频| 日韩午夜三级在线| 亚洲人成网站999久久久综合| 国产在线精品一区二区夜色| 国产日韩欧美精品综合| 国产女主播一区二区| 国产精品萝li| 国产欧美精品| 国产日韩在线视频| 国产一区二区三区久久悠悠色av | 国产精品中文在线| 国产精品区免费视频| 国产精品xxx在线观看www| 欧美日韩免费观看一区三区| 欧美日本韩国在线| 欧美日韩三级| 国产精品久久久久久久久免费桃花| 欧美日韩一区二区三区四区五区 | 欧美伊人精品成人久久综合97| 亚洲欧美日本伦理| 欧美在线欧美在线| 久久久久一本一区二区青青蜜月| 久久久蜜臀国产一区二区| 麻豆精品传媒视频| 欧美久久影院| 国产精品久久久免费| 国产一区二区三区在线观看精品 | 欧美精品久久一区| 欧美性一区二区| 国产日韩高清一区二区三区在线| 国产一区二区三区直播精品电影| 伊人久久成人| 99热在线精品观看| 亚欧美中日韩视频| 你懂的国产精品| 99re6这里只有精品| 亚洲在线成人精品| 毛片av中文字幕一区二区| 欧美日韩在线视频观看| 国产午夜精品理论片a级大结局| 在线观看欧美日韩| 亚洲视频在线视频| 久久综合九色综合欧美狠狠| 亚洲欧洲在线一区| 销魂美女一区二区三区视频在线| 久久久久一区二区三区| 欧美日韩一区在线观看| 国产一区二区三区av电影| 亚洲精品无人区| 欧美在线资源| 亚洲日本va午夜在线电影| 亚洲免费中文| 欧美日韩国产精品| 一区精品在线| 欧美在线观看你懂的| 亚洲国产成人久久综合| 亚洲欧美日韩国产| 欧美连裤袜在线视频| 国产综合第一页| 亚洲永久免费观看| 亚洲电影在线看| 久久国产66| 国产精品视频第一区| 日韩视频一区二区三区| 久久久美女艺术照精彩视频福利播放| 亚洲欧洲另类国产综合| 久久精品国产免费看久久精品| 欧美日韩综合在线| 亚洲九九爱视频| 欧美凹凸一区二区三区视频| 午夜精品免费视频| 国产精品久久久一区麻豆最新章节| 亚洲国产精品v|