-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path11504.cpp
More file actions
135 lines (129 loc) · 2.66 KB
/
Copy path11504.cpp
File metadata and controls
135 lines (129 loc) · 2.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <bitset>
#include <queue>
using namespace std;
#define MAX 100001
#define UNVISITED -1
int n,m;
// queue< int > cicle;
vector< vector< int > > graph,graph2;
int dfs_num[ MAX ], dfs_low[ MAX ],dfsNum,numSCC;
bitset< MAX > visited,vv;
vector< int > S;
bitset< MAX > topo;
int otherGraph[ MAX ];
void bfs( int u )
{
queue< int > q;
q.push( u );
vv[ u ] = 1;
int now,next;
while( !q.empty() )
{
now = q.front(); q.pop();
for( int i = 0; i < graph2[ now ].size() ; ++i )
{
next = graph2[ now ][ i ];
if( !vv[ next ] )
{
vv[ next ] = 1;
q.push( next );
}
}
}
}
void tarjanSCC( int u )
{
dfs_num[ u ] = dfs_low[ u ] = dfsNum++;
visited[ u ] = 1;
S.push_back( u );
for( int i = 0 ; i < graph[ u ].size() ; ++i )
{
int v = graph[ u ][ i ];
if( dfs_num[ v ] == UNVISITED )
tarjanSCC( v );
if( visited[ v ] )
dfs_low[ u ] = min( dfs_low[ u ] , dfs_low[ v ] );
}
if( dfs_num[ u ] == dfs_low[ u ] )
{
// int cont = 0;
while ( 1 ) {
// ++cont;
int v = S.back() ; S.pop_back();
otherGraph[ v ] = numSCC;
visited[ v ] = 0;
if( u == v ) break;
}
// if( cont > 1 )
// cicle.push( numSCC );
++numSCC;
}
}
int main()
{
int t;
int a,b;
scanf("%d\n",&t );
while( t-- )
{
scanf("%d %d",&n,&m);
graph.assign( n , vector< int >() );
S.clear();
visited.reset();
memset( dfs_num , UNVISITED , sizeof dfs_num );
memset( dfs_low , 0 , sizeof dfs_low );
memset( otherGraph , -1 , sizeof otherGraph );
dfsNum = numSCC = 0;
topo.reset();
vv.reset();
for( int i = 0; i < m ;++i )
{
scanf("%d %d",&a,&b);
--a;
--b;
graph[ a ].push_back( b );
}
for( int i = 0; i < n ; ++i )
if( dfs_num[ i ] == UNVISITED )
tarjanSCC( i );
graph2.assign( numSCC , vector< int > () );
for( int i = 0; i < n ; ++i )
{
a = otherGraph[ i ];
for( int j = 0 ; j < graph[ i ].size() ; ++j )
{
b = otherGraph[ graph[i][j] ];
if( a != b )
{
graph2[ a ].push_back( b );
topo[ b ] = 1;
}
}
}
int top = 0;
for( int i = 0 ; i < numSCC ; ++i )
if( !topo[ i ] )
{
++top;
bfs( i );
}
// while ( !cicle.empty() ) {
// if( !vv[ cicle.front() ] )
// {
//
// bfs( cicle.front() );
// ++top;
// }
// cicle.pop();
// }
// for( int i = 0 ; i < numSCC ; ++i )
// if( !vv[ i ] )
// ++top;
printf("%d\n", top );
}
return 0;
}