Water supply

2.3

41 votes
Algorithms, C++, Depth First Search, Graphs
Problem

Your country can be represented by N cities connected using N1 roads. There exists an edge between city (i, i+1) for all i from 1 to n1.

You have to set up a connection for water supply. You set this in one city and water gets transported from it to other cities using road transport. Certain cities are blocked which means that water cannot pass through this city. Determine the maximum number of cities to which water can be supplied.

Input format

  • The first line contains an integer N denoting the number of cities.
  • The next N1 lines contain two space-separated integers u v denoting a road between city u and v.
  • The next line contains N space-separated integers where it is 1 if the ith city is blocked else 0.

Output format
Print a single integer denoting the maximum cities to which water can be supplied.

Constraints
1N2×105

Time Limit: 1
Memory Limit: 256
Source Limit:
Explanation

Mayor will choose city number 2 and supply water to cities 2, 1. (Note city 3 can also be selected , water is supplied to 3 and 4)

Editor Image

?