Submission #42488
ソースコード
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 | #include<iostream> #include<stack> #include<vector> #include<algorithm> using namespace std; const int INF=1<<30; vector< int >Graph[10005]; vector< int >rev_Graph[10005]; stack< int >order; int color[10005]; bool reached[10005]={ false }; void dfs( int node); void rdfs( int node, int num); int main(){ int n,m; cin>>n>>m; for ( int i=0;i<=n;i++){ color[i]=INF; } for ( int i=0;i<m;i++){ int from,to; cin>>from>>to; Graph[from].push_back(to); rev_Graph[to].push_back(from); } for ( int i=0;i<n;i++){ dfs(i); } int cnt=0; while (!order.empty()){ int node=order.top();order.pop(); if (color[node]==INF){ rdfs(node,cnt); cnt++; } } bool flag= false ; for ( int i=0;i<n&&!flag;i++){ if (color[i]!=0){ flag= true ; } } if (!flag){ cout<<0<<endl; return (0); } int in_cnt[10005]={0}; int out_cnt[10005]={0}; for ( int i=0;i<n;i++){ for ( int j=0;j<Graph[i].size();j++){ int next=Graph[i][j]; if (color[i]!=color[next]){ in_cnt[color[next]]++; out_cnt[color[i]]++; } } } int in_zero=0; int out_zero=0; for ( int i=0;i<cnt;i++){ if (in_cnt[i]==0){ in_zero++; } if (out_cnt[i]==0){ out_zero++; } } cout<<max(in_zero,out_zero)<<endl; return (0); } void dfs( int node){ if (reached[node]){ return ; } reached[node]= true ; for ( int i=0;i<Graph[node].size();i++){ dfs(Graph[node][i]); } order.push(node); return ; } void rdfs( int node, int num){ if (color[node]!=INF){ return ; } color[node]=num; for ( int i=0;i<rev_Graph[node].size();i++){ rdfs(rev_Graph[node][i],num); } return ; } |
ステータス
項目 | データ |
---|---|
問題 | 0964 - 道路網改修 |
ユーザー名 | ei1630 |
投稿日時 | 2018-08-31 12:05:58 |
言語 | C++11 |
状態 | Accepted |
得点 | 14 |
ソースコード長 | 1778 Byte |
最大実行時間 | 89 ms |
最大メモリ使用量 | 11500 KB |
セット
セット | 得点 | Cases | |
---|---|---|---|
1 | ALL | 14 / 14 | * |
テストケース
ファイル名 | 状態 | 実行時間 | メモリ使用量 | # |
---|---|---|---|---|
in1.txt | AC | 44 ms | 992 KB |
1
|
in2.txt | AC | 25 ms | 5644 KB |
1
|
in3.txt | AC | 26 ms | 7860 KB |
1
|
in4.txt | AC | 28 ms | 8812 KB |
1
|
in5.txt | AC | 30 ms | 8748 KB |
1
|
in6.txt | AC | 28 ms | 8876 KB |
1
|
in7.txt | AC | 61 ms | 9888 KB |
1
|
in8.txt | AC | 30 ms | 9068 KB |
1
|
in9.txt | AC | 62 ms | 11500 KB |
1
|
in10.txt | AC | 77 ms | 2728 KB |
1
|
in11.txt | AC | 65 ms | 2332 KB |
1
|
in12.txt | AC | 74 ms | 2620 KB |
1
|
in13.txt | AC | 19 ms | 1600 KB |
1
|
in14.txt | AC | 89 ms | 4552 KB |
1
|
in15.txt | AC | 75 ms | 5288 KB |
1
|
in16.txt | AC | 78 ms | 6608 KB |
1
|
in17.txt | AC | 61 ms | 7668 KB |
1
|
in18.txt | AC | 31 ms | 5780 KB |
1
|
in19.txt | AC | 28 ms | 5716 KB |
1
|
in20.txt | AC | 36 ms | 6664 KB |
1
|
in21.txt | AC | 24 ms | 6036 KB |
1
|
in22.txt | AC | 27 ms | 6028 KB |
1
|
in23.txt | AC | 27 ms | 6208 KB |
1
|
in24.txt | AC | 31 ms | 6312 KB |
1
|
in25.txt | AC | 26 ms | 6588 KB |
1
|
in26.txt | AC | 27 ms | 6800 KB |
1
|
in27.txt | AC | 34 ms | 7196 KB |
1
|
in28.txt | AC | 42 ms | 7800 KB |
1
|
in29.txt | AC | 54 ms | 9356 KB |
1
|
in30.txt | AC | 65 ms | 10608 KB |
1
|