Hdustm1the King's problem (strongly connected contraction point + minimum path overwrite)

Source: Internet
Author: User
The King's Problem Time Limit: 2000/1000 MS (Java/others) memory limit: 65536/32768 K (Java/Others)
Total submission (s): 1606 accepted submission (s): 584


Problem descriptionin the kingdom of silence, the King has a new problem. there are n cities in the Kingdom and there are m directional roads between the cities. that means that if there is a road from u to V, you can only go from City U to city V, but can't go from city V to City U. in order to rule his kingdom more than tively, the King want to divide his kingdom into several States, and each city must belong to exactly one state. what's more, for each pair of city (u, v), if there is one way to go from u to V and go from V to U, (u, v) have to belong to a same state. and the king must insure that in each state we can ether go from u to V or go from V to u between every pair of cities (u, v) without passing any city which belongs to other state.
Now the king asks for your help, he wants to know the least number of states he have to divide the kingdom.
Inputthe first line contains a single integer t, the number of test cases. And then followed t cases.

The first line for each case contains two integers n, m (0 <n <= 5000,0 <= m <= 100000), the number of cities and roads in the Kingdom. the next M lines each contains two integers U and V (1 <= u, v <= N), indicating that there is a road going from City U to city v.
Outputthe output shoshould contain t lines. For each test case you shoshould just output an integer which is the least number of states the king have to divide.
Sample Input
13 21 21 3

Sample output
2

Source2011 multi-university training contest 3-host by bit. If a road from a city can go without a road, all the cities on this road will be divided into one country, and each city can only belong to one country, of course, a city on the ring belongs to the same country. Ask the minimum number of countries in which these cities are divided. Problem-solving: it is obvious that the minimum path overwrites, but the directed graph contains loops, and the bipartite graph is correct only when directed acyclic graphs exist, therefore, a directed acyclic graph is re-formed with a strongly connected contraction point. And then perform binary matching.
# Include <stdio. h >#include <iostream >#include <vector> using namespace STD; int vist [5005], Match [5005]; vector <int> map [5005]; int find (int I) {for (Int J = 0; j <map [I]. size (); j ++) if (vist [map [I] [J] = 0) {vist [map [I] [J] = 1; if (Match [map [I] [J] = 0 | find (Match [map [I] [J]) {match [map [I] [J] = I; return 1 ;}} return 0 ;}// the following link is used to scale the ring into a vertex, turn the entire graph into a directed acyclic graph ------ int stack [5005], Sn, low [5005], dfn [5005], deep, node [5005], K; int min (int A, int B) {return A> B? B: A;} void DFS (int I) {stack [++ Sn] = I; vist [I] = 1; deep ++; low [I] = dfn [I] = deep; For (Int J = 0; j <map [I]. size (); j ++) if (vist [map [I] [J] = 0) {DFS (Map [I] [J]); low [I] = min (low [I], low [map [I] [J]);} else if (vist [map [I] [J] = 1) // this point in the stack is low [I] = min (low [I], dfn [map [I] [J]); If (low [I] = dfn [I]) // point I in the stack and the point at the top of the stack is a strongly connected component {k ++; while (stack [Sn]! = I) {node [stack [Sn] = K; // indicates that the original node stack [Sn] is a class K vertex after the point is reduced. Vist [stack [Sn] = 2; // pass, but the outbound stack sn --;} node [stack [Sn] = K; vist [stack [Sn] = 2; sn -- ;}} int resetmap (int n) {vector <int> MP [5005]; Sn = 0; deep = 0; k = 0; For (Int J = 1; j <= N; j ++) vist [J] = 0; For (INT I = 1; I <= N; I ++) if (vist [I] = 0) DFS (I); For (INT I = 1; I <= N; I ++) // scale down to form a new graph for (Int J = 0; j <map [I]. size (); j ++) if (node [I]! = Node [map [I] [J]) // MP [node [I] in the new graph. push_back (node [map [I] [J]); For (INT I = 1; I <= K; I ++) map [I] = MP [I]; return K; // returns the number of vertices in The New Graph} int main () {int t, n, m, a, B, ans; scanf ("% d ", & T); While (t --) {scanf ("% d", & N, & M); For (INT I = 1; I <= N; I ++) map [I]. clear (), Match [I] = 0; while (M --) {scanf ("% d", & A, & B); map [A]. push_back (B);} n = resetmap (n); // The scaled down point is a new directed acyclic graph (ANS) = 0; For (INT I = 1; I <= N; I ++) {for (Int J = 1; j <= N; j ++) vist [J] = 0; ans + = find (I );} printf ("% d \ n", N-ans );}}


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.