-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path11383.cpp
71 lines (64 loc) · 1.38 KB
/
11383.cpp
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
#include <iostream>
#include <cstdio>
#include <cstring>
#include <bitset>
#include <vector>
using namespace std;
#define MAX 2005
#define UNVISITED -1
int dfsNum,dfs_low[ MAX ],dfs_num[ MAX ],SCCnum;
vector< vector< int > > graph;
vector< int > S;
bitset< MAX > visited;
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_low[ u ] == dfs_num[ u ] )
{
++SCCnum;
while( 1 )
{
int v = S.back(); s.pop_back();
visited[ v ] = 0;
if( v == u ) break;
}
}
}
int main()
{
int n,m;
int a,b,o;
while( scanf("%d %d",&n,&m) , (n || m ) )
{
memset( dfs_num , UNVISITED , sizeof dfs_num );
memset( dfs_low , 0 , sizeof dfs_low );
visited.reset();
S.clear();
dfsNum = SCCnum = 0;
graph.assing( n , vector< int > () );
for( int i = 0 ; i < m ;++i )
{
scanf("%d %d %d",&a,&b,&o);
--a;
--b;
graph[ a ].push_back( b );
if( o == 2 )
graph[ b ].push_back( a );
}
for( int i = 0 ; i < n ; ++i )
if( dfs_num[ i ] == UNVISITED )
tarjanSCC( i );
printf("%d\n",(SCCnum==1) );
}
return 0;
}