I came across a problem from the last Facebook Hacker Cup (so this is not my homework, I just find it very interesting), and I also thought of a curious, but rather good solution. Could you check my mind? Here is the task:
You are provided with a network with N cities and M bi-directional roads connecting these cities. The first cities are important K. You need to remove the minimum number of roads, so there are no cycles in the remaining network that contain important cities. A cycle is a sequence of at least three different cities in which each pair of neighboring cities is connected by a road and the first and last city in the sequence are also connected by a road.
Enter
The first line contains the number of test cases T.
Each case begins with a line containing the integers N, M, and K, which represent the number of cities, the number of roads, and the number of important cities, respectively. Cities are numbered from 0 to N-1, and important cities are numbered from 0 to K-1. The next M lines contain two integers a [i] and b [i], 0 ≤ i <M, which represent two different cities connected by a road.
It is guaranteed that 0 ≤ a [i], b [i] N and a [i] ≠ b [i]. There will be one road between the two cities.
Exit
For each of the test cases, numbered in the order from 1 to T, print “Case No. i:” followed by a single integer, the minimum number of roads that should be free of cycles that contain an important city.
Limitations
1 ≤ T ≤ 20
1 ≤ N ≤ 10000
1 ≤ M ≤ 50,000
1 ≤ K ≤ N
Example
In the first example, we have N = 5 cities that are connected M = 7 roads and cities 0 and 1. We can remove two roads connecting (0, 1) and (1, 2), and the remaining network will not contain cycles with important cities. Please note that in the remaining network there is a cycle that contains only non-important cities, and there are several ways to remove two roads and fulfill all conditions. You cannot delete only one road and destroy all cycles containing important cities.
Input example
one
5 7 2
0 1
12
14
0 2
2 4
2 3
3 4
Therefore, I thought about it this way: when plotting, let's have a separate array that stores information about how many neighbors each city makes (== how many roads are connected to this city). In the example case, city 0 has 2, city 1 has 3, and so on. Let me call these numbers the "city value" of a particular city.
After receiving all the input, we look through the entire array of city values, looking for cities with a value of 1. When you reach one, it means that it cannot be in a cycle, so we decrease its value “delete” (without loss of generality) the path connecting with its only neighbor and reducing neighboring value. After that, we recursively go to the neighborhood, checking the same thing, if the value is 1, repeat the scheme and go deeper recursively. If this is not the case, do not touch.
After this operation, we cleared all parts of the graph that are not cycles and cannot be part of one. We also got rid of all the removal of roads that made no sense. Therefore, we call another function, this time - we work only in important cities. Thus, we take vertex 1 - after using the function described in the previous paragraph, its value cannot be 1 (since it would already be a zero function), so it is either 0 or something> 1. In the first case we don’t have to do anything. In the latter case, we must make the value 1, which is performed by removing the values of 1. As in the previous paragraph, after each deletion, we reduce the cost of both this city and its neighbor, also removing the road. We repeat this for all important cities, summing the value -1 from all important cities and our response.
Is there any reason? For all the tests, I tried to work, and I would like to believe it correctly, but somehow I feel that there might be a leak somewhere. Could you check it out? It's good? If not, then why is there anything right in this thinking process? :)