Poj3107 (DFS, tree DP)

Source: Internet
Author: User

The method is exactly the same as that of poj1655, but the range of N in this question is large, and the map will be saved with a vector, so you can use the forward star to save the graph.

Here, we will explain the method of saving a graph to the forward star:

 

 

In fact, we use a static linked list to implement an adjacent linked list, which can avoid using pointers.

The head [I] Array records the first edge of each node. Each edge is stored using the struct E [I], E [I]. V indicates the point pointed by this edge, E [I]. next indicates the next edge of the edge link.

It cleverly lies in the fact that each time it is inserted into the head rather than the end of the linked list, it avoids traversing the linked list. The order of each edge at the same starting point in the adjacent table is exactly the opposite of the read order.

Post template:

Struct node {int V, next;} e [m]; // M indicates the total number of edges. Int head [N], CNT; // n indicates the total number of nodes, CNT record Edge Number void Init () {memset (Head,-1, sizeof (head); CNT = 0;} void add (int u, int V) {e [CNT]. V = V; E [CNT]. next = head [u]; head [u] = CNT ++ ;}

AC code:

#include<iostream>#include<cstdio>#include<cstdlib>#include<cstring>#include<cmath>#include<map>#include<set>#include<vector>#include<algorithm>#include<stack>#include<queue>using namespace std;#define INF 100000000#define eps 1e-8#define pii pair<int,int>#define LL long long intstruct node{    int v,next;}e[100010];int n,a,b,head[50005],mi=1;int num[50005],bal[50005],cnt=1;void add(int aa,int bb);int dfs1(int x,int fa);void dfs2(int x,int fa);int main(){    //freopen("in1.txt","r",stdin);    //freopen("out.txt","w",stdout);    scanf("%d",&n);    memset(head,-1,sizeof(int)*(n+1));    for(int i=1;i<=n;i++) num[i]=1;    for(int i=1;i<=n-1;i++)    {        scanf("%d%d",&a,&b);        add(a,b);        add(b,a);    }    dfs1(1,-1);    dfs2(1,-1);    for(int i=1;i<=n;i++)    {        if(bal[i]<bal[mi])        {            mi=i;        }    }    printf("%d",mi);    for(int i=mi+1;i<=n;i++)    {        if(bal[i]==bal[mi])        {            printf(" %d",i);        }    }    printf("\n");    //fclose(stdin);    //fclose(stdout);    return 0;}void add(int aa,int bb){    e[cnt].v=bb;    e[cnt].next=head[aa];    head[aa]=cnt++;}int dfs1(int x,int fa){    for(int i=head[x];i!=-1;i=e[i].next)    {        if(e[i].v==fa)            continue;        else        {            num[x]+=dfs1(e[i].v,x);        }    }    return num[x];}void dfs2(int x,int fa){    for(int i=head[x];i!=-1;i=e[i].next)    {        if(e[i].v==fa)        {            bal[x]=max(bal[x],n-num[x]);        }        else        {            bal[x]=max(bal[x],num[e[i].v]);            dfs2(e[i].v,x);        }    }}
View code

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.