cogs 1001. [WZOI2011 S3] Messaging Tarjan

1001. [WZOI2011 S3] Messaging

★★ Input file: messagew.in   Output file: messagew.out   a simple comparison of
the time limit: 1 s memory limit: 128 MB

Problem 2 Messaging (messagew.pas / c / cpp)

 

Problem Description

 

WZland opened a club (which it can do any thing), which attracted many people to join. The number of the club's more and more, relations have become increasingly complex ......

 

Club people from various places, in order to increase the friendship, the club held a party. The party has conducted a messenger game, if A know B, then A receives a message, it will put this message to B, A and all those who know (if A know B, B may not know A), everyone numbered from 1 to N.

 

Now given all "know" relationship, the person in charge of the club WZland king wanted to know a very simple question: If A post a new message, it will not be sent a message after several passes, this message back to the A, 1 ≤A≤N. But WZland king is out of the name of poor math, but fortunately you are on his side, and he will give you to solve this problem.




Input Format

 

The first line of input data number N is two and M, there is a space between the two numbers, and the number represents the number of recognized relationship.

 

The next M rows, each row two numbers A and B, A represents appreciated B (1? A, B? N, A? B).

 

Output Format

 

Output file a total of N lines of a character "T" or "F". If the i-th row is "T", i represents a new message will be sent back to the i; if it is "F", i represents a new message will not be sent back to i.

 

Sample input and output

 

messagwe.in 

 

4 6

 

1 2

 

2 3

 

4 1

 

3 1

 

1 3

 

2 3

 

messagew.out

 

T

 

T

 

T

 

F




Scale data

 

For 30% of the data, N≤1000, M≤20000;

 

For 50% of the data, N≤10000, M≤100000;

 

To 100% of the data, N≤100000, M≤200000;

 

Understanding of the relationship may be repeated given.

 

time limit

 

1s

 

This is actually quite simple questions is a Tarjan it
should then be how to deal with it? ? The size of the strongly connected component of the point where as long as the current> 1 is equivalent to this point later came to pass can someone give him back! else is certainly not one of his own friends

As for the understanding of the relationship may be repeated. . . I did not start to think of how to deal with cross eyes closed up A later actually want to understand (or board clever ah) quick look quick look:

The line 67 to see if the point is not already in the stack and put the current point to the low point of dfn take a smaller value will not be returned directly out of the bug

 

(It << voices: Tsinghua orz board is much like this one!)

Let's put clear and concise (think) bar code

#include <Vector> 
#include <cstdio> 
#include <the iostream> 
#include <algorithm> 
#include <the cmath>
 #define MAXN 100005
 the using  namespace STD;
 int n-, m; 
Vector < int > V [MAXN];
 int DFN [ MAXN], Low [MAXN], size [MAXN], scc_cnt, ST [MAXN], CNT, Tim, belong [MAXN];
 BOOL Bein [MAXN];
 void Tarjan ( int RT) 
{ 
    DFN [RT] = Low [RT ] = Tim ++; // timestamp equal ++ initialization dfn and low 
    ST [CNT +] = RT; // this point stack 
    bein [rt] =to true ; // flag is in the stack array 
    for ( int I = 0 ; I <V [RT] .size (); I ++ ) 
    { 
        int to = V [RT] [I];
         IF (! DFN [to] ) 
            Tarjan (to), Low [RT] = min (Low [RT], Low [to]);
         the else  IF (Bein [to]) 
            Low [RT] = min (Low [RT], DFN [to]); 
    } 
    IF (DFN [RT] == Low [RT]) 
    { 
        scc_cnt ++; // statistical strongly connected components 
        int K;
         do { 
            K =ST [CNT]; 
            CNT - ; 
            belong [K] = scc_cnt; 
            Bein [K] = to false ; 
            size [scc_cnt] ++ ; 
        } the while (K ^ RT); // ! ^ here equivalent = do while is then determined again Xianpao     
    } 
} 
int main () 
{ 
    The freopen ( " messagew.in " , " R & lt " , stdin); 
    The freopen ( " messagew.out " , " W " , stdout); 
    Scanf ( "% D% D " , & n-, & m);
     for ( int I = . 1 ; I <= m; I ++ ) 
    { 
        int X, Y; Scanf ( " % D% D " , & X, & Y); 
        V [X ] .push_back (Y); // this is a single side is to be noted ah 
    }    
     for ( int I = . 1 ; I <= n-; I ++ )
         IF (! DFN [I]) // If no access 
            Tarjan (i);
     for ( int I = . 1 ; I <= n-; I ++ ) 
    { 
        IF (size [belong [I]]> . 1)
            puts("T");
        else
            puts("F");
    }
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/Tidoblogs/p/11329587.html