• The Cave


    The Cave

    题目描述

     

    给定一棵有n个节点的树,相邻两点之间的距离为1。

    请找到一个点x,使其满足所有m条限制,其中第i条限制为dist(x,a[i])+dist(x,b[i])<=d[i]。


    solution

    假设一号点就是答案点。

    依次考虑每一条限制

    一号点到答案点距离为

    max(0,dist(1,ai)+dist(1,bi)-Di)/2

    考虑最深的那一条限制,如果取那条限制的最浅的那个点不合法,那么其他也一定不合法。

    dfs判断

    #include<cstdio>
    #include<iostream>
    #include<cstdlib>
    #include<cstring>
    #include<algorithm>
    #include<cmath>
    #define maxn 300005
    using namespace std;
    int T,n,m,head[maxn],t1,t2,t3,tot,a[maxn],b[maxn],c[maxn];
    int d[4][maxn];
    struct node{
        int v,nex;
    }e[maxn*2];
    void lj(int t1,int t2){
        e[++tot].v=t2;e[tot].nex=head[t1];head[t1]=tot;
    }
    void dfs(int k,int fa,int l,int op){
        d[op][k]=l;
        for(int i=head[k];i;i=e[i].nex){
            if(e[i].v==fa)continue;
            dfs(e[i].v,k,l+1,op);
        }
    }
    void Q(){
        memset(head,0,sizeof head);tot=0;
    }
    int main()
    {
        cin>>T;
        while(T--){
            cin>>n>>m;Q();
            for(int i=1;i<n;i++){
                scanf("%d%d",&t1,&t2);
                lj(t1,t2);lj(t2,t1);
            }
            dfs(1,0,0,0);
            int len=0,S=0;
            for(int i=1;i<=m;i++){
                scanf("%d%d%d",&t1,&t2,&t3);
                a[i]=t1;b[i]=t2;c[i]=t3;
                int tmp=d[0][t1]+d[0][t2]-t3;
                if(!S||tmp>len)len=tmp,S=i;
            }
            dfs(a[S],0,0,1);dfs(b[S],0,0,2);
            int st=0,l=1e9;
            for(int i=1;i<=n;i++){
                if(d[1][i]+d[2][i]<=c[S]){
                    if(!st)st=i,l=d[0][i];
                    else if(d[0][i]<l)l=d[0][i],st=i;
                }
            }
            if(!st){puts("NIE");continue;}
            dfs(st,0,0,3);bool fl=0;
            for(int i=1;i<=m;i++){
                if(d[3][a[i]]+d[3][b[i]]>c[i]){fl=1;break;}
            }
            if(!fl){printf("TAK %d
    ",st);}
            else puts("NIE");
        }
        return 0;
    }
  • 相关阅读:
    php发送post请求的方法
    跨域请求的三种解决办法
    php验证码+js点击刷新
    13.mysql数据类型
    12.dateformat常用格式
    11.设计的三大范式
    nginx passwd (http://www.voidcn.com/article/p-suebfyqy-nx.html)
    删除文件 过滤某个文件
    mac必装软件
    elasticsearch 安装
  • 原文地址:https://www.cnblogs.com/liankewei/p/10358756.html
Copyright © 2020-2023  润新知