DescriptionDescription
A one-way railway line numbered 1, 2 ,..., N train stations. Each railway station has a level, with a minimum level of 1. There are several train trips traveling on this line, each of which meets the following requirements: if this train stops at the railway station X, all levels between the origin station and terminal station must be docked if they are greater than or equal to the Railway Station X. (Note: The start station and the terminal station are also known as sites to be docked in advance)
For example, the following table shows the running status of five trains. Among them, the first four trains met the requirements, while the first four trains were docked at the No. 6 railway station (level 2) but not through the bus (level 2) but does not meet the requirements.
The current running status of M trains (all meeting the requirements) is estimated that the N train stations are divided into at least several different levels.
Idea: I wanted to find confidence through the popular group's questions, but it took me two hours to get it... Really drunk... After reading the questions, I thought of the topological sorting. I connected the site that did not appear in the train departure station and the terminal station to the site that appeared with a directed edge, you can find several layers of Topology Sorting. However, there is a problem here, that is, the reading of O (Mn ^ 2) and a large amount of computing, so the reading is slightly reduced by some loop range, the read complexity of the worst O (Mn ^ 2/4) is reached. In the topological sorting, there is also a small optimization. The point with an inbound degree of 0 for each layer is generated when the last layer deletes the edge, therefore, when deleting an edge, you can add the vertex to the next layer to reduce the time complexity.
Code:
# Include <iostream>
# Include <cstdio>
# Include <cstring>
Using namespace STD;
Struct use {
Int num [1001], L;
} UC, UCC;
Bool map [1001] [1001] = {false };
Int R [1001] = {0}, Bian [1001] [1001] = {0 };
Void work ()
{
Int tot = 0, n, m, I, J, K, Q, X, ANS = 0;
Scanf ("% d", & N, & M );
For (I = 1; I <= m; ++ I)
{
Scanf ("% d", & Q );
UC. L = Q; UCC. L = 0;
Scanf ("% d", & UC. Num [1]);
For (j = 2; j <= Q; ++ J)
{
Scanf ("% d", & UC. Num [J]);
For (k = UC. Num [J-1] + 1; k <UC. Num [J]; ++ K)
{
++ UCC. L; UCC. Num [UCC. L] = K;
}
}
For (j = 1; j <= UC. L; ++ J)
For (k = 1; k <= UCC. L; ++ K)
{
If (! Map [UCC. Num [k] [uc. Num [J])
{
Map [UCC. Num [k] [uc. Num [J] = true;
++ Bian [UCC. Num [k] [0];
Bian [UCC. Num [k] [Bian [UCC. Num [k] [0] = UC. Num [J];
++ R [uc. Num [J];
}
}
}
UC. L = 0;
For (I = 1; I <= N; ++ I)
If (R [I] = 0)
{
++ UC. L;
UC. Num [uc. L] = I;
}
TOT = UC. L;
Ans = 1;
While (TOT <n)
{
UCC. L = 0;
For (I = 1; I <= UC. L; ++ I)
For (j = 1; j <= Bian [uc. Num [I] [0]; ++ J)
{
-- R [Bian [uc. Num [I] [J];
If (R [Bian [uc. Num [I] [J] = 0)
{
++ UCC. L;
UCC. Num [UCC. L] = Bian [uc. Num [I] [J];
}
}
UC = UCC;
TOT = tot + UC. L;
++ Ans;
}
Cout <ans <Endl;
}
Int main ()
{
Work ();
}
Codevs3294 station classification noip universal group T4