• 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
  • 相关阅读:
    Leetcode刷题笔记
    Leetcode刷题笔记
    朋友发来的图片,要制作成身份证复印件,怎么办?
    记录一次MAC连接投影闪屏的问题。
    win10系统 端口查看问题。
    使用Windows命令行reg控制注册表键值
    SVN钩子HOOK设置自动备份,服务本地可以看到所有更新内容。
    报错代码:svn-http status413'requset entity too large
    SVN: Cleanup failed update报错 文件被锁定lock办法,cleanup 失效报错。
    Samba centos7文件共享服务器搭建教程,可以更改任意需求操作配置详解。
  • 原文地址:https://www.cnblogs.com/clliff/p/4686117.html
Copyright © 2020-2023  润新知