bzoj 1779
较水的网络流。
1 /************************************************************** 2 Problem: 1779 3 User: idy002 4 Language: C++ 5 Result: Accepted 6 Time:36 ms 7 Memory:1520 kb 8 ****************************************************************/ 9 10 #include <cstdio> 11 #include <cstring> 12 #include <vector> 13 #include <queue> 14 #define maxn 4010 15 #define fprintf(...) 16 #define oo 0x3f3f3f3f 17 using namespace std; 18 19 20 struct Edge { 21 int u, v, f; 22 Edge( int u, int v, int f ):u(u),v(v),f(f){} 23 }; 24 struct Dinic { 25 int src, dst; 26 vector<Edge> edge; 27 vector<int> g[maxn]; 28 int dep[maxn], cur[maxn]; 29 30 void init( int src, int dst ) { 31 this->src = src; 32 this->dst = dst; 33 } 34 void adde( int u, int v, int f ) { 35 fprintf( stderr, "%d->%d %d\n", u, v, f ); 36 g[u].push_back( edge.size() ); 37 edge.push_back( Edge(u,v,f) ); 38 g[v].push_back( edge.size() ); 39 edge.push_back( Edge(v,u,0) ); 40 } 41 bool bfs() { 42 queue<int> qu; 43 memset( dep, 0, sizeof(dep) ); 44 qu.push( src ); 45 dep[src] = 1; 46 while( !qu.empty() ) { 47 int u=qu.front(); 48 qu.pop(); 49 for( int t=0; t<g[u].size(); t++ ) { 50 Edge &e = edge[g[u][t]]; 51 if( e.f && !dep[e.v] ) { 52 dep[e.v] = dep[e.u]+1; 53 qu.push( e.v ); 54 } 55 } 56 } 57 return dep[dst]; 58 } 59 int dfs( int u, int a ) { 60 if( u==dst || a==0 ) return a; 61 int remain=a, past=0, na; 62 for( int &t=cur[u]; t<g[u].size(); t++ ) { 63 Edge &e = edge[g[u][t]]; 64 Edge &ve = edge[g[u][t]^1]; 65 if( dep[e.v]==dep[e.u]+1 && e.f && (na=dfs(e.v,min(e.f,remain))) ) { 66 remain -= na; 67 past += na; 68 e.f -= na; 69 ve.f += na; 70 if( remain==0 ) break; 71 } 72 } 73 return past; 74 } 75 int maxflow() { 76 int flow=0; 77 while( bfs() ) { 78 memset( cur, 0, sizeof(cur) ); 79 flow += dfs(src,oo); 80 } 81 return flow; 82 } 83 }D; 84 85 int n, m; 86 vector<int> g[maxn]; 87 int cnt[maxn]; 88 char stat[maxn]; 89 int idj[maxn], idt[maxn], ide[maxn][2]; 90 91 void makeid( int &src, int &dst ) { 92 int id_clock = 0; 93 src = ++id_clock; 94 dst = ++id_clock; 95 fprintf( stderr, "src=%d dst=%d\n", src, dst ); 96 for( int i=1; i<=n; i++ ) { 97 if( stat[i]=='J' ) { 98 idj[i] = ++id_clock; 99 fprintf( stderr, "idj[%d] = %d\n", i, idj[i] ); 100 } else if( stat[i]=='T' ) { 101 idt[i] = ++id_clock; 102 fprintf( stderr, "idt[%d] = %d\n", i, idt[i] ); 103 } 104 if( stat[i]!='T' ) { 105 ide[i][0] = ++id_clock; 106 ide[i][1] = ++id_clock; 107 fprintf( stderr, "ide[%d][0] = %d\n", i, ide[i][0] ); 108 fprintf( stderr, "ide[%d][1] = %d\n", i, ide[i][1] ); 109 } 110 } 111 } 112 int main() { 113 scanf( "%d%d", &n, &m ); 114 scanf( "%s", stat+1 ); 115 for( int i=1; i<=n; i++ ) 116 cnt[i] = cnt[i-1]+(stat[i]=='E'); 117 for( int i=1,u,v; i<=m; i++ ) { 118 scanf( "%d%d", &u, &v ); 119 g[u].push_back(v); 120 g[v].push_back(u); 121 } 122 int src, dst; 123 makeid( src, dst ); 124 D.init( src, dst ); 125 for( int i=1; i<=n; i++ ) { 126 if( stat[i]=='J' ) { 127 D.adde( src, idj[i], 1 ); 128 D.adde( idj[i], ide[i][0], 1 ); 129 for( int t=0; t<g[i].size(); t++ ) { 130 int j=g[i][t]; 131 if( stat[j]=='T' ) continue; 132 D.adde( idj[i], ide[j][0], 1 ); 133 } 134 } else if( stat[i]=='T' ) { 135 D.adde( idt[i], dst, 1 ); 136 for( int t=0; t<g[i].size(); t++ ) { 137 int j=g[i][t]; 138 if( stat[j]=='T' ) continue; 139 D.adde( ide[j][1], idt[i], 1 ); 140 } 141 } 142 if( stat[i]!='T' ) { 143 D.adde( ide[i][0], ide[i][1], 1 ); 144 } 145 } 146 printf( "%d\n", D.maxflow() ); 147 } 148 149