Uvalive 6534 join two kingdoms tree DP + binary

Source: Internet
Author: User

Link: https://icpcarchive.ecs.baylor.edu/index.php? Option = com_onlinejudge & Itemid = 8 & page = show_problem & problem = 4545

Two countries, A and B, have n cities and Q cities (1 ≤ n, Q ≤ 4 × 10 ^ 4). Each country has a tree structure, the weight of each edge is 1. Now we need to randomly select a city from each of the two countries to connect the two countries and ask what the longest path of the connected country is.

Idea: first, use the tree-like DP to find the longest distance that can be found in the country, and record the longest Len in the longest distance. Then sort all the longest roads of B, and for each city of A, when a chooses this city, the longest road for each city of B is max (Len, dp_a [I] [0] + dp_ B [J] [0] + 1), where dp_a [I] [0], dp_ B [J] [0] represents the distance between the two cities. Find the position that satisfies dp_a [I] [0] + dp_ B [J] [0] + 1> Len in binary mode, the length greater than this position is dp_a [I] [0] + dp_ B [J] [0] + 1, and Len. In this way, expectations can be obtained.

Code:

#include <algorithm>#include <cmath>#include <cstdio>#include <cstdlib>#include <cstring>#include <ctime>#include <ctype.h>#include <iostream>#include <map>#include <queue>#include <set>#include <stack>#include <string>#include <vector>#define eps 1e-8#define INF 0x7fffffff#define maxn 40005#define PI acos(-1.0)#define seed 31//131,1313typedef long long LL;typedef unsigned long long ULL;using namespace std;int from_a[maxn],head_a[maxn],top_a;int from_b[maxn],head_b[maxn],top_b;double dp_a[maxn][2], dp_b[maxn][2];double aa[maxn],bb[maxn];struct Edge{    int v;    int next;} edge_a[maxn*2],edge_b[maxn*2];void init(){    memset(head_a,-1,sizeof(head_a));    memset(dp_a,0,sizeof(dp_a));    memset(head_b,-1,sizeof(head_b));    memset(dp_b,0,sizeof(dp_b));    top_a=0;    top_b=0;}void add_edge_a(int u,int v){    edge_a[top_a].v=v;    edge_a[top_a].next=head_a[u];    head_a[u]=top_a++;}void add_edge_b(int u,int v){    edge_b[top_b].v=v;    edge_b[top_b].next=head_b[u];    head_b[u]=top_b++;}void dfs_first(int u,int f,int head[],Edge edge[],int from[],double dp[][2]){    from[u]=u;    for(int i=head[u]; i!=-1; i=edge[i].next)    {        int v=edge[i].v;        if(v==f)            continue;        dfs_first(v,u,head,edge,from,dp);        if(dp[v][0]+1>dp[u][0])        {            from[u]=v;            dp[u][1]=dp[u][0];            dp[u][0]=dp[v][0]+1;        }        else if(dp[v][0]+1>dp[u][1])            dp[u][1]=dp[v][0]+1;    }}void dfs_second(int u,int f,int from[],double dp[][2],int head[],Edge edge[]){    if(u!=f)        if(from[f]!=u)        {            if(dp[f][0]+1>dp[u][0])            {                from[u]=f;                dp[u][1]=dp[u][0];                dp[u][0]=dp[f][0]+1;            }            else if(dp[f][0]+1>dp[u][1])                dp[u][1]=dp[f][0]+1;        }        else        {            if(dp[f][1]+1>dp[u][0])            {                from[u]=f;                dp[u][1]=dp[u][0];                dp[u][0]=dp[f][1]+1;            }            else if(dp[f][1]+1>dp[u][1])                dp[u][1]=dp[f][1]+1;        }    for(int i=head[u]; i!=-1; i=edge[i].next)    {        int v=edge[i].v;        if(v==f)            continue;        dfs_second(v,u,from,dp,head,edge);    }}int main(){    int T_a,T_b;    while(~scanf("%d%d",&T_a,&T_b))    {        init();        for(int i=1;i<=T_a-1;i++)        {            int u,v;            scanf("%d%d",&u,&v);            add_edge_a(u,v);            add_edge_a(v,u);        }        dfs_first(1,1,head_a,edge_a,from_a,dp_a);        dfs_second(1,1,from_a,dp_a,head_a,edge_a);        for(int i=1;i<=T_b-1;i++)        {            int u,v;            scanf("%d%d",&u,&v);            add_edge_b(u,v);            add_edge_b(v,u);        }        dfs_first(1,1,head_b,edge_b,from_b,dp_b);        dfs_second(1,1,from_b,dp_b,head_b,edge_b);        double cc=0;        double ans=0;        for(int i=1;i<=T_a;i++)        {            if(dp_a[i][0]>cc)                cc=dp_a[i][0];        }        for(int i=1;i<=T_b;i++)        {            bb[i]=dp_b[i][0];            if(bb[i]>cc)                cc=bb[i];        }        sort(bb+1,bb+T_b+1);        double S[maxn];        S[1]=bb[1];        for(int i=2;i<=T_b;i++)        {            S[i]=S[i-1]+bb[i];        }        for(int i=1;i<=T_a;i++)        {            int pos=lower_bound(bb+1,bb+T_b+1,cc-dp_a[i][0]-1)-bb;            ans+=(double)(pos-1)*cc;            ans+=(T_b-pos+1)*(dp_a[i][0]+1)+S[T_b]-S[pos-1];        }        printf("%.3f\n",ans/((double)T_a*T_b)+eps);    }    return 0;}


Uvalive 6534 join two kingdoms tree DP + binary

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.