• 2015 Multi-University Training Contest 3 1010(DFS)


    Crazy Bobo

    Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others)
    Total Submission(s): 905    Accepted Submission(s): 271


    Problem Description
    Bobo has a tree,whose vertices are conveniently labeled by 1,2,...,n.Each node has a weight wi. All the weights are distrinct.
    A set with m nodes v1,v2,...,vm is a Bobo Set if:
    - The subgraph of his tree induced by this set is connected.
    - After we sort these nodes in set by their weights in ascending order,we get u1,u2,...,um,(that is,wui<wui+1 for i from 1 to m-1).For any node x in the path from ui to ui+1(excluding ui and ui+1),should satisfy wx<wui.
    Your task is to find the maximum size of Bobo Set in a given tree.
     
    Input
    The input consists of several tests. For each tests:
    The first line contains a integer n (1n500000). Then following a line contains n integers w1,w2,...,wn (1wi109,all the wi is distrinct).Each of the following n-1 lines contain 2 integers ai and bi,denoting an edge between vertices ai and bi (1ai,bin).
    The sum of n is not bigger than 800000.
     
    Output
    For each test output one line contains a integer,denoting the maximum size of Bobo Set.
     
    Sample Input
    7
    3 30 350 100 200 300 400
    1 2
    2 3
    3 4
    4 5
    5 6
    6 7
     
    Sample Output
    5
     
    Source
     

    题意:给定一棵树,每个点都有其权值,现在有对集合的定义:

    1、集合是树的一个子图并且要连通

    2、点按照权值排序,求排序后相邻的两点X,Y,原路径上两点间的任意点的权值要小于min(X,Y)

    求符合条件的集合最多含有多少个点

    分析:官方题解说是在原来树上的点,权值小的到权值大的构成一条单项边,然后求权值小的点为起点构成的最大的点集。

    可是题目意思明明是求排序后相邻的两个点的原路径上符合条件的点集最大值啊!o(╯□╰)o

    可以这么想,因为是权值小的点向权值大的点构成一条单项边,如果存在一条边,那么可以沿着这条边继续搜下去,当边不存在的时候说明两端的点是集合里面最大的两个点,有因为在一棵树上,所以这两点的排序后的位置是相邻的。

    #pragma comment(linker, "/STACK:1024000000,1024000000")
    #include<cstdio>
    #include<string>
    #include<iostream>
    #include<cstring>
    #include<cmath>
    #include<stack>
    #include<queue>
    #include<vector>
    #include<map>
    #include<stdlib.h>
    #include<algorithm>
    #define LL __int64
    using namespace std;
    const int MAXN=500000+5;
    int n;
    int val[MAXN];
    int cn[MAXN];
    vector<int> w[MAXN];
    
    int DFS(int s)
    {
        if(cn[s]!=0) return cn[s];
    
        cn[s]=1;
        for(int i=0;i<w[s].size();i++)
            cn[s]+=DFS(w[s][i]);
        return cn[s];
    }
    
    int main()
    {
        //freopen("in.txt","r",stdin);
        while(scanf("%d",&n)!=EOF)
        {
            memset(cn,0,sizeof(cn));
            memset(vis,0,sizeof(vis));
            for(int i=1;i<=n;i++)
            {
                scanf("%d",&val[i]);
                w[i].clear();
            }
    
            for(int i=1;i<n;i++)
            {
                int u,v;
                scanf("%d %d",&u,&v);
                if(val[u]<val[v]) w[u].push_back(v);
                if(val[v]<val[u]) w[v].push_back(u);
            }
    
            for(int i=1;i<=n;i++)
                if(!cn[i])
                    DFS(i);
    
            int ans=-1;
            for(int i=1;i<=n;i++)
                if(ans<cn[i])
                    ans=cn[i];
    
            printf("%d
    ",ans);
        }
        return 0;
    }
    View Code
  • 相关阅读:
    postgresql 简单入门
    自用 docker-compose
    前后端分离 开发环境通过CORS实现跨域联调
    导出 java.io.IOException: 权限不够
    mysql 数值与字符类型 长度梳理
    easyui input未设id导致的问题
    springmvc处理url请求步骤
    js中的变量提升(hoisting)
    auto semicolon insertion 自动分号补齐的坑
    $.ajax dataType设置为json 回调函数不执行
  • 原文地址:https://www.cnblogs.com/clliff/p/4686117.html
Copyright © 2020-2023  润新知