Submission #52490
ソースコード
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 | #include <bits/stdc++.h> using namespace std; struct StronglyConnectedComponents { int size; int maxFri; vector<vector< int > > g; vector<vector< int > > rg; vector< int > memo; vector< bool > isVisited; vector< int > fri; void init ( int n ) { size = n; n += 5; g.resize(n); rg.resize(n); isVisited.resize(n, false ); fri.resize(n, 0); } void add ( int s, int t ) { g[s].push_back(t); rg[t].push_back(s); } void dfs ( int pos ) { isVisited[pos] = true ; for ( int i = 0; i < g[pos].size(); i++ ) { if ( !isVisited[g[pos][i]] ) dfs ( g[pos][i] ); } memo.push_back(pos); } void rdfs ( int pos, int t ) { isVisited[pos] = true ; fri[pos] = t; for ( int i = 0; i < rg[pos].size(); i++ ) { if ( !isVisited[rg[pos][i]] ) rdfs ( rg[pos][i], t ); } } void build ( ) { for ( int i = 0; i < size; i++ ) { if ( !isVisited[i] ) dfs ( i ); } fill ( isVisited.begin(), isVisited.end(), false ); int t = 0; reverse ( memo.begin(), memo.end() ); for ( int i = 0; i < memo.size(); i++ ) { if ( !isVisited[memo[i]] ) rdfs ( memo[i], t++ ); } maxFri = t; } const int operator[] ( int index ) const { return fri[index]; } bool isConnected ( int a, int b ) { return fri[a] == fri[b]; } }; StronglyConnectedComponents scc; signed main ( ) { int n, m; scanf ( "%d %d" , &n, &m); scc.init(n); for ( int i = 0; i < m; i++ ) { int s, t; scanf ( "%d %d" , &s, &t); scc.add(s, t); } scc.build(); vector< int > in(scc.maxFri); vector< int > out(scc.maxFri); for ( int i = 0; i < n; i++ ) { for ( int J = 0; J < scc.g[i].size(); J++ ) { int j = scc.g[i][J]; int s = scc[i]; int t = scc[j]; if ( !scc.isConnected(i, j) ) { in[t]++; out[s]++; } } } int IN = 0; int OUT = 0; if ( scc.maxFri == 1 ) { puts ( "0" ); return 0; } for ( int i = 0; i < scc.maxFri; i++ ) { if ( in[i] == 0 ) IN++; if ( out[i] == 0 ) OUT++; } cout << max ( IN, OUT ) << endl; } |
ステータス
項目 | データ |
---|---|
問題 | 0964 - 道路網改修 |
ユーザー名 | r1825 |
投稿日時 | 2019-08-05 21:33:01 |
言語 | C++ |
状態 | Accepted |
得点 | 14 |
ソースコード長 | 2517 Byte |
最大実行時間 | 50 ms |
最大メモリ使用量 | 3312 KB |
セット
セット | 得点 | Cases | |
---|---|---|---|
1 | ALL | 14 / 14 | * |
テストケース
ファイル名 | 状態 | 実行時間 | メモリ使用量 | # |
---|---|---|---|---|
in1.txt | AC | 23 ms | 476 KB |
1
|
in2.txt | AC | 26 ms | 684 KB |
1
|
in3.txt | AC | 22 ms | 520 KB |
1
|
in4.txt | AC | 19 ms | 612 KB |
1
|
in5.txt | AC | 20 ms | 452 KB |
1
|
in6.txt | AC | 29 ms | 1192 KB |
1
|
in7.txt | AC | 18 ms | 2292 KB |
1
|
in8.txt | AC | 21 ms | 1292 KB |
1
|
in9.txt | AC | 43 ms | 2804 KB |
1
|
in10.txt | AC | 30 ms | 2112 KB |
1
|
in11.txt | AC | 19 ms | 1856 KB |
1
|
in12.txt | AC | 25 ms | 2144 KB |
1
|
in13.txt | AC | 17 ms | 412 KB |
1
|
in14.txt | AC | 44 ms | 3208 KB |
1
|
in15.txt | AC | 50 ms | 2820 KB |
1
|
in16.txt | AC | 38 ms | 3184 KB |
1
|
in17.txt | AC | 33 ms | 3312 KB |
1
|
in18.txt | AC | 21 ms | 628 KB |
1
|
in19.txt | AC | 20 ms | 464 KB |
1
|
in20.txt | AC | 24 ms | 1672 KB |
1
|
in21.txt | AC | 17 ms | 508 KB |
1
|
in22.txt | AC | 16 ms | 864 KB |
1
|
in23.txt | AC | 24 ms | 972 KB |
1
|
in24.txt | AC | 17 ms | 1076 KB |
1
|
in25.txt | AC | 18 ms | 1188 KB |
1
|
in26.txt | AC | 23 ms | 1412 KB |
1
|
in27.txt | AC | 26 ms | 1628 KB |
1
|
in28.txt | AC | 28 ms | 2112 KB |
1
|
in29.txt | AC | 36 ms | 2704 KB |
1
|
in30.txt | AC | 39 ms | 3044 KB |
1
|