Water supply
Practice
2.3 (41 votes)
Algorithms
C++
Depth first search
Graphs
Problem
67% Success 7727 Attempts 20 Points 1s Time Limit 256MB Memory 1024 KB Max Code
Your country can be represented by \(N\) cities connected using \(N - 1\) roads. There exists an edge between city \((i,\ i+1)\) for all \(i\) from 1 to \(n - 1\).
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 \(N - 1\) 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 \(i^{th}\) city is blocked else 0.
Output format
Print a single integer denoting the maximum cities to which water can be supplied.
Constraints
\(1 \le N \le 2\times 10^5\)
Submissions
Please login to view your submissions
Similar Problems
Points:20
33 votes
Tags:
AlgorithmsDepth First SearchEasyGraphsTrees
2.Easylife
Points:20
17 votes
Tags:
AlgorithmsDepth First SearchEasyGraphs
Points:20
69 votes
Tags:
Depth First SearchEasyGraphs
Editorial