SDUT2013級測試賽_D

來源:互聯網
上載者:User

標籤:des   style   class   blog   code   ext   

題目描述給出一棵含有n個點的樹,每個點權值為wi,求從根節點到葉子結點權值和最大的那條路經的權值和是多少。輸入

n(1<= n && n <= 10000)。

接下來n+1行,每行兩個整數w(w <= 1000)。

第i個節點的父節點為w,若 i為根節點。600組資料。

輸出 對於每組資料,輸出一個數代表答案。樣本輸入
30 51 51 6
樣本輸出
11
提示 來源 解題報告求從根節點出發到葉子的最長路。。。很像數塔。。。我暴力dfs過了.怒搜所有數枝,搜完答案就有了。。。
#include <iostream>#include <cstdio>#include <cstring>using namespace std;struct node{    int v,next;} edge[100000];int head[10010],cnt,vv[10010],vis[10010],n,maxx;void add(int u,int v){    edge[cnt].v=v;    edge[cnt].next=head[u];    head[u]=cnt++;}void dfs(int s,int d){    int i,j;    for(i=head[s]; i!=-1; i=edge[i].next)        dfs(edge[i].v,d+vv[edge[i].v]);    if(d>maxx)        maxx=d;}int main(){    int i,j,v,w;    while(~scanf("%d",&n))    {        maxx=0;        cnt=0;        memset(edge,0,sizeof(edge));        memset(vv,0,sizeof(vv));        memset(head,-1,sizeof(head));        for(i=0; i<n; i++)        {            scanf("%d%d",&v,&w);            add(v-1,i);            vv[i]=w;        }        dfs(0,vv[0]);        printf("%d\n",maxx);    }    return 0;}

學長標程好像跟數塔類似,從下至上的找最大的。。。下面是嘯爺的標程。。。
#include <algorithm>#include <iostream>#include <cstring>#include <cstdlib>#include <cstdio>#include <queue>#include <cmath>#include <stack>#include <map>#pragma comment(linker, "/STACK:1024000000");#define EPS (1e-8)#define LL long long#define ULL unsigned long long#define _LL __int64#define _INF 0x3f3f3f3f#define Mod 9999991using namespace std;int head[10010];struct E{    int v,next;}edge[10010];int Top;int Link(int u,int v){    edge[Top].v = v;    edge[Top].next = head[u];    head[u] = Top++;}int value[10010];int dfs(int root){    int temp = 0;    for(int p = head[root];p != -1; p = edge[p].next)    {        temp = max(temp,dfs(edge[p].v));    }    value[root] += temp;    return value[root];}int main(){    freopen("data1.in","r",stdin);    freopen("data1.out","w",stdout);    int n,i,j,v,w;    while(scanf("%d",&n) != EOF)    {        memset(head,-1,sizeof(head));        Top = 0;        for(i = 1;i <= n; ++i)        {            scanf("%d %d",&v,&w);            Link(v,i);            value[i] = w;        }        printf("%d\n",dfs(1));    }    return 0;}


相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.