本文主要是介绍1423. 魔王bug的2色定理,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
转换为求最小割
#include<iostream>
#include<cstdio>#include<cstring>
#include<vector>
#include<queue>
using namespace std;
const int INF = 10000000;
vector<int> path[220];
int cap[220][220];
int flow[220][220];
int a[220];
int p[220];
int n,m;
void solve(){
int s = 0;
int t = n+1;
int ans = 0;
queue<int> q;
memset(flow, 0, sizeof(flow));
for(;;) {
memset(a, 0, sizeof(a));
a[s] = INF;
q.push(s);
while(!q.empty()) {
int u = q.front(); q.pop();
for(int i = 0; i<path[u].size(); i++) if(!a[path[u][i]] && cap[u][path[u][i]]>flow[u][path[u][i]]){
p[path[u][i]] = u; q.push(path[u][i]);
a[path[u][i]] = a[u] < cap[u][path[u][i]]-flow[u][path[u][i]]?a[u]:cap[u][path[u][i]]-flow[u][path[u][i]];
}
}
if(a[t] == 0) break;
for(int u = t; u != s; u = p[u]) {
flow[p[u]][u] += a[t];
flow[u][p[u]] -= a[t];
}
ans += a[t];
}
printf("%d\n", ans);
///return 0;
}
int main()
{
while(scanf("%d%d",&n,&m)!=EOF){
for(int i = 0;i<=n+1;i++)path[i].clear();
memset(cap,0,sizeof(cap));
int a,b;
for(int i = 0;i<m;i++){
scanf("%d%d",&a,&b);
path[a].push_back(b);
path[b].push_back(a);
cap[a][b] = cap[b][a] = 1;
}
scanf("%d",&a);
for(int i = 0;i<a;i++){
scanf("%d",&b);
path[0].push_back(b);
path[b].push_back(0);
cap[0][b] = INF;
}
scanf("%d",&a);
for(int i = 0;i<a;i++){
scanf("%d",&b);
path[n+1].push_back(b);
cap[b][n+1] = INF;
path[b].push_back(n+1);
}
solve();
}
return 0;
}
这篇关于1423. 魔王bug的2色定理的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!