Poj_1236
TASKA is actually finding the number of strongly connected components whose input degree is 0, in fact, taskb is a larger ratio between the number of input degrees 0 and the number of outgoing degrees 0 in each strongly connected component.
At the same time, this question has different components from the previously made vertex dual-connectivity, one of them is to update the low [u] value for low [u]> dfn [v] when requesting a strongly connected component. One precondition is that V is in the stack, therefore, it is necessary to use a number group to identify whether V is in the stack. This array is not required for determining the vertex double-connected component, as long as G [u] [v] is 1, if dfn [v] is not 0, V1 is in the stack.
ForWhy do I not need to mark whether V is in the stack?Assume that V is not in the stack, but dfn [v] is not 0. If G [u] [v] is 1, then in the DFS process, either V should be searched behind u, then dfn [v] should obviously be 0, which is in conflict with assumptions, or V should be searched before u, then, because u is behind V, and V must have been out of the stack before it is out of the stack, and now u is indeed in the stack, this is also a contradiction (because the elements after the stack are not pushed into the stack), so for the process of finding the dual-connectivity component of a vertex, if G [u] [v] is 1 and dfn [v] is not equal to 0 in the DFS process, v1.
# Include < Stdio. h >
# Include < String . H >
Int Ans [ 110 ] [ 110 ], Indgr [ 110 ], Outdgr [ 110 ], G [ 110 ] [ 110 ];
Int INS [ 110 ], S [ 110 ], Top, num, N, dfn [ 110 ], Low [ 110 ], Time;
Void Tarjan ( Int U)
{
Int I, V;
Dfn [u] = Low [u] = ++ Time;
S [Top ++ ] = U;
INS [u] = 1 ;
For (V = 0 ; V < N; V ++ )
If (G [u] [v])
{
If ( ! Dfn [v])
{
Tarjan (v );
If (Low [v] < Low [u])
Low [u] = Low [v];
}
Else If (INS [v] && Dfn [v] < Low [u])
Low [u] = Dfn [v];
}
If (Dfn [u] = Low [u])
{
S [Top] =- 1 ;
For (I = 0 ; S [Top] ! = U; I ++ )
{
Ans [num] [I] = S [ -- Top];
INS [ans [num] [I] = 0 ;
}
Num ++ ;
}
}
Int Main ()
{
Int I, J, K, taskb, p, q, TASKA, OK, U;
While (Scanf ( " % D " , & N) = 1 )
{
For (I = 0 ; I < N; I ++ )
While ( 1 )
{
Scanf ( " % D " , & J );
If (J = 0 )
Break ;
J -- ;
G [I] [J] = 1 ;
}
Memset (ANS, - 1 , Sizeof (ANS ));
Memset (INS, 0 , Sizeof (INS ));
Memset (dfn, 0 , Sizeof (Dfn ));
Time = Num = Top = 0 ;
For (U = 0 ; U < N; u ++ )
If ( ! Dfn [u])
Tarjan (U );
If (Num = 1 )
{
Printf ( " 1 \ N0 \ n " );
Continue ;
}
Memset (indgr, 0 , Sizeof (Indgr ));
Memset (outdgr, 0 , Sizeof (Outdgr ));
For (I = 0 ; I < Num; I ++ )
For (J = 0 ; J < Num; j ++ )
{
If (I = J)
Continue ;
OK = 0 ;
For (P = 0 ; Ans [I] [p] ! =- 1 ; P ++ )
{
For (Q = 0 ; Ans [J] [Q] ! =- 1 ; Q ++ )
If (G [ans [I] [p] [ans [J] [Q])
{
Indgr [J] ++ ;
Outdgr [I] ++ ;
OK = 1 ;
Break ;
}
If (OK)
Break ;
}
}
Taskb = TASKA = 0 ;
For (I = 0 ; I < Num; I ++ )
{
If (Indgr [I] = 0 )
TASKA ++ ;
If (Outdgr [I] = 0 )
Taskb ++ ;
}
Taskb = Taskb > TASKA ? Taskb: TASKA;
Printf ( " % D \ n " , TASKA, taskb );
}
Return 0 ;< BR >}