Question:
In a town, there are m intersections and n roads. These roads are one-way and will not form a ring. Now we need some paratroopers to inspect the town,
The paratroopers can only follow the path and ask how many paratroopers are required to search all intersections. This question is converted to the most efficient solution for Directed Acyclic graphs.
The small path overwrites the problem.
Conclusion: The minimum path overwrite of a directed acyclic graph = the number of vertices of the graph-the maximum matching of the graph.
AC code:
[Cpp]
# Include <iostream>
Using namespace std;
Const int MAX = 121;
Int no_of_intersections, no_of_streets, t;
Int link [MAX]; // record the starting point of the current link to the street endpoint
Bool map [MAX] [MAX]; // store a bipartite graph. If there is a path from the start point to the end point, map [x] [y] = true; otherwise, false
Bool useif [MAX]; // records whether the intersection point is used)
Void getMap () // save Graph
{
Memset (map, false, sizeof (map ));
Int a, B;
For (int I = 1; I <= no_of_streets; I ++)
{
Cin> a> B;
Map [a] [B] = true;
}
}
Bool can (int t)
{
For (int I = 1; I <= no_of_intersections; I ++)
{
If (! Useif [I] & map [t] [I])
{
Useif [I] = true;
If (link [I] =-1 | can (link [I])
{
Link [I] = t;
Return true;
}
}
}
Return false;
}
Int maxMatch ()
{
Int num = 0;
Memset (link, int (-1), sizeof (link); // if the link is not connected, the value is-1.
For (int I = 1; I <= no_of_intersections; I ++) // search for the augmented path from the street start point
{
Memset (useif, false, sizeof (useif ));
If (can (I) num ++;
}
Return num;
}
Int main ()
{
Cin> t;
While (t --)
{
Cin> no_of_intersections> no_of_streets;
GetMap ();
Cout <no_of_intersections-maxMatch () <endl;
}
Return 0;
}
From ON THE WAY