Problem 2169 Shadow

Source: Internet
Author: User

problem 2169 Shadowaccept:141 submit:421
Time limit:1000 mSec Memory limit:32768 KB Problem Description

YL is the king of Shadow State, shadow state-owned N cities.

In order to save money, Shadow country only has N-1 road, this N-1 road makes N City Connect. One year, the shadow rebellion, the rebels occupy a number of cities, the king is in a precarious state.

The Kings were numbered 1 cities, except the Kings had yl armies in the K-Cities. Now the K-troops are marching to the king. And destroy the rebels in the city along the way. Given the road conditions in N cities and the number of rebels in the city, how many rebels must be eliminated in total?

 Input

In the first line, enter two integers n,k, followed by the N (1<=n<=100000) integer Ai (0<=ai<=10000), which represents the number of rebels in the city of first I. Next enter an integer of k greater than or equal to 1 and less than or equal to N. A number representing the city with an army.

Data guarantee capital and the cities with armies have no rebels. Next enter the N-1 line. two integers per line u, V, represents a road connecting U and v.

Each army can only walk along the road, and is the shortest route between its city and the king.

 Output

The output line is an integer representing the number of rebels killed.

 Sample Input4 2
0 3 0 0
3 4
1 2
2 3
2 4 Sample OUTPUT3

http://acm.fzu.edu.cn/problem.php?pid=2169

/* Walk through the city of BFs with an army and find capital. The number of rebels that have been killed along the way
Storing neighbor relationships with two arrays
*/
#include <cstdio>
#include <cstring>
#include <queue>
using namespace Std;
#define N 100005
struct list
{
int Val;
int NXT;
}L[N*2]; Store the relationship of the neighboring side
struct POINT
{
int Val;
int k_num; Kill enemy Numbers
}p,q;
int enemy[n]; Rebels
int army[n]; Communists
int vis[n]; Mark whether to interview
int head[n];
int n,k,id;
void Add (int u,int v)//connect u behind V
{
L[id].val=v; Holds the value of V and has a unique ID
L[id].nxt=head[u]; V's next is head[u], where Head[u] represents the ID of the head node of U, and connects it to the back of V
head[u]=id++;//at this point because the head node of U is already connected to V, so update the head node, change the head[u] to the ID of V
}
int BFS (int x)
{
int ret=0;
queue<point>s;
P.val=x;
P.K_NUM=ENEMY[X];
S.push (P);
Vis[x]=1;
while (!s.empty ())
{
P=s.front ();
S.pop ();
if (1==p.val)//Find target Point
Ret=p.k_num;
for (int i=head[p.val];i!=0;i=l[i].nxt)
{
int v=l[i].val;
if (0==vis[v])
{
Vis[v]=1;
Q.val=v;
Q.K_NUM=P.K_NUM+ENEMY[V];
S.push (q);
}
}
}
return ret;
}

int main ()
{
while (scanf ("%d%d", &n,&k)!=eof)
{
for (int i=1;i<=n;i++)
scanf ("%d", &enemy[i]);
for (int i=1;i<=k;i++)
scanf ("%d", &army[i]);
memset (vis,0,sizeof (VIS));
memset (head,0,sizeof (head));
int ans=0;
id=1;
for (int i=1;i<=n-1;i++)
{
int u,v;
scanf ("%d%d", &u,&v);
Add (U,V);
Add (V,u);
}
for (int i=1;i<=k;i++)
{
if (0==vis[army[i]])
ANS+=BFS (Army[i]);
}
printf ("%d\n", ans);
}
return 0;
}

Problem 2169 Shadow

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.