Dessert Café

题目链接: Gym - 102920C .
题目描述:
Kim, who wishes to start a business, is trying to open a dessert cafe he has been preparing after graduating from college. The road network in the town where Kim lives forms a tree structure, that is, a connected acyclic graph as shown in the figure below. There are n candidate sites for a dessert café in the town. In the figure below, a circle represents a candidate site for a dessert café, a line segment between two candidate sites represents a road, and the value labeled on a line segment represents the length of a road.
在这里插入图片描述
There are k apartment complexes in this town, so he wants his dessert café to be located as close as possible to an apartment complex. In above figure, there are three apartment complexes which are located to the candidate sites labeled by A, B, and C. Considering the competitiveness and profitability, he thinks that a candidate site satisfying the following condition is a good place.

Let d(x,y) be the length of the shortest path on a road network between two candidate sites x and y. A candidate site p is a good place if there exists a candidate site z where an apartment complex is located such that d(p,z)<d(q,z) for each candidate site q (≠p).

In above figure, candidate sites 2, 4, 5, 6, 8, and 9 are good places. More specifically, for example, candidate 6 is a good place because it is closer to apartment complex B than any other candidate sites except for candidate 5, and is closer to apartment complex A than candidate 5. That is, there exists apartment complex B on candidate 5 satisfying d(6,5)<d(q,5) for q∈{1,2,3,4,7,8,9}, and there exists apartment complex A on candidate 2 satisfying d(6,2)<d(5,2). Candidate 7 is not a good place because none of apartment complexes are closer than candidate 6.

Given the information on candidate sites and apartment complexes in the town, write a program to output the number of good places.
Input:
Your program is to read from standard input. The input starts with a line containing two integers, n and k (3≤n≤100,000, 1≤k≤n), where n is the number of candidate sites and k is the number of apartment complexes. The candidate sites are numbered from 1 to n. In the following n−1 lines, each line contains three integers, i, j, and w (1≤i,j≤n, 1≤w≤1,000), where i and j are candidate sites and w is the length of the road between i and j. The last line contains k integers which represent the locations of apartment complexes in the town.
Output:
Your program is to write to standard output. Print exactly one line. The line should contain the number of good places.
Examples:
input:
9 3
1 2 8
2 4 7
4 3 6
4 6 4
5 6 3
6 7 2
6 9 5
9 8 6
2 5 8
output:
6

input:
4 4
1 2 1
1 3 1
1 4 1
2 4 1 3
output:
4

题意:首先给出一张完整的无向图以及图中每个相邻节点的距离,然后给出若干个关键节点。定义重要的节点为到所以节点距离之和最小的点,问共有多少个重要的节点。

思路:建无向图,然后将所有的关键节点的标记值定为1,从某一关键节点出发,开始DFS,在递归之后将节点的标记值与子节点进行异或,最后得到的标记值为1的节点的总数,就是重要节点的个数。因为是以某一关键节点为祖先开始DFS,所以值为1的节点就是在所有关键节点中所围绕的点,其距离一定最小,边全在递归过程中无用。

#include<bits/stdc++.h>
#include<iostream>
#include<cctype>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<queue>
#include<stack>
using namespace std;
typedef unsigned long long ull;
typedef  long long ll;
#define IOS std::ios::sync_with_stdio(false)
#define ls p<<1
#define rs p<<1|1
#define mod 1000000000 + 7
#define PI acos(-1.0)
#define INF   1e9
#define N 2000000 + 5
/*******************Code*******************/
int n,m,cnt,head[N],ans,sum[N];
struct node{
    int u,v,nxt,w;
}e[N];
void _add(int u,int v,int w){
    e[++cnt].u = u;
    e[cnt].v = v;
    e[cnt].w = w;
    e[cnt].nxt = head[u];
    head[u] = cnt;
}
void dfs(int u,int fa){
    for(int i = head[u];i;i=e[i].nxt){
        int v = e[i].v;
        if(v==fa)
            continue;
        dfs(v,u);
        sum[u] |=sum[v];
    }
}
int main(void){
    IOS;
    cin>>n>>m;
    for(int i = 1;i < n;i++){
        int u,v,w;
        cin>>u>>v>>w;
        _add(u,v,w);
        _add(v,u,w);
    }
    int p;
    for(int i = 0;i < m;i++){
        int x;
        cin>>x;
        p = x;
        sum[x] = 1;
    }
    dfs(p,0);
    for(int i = 1;i<=n;i++)
        ans +=sum[i];
    cout<<ans<<endl;
    return 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值