1. 程式人生 > >今日頭條”杯2018年湖北省賽網)

今日頭條”杯2018年湖北省賽網)

algo 分享 ec2 disable 是不是 line space () view

所有題目: http://cdn.vo-ov.cn/online_f9ec217.pdf

F: A-maze-ing

哇我也是哭了...dfs寫錯,dfs還用了vis數組,實際上並不需要,WA了N多次...嗚嗚嗚

看出來對圖的基本概念還比較生疏,或者說都忘了好多,一開始還在糾結環是不是強連通分量...唉如果能果斷點就好了

技術分享圖片
  1 #include <iostream>
  2 #include <string.h>
  3 #include <cstdio>
  4 #include <queue>
  5 #include <set>
  6
#include <cstring> 7 #include <algorithm> 8 9 #define SIGMA_SIZE 26 10 #pragma warning ( disable : 4996 ) 11 12 using namespace std; 13 typedef long long LL; 14 15 inline LL LMax(LL a,LL b) { return a>b?a:b; } 16 inline LL LMin(LL a,LL b) { return a>b?b:a; } 17
inline int Max(int a,int b) { return a>b?a:b; } 18 inline int Min(int a,int b) { return a>b?b:a; } 19 inline int gcd( int a, int b ) { return b==0?a:gcd(b,a%b); } 20 inline int lcm( int a, int b ) { return a/gcd(a,b)*b; } //a*b = gcd*lcm 21 const int inf = 0x3f3f3f3f; 22 const int mod = 7;
23 const int maxn = 2e5+5; 24 const int maxk = 200; 25 26 struct edge { 27 int to, next; 28 }edges[maxn]; 29 30 struct edg{ 31 int to, next; 32 }e[maxn]; 33 34 int N, M, cnt, sum; 35 int order, iindex; 36 int linjie[maxn], linjie_2[maxn], indeg[maxn], LOW[maxn]; 37 int DFN[maxn], belong[maxn], Stack[maxn]; 38 int num[maxn]; 39 bool vis[maxn]; 40 41 42 void addedge( int u, int v ) 43 { 44 edges[cnt].to = v; edges[cnt].next = linjie[u]; linjie[u] = cnt++; } 45 46 void addedge_2( int u, int v ) 47 { 48 e[cnt].to = v; e[cnt].next = linjie_2[u]; linjie_2[u] = cnt++; } 49 50 void init() 51 { 52 cnt = order = iindex = sum = 0; 53 memset( linjie, -1, sizeof(linjie) ); 54 memset( linjie_2, -1, sizeof(linjie_2) ); 55 memset( indeg, 0, sizeof(indeg) ); 56 memset( LOW, 0, sizeof(LOW) ); 57 memset( DFN, 0, sizeof(DFN) ); 58 memset( belong, 0, sizeof(belong) ); 59 memset( Stack, 0, sizeof(Stack) ); 60 memset( num, 0, sizeof(num) ); 61 memset( vis, 0, sizeof(vis) ); 62 } 63 64 void tarjan( int x ) 65 { 66 DFN[x] = LOW[x] = ++order; 67 Stack[++iindex] = x; 68 vis[x] = true; 69 for( int i = linjie[x]; i+1; i = edges[i].next ) 70 { 71 if (!DFN[edges[i].to]) 72 { 73 tarjan(edges[i].to); 74 LOW[x] = Min( LOW[x], LOW[edges[i].to] ); 75 } 76 else if ( vis[edges[i].to] ) 77 LOW[x] = Min( LOW[x], DFN[edges[i].to] ); 78 } 79 80 if ( LOW[x] == DFN[x] ) 81 { 82 sum++; 83 while( iindex ) 84 { 85 86 int temp = Stack[iindex--]; 87 vis[temp] = false; 88 belong[temp] = sum; 89 if ( temp == x ) 90 break; 91 } 92 } 93 } 94 95 void solve() 96 { 97 cnt = 0; 98 99 for ( int i = 1; i <= N; i++ ) 100 num[belong[i]]++; 101 102 for ( int i = 1; i <= N; i++ ) 103 for ( int j = linjie[i]; j+1; j = edges[j].next ) 104 if ( belong[i] != belong[edges[j].to] ) 105 { 106 indeg[belong[edges[j].to]] = 1; 107 addedge_2(belong[i], belong[edges[j].to]); 108 } 109 } 110 111 void read() 112 { 113 int x, y; 114 for ( int i = 1; i <= M; i++ ) 115 { 116 scanf("%d %d", &x, &y); 117 addedge(x, y); 118 } 119 } 120 121 void find( int x ) 122 { 123 if ( linjie_2[x] == -1 ) 124 { 125 belong[x] = num[x]; 126 return; 127 } 128 129 130 for ( int i = linjie_2[x]; i+1; i = e[i].next ) 131 { 132 if (!belong[e[i].to]) 133 find(e[i].to); 134 belong[x] = Max( belong[x], belong[e[i].to]+num[x] ); 135 } 136 137 return; 138 } 139 140 int main() 141 { 142 while (~scanf("%d %d", &N, &M)) 143 { 144 init(); 145 read(); 146 147 for (int i = 1; i <= N; i++) 148 if (!DFN[i]) 149 tarjan(i); 150 solve(); 151 152 int ans = 0; 153 memset( belong, 0, sizeof(belong) ); 154 155 for (int i = 1; i <= sum; i++) 156 if (!indeg[i]) 157 { 158 find(i); 159 ans = Max(ans, belong[i]); 160 } 161 162 printf("%d\n", ans); 163 } 164 return 0; 165 }
View Code

今日頭條”杯2018年湖北省賽網)