【HDOJ】3277 Marriage Match III

时间:2023-01-14 17:50:36

Dinic不同实现的效率果然不同啊。

 /* 3277 */
#include <iostream>
#include <string>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
#include <deque>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <ctime>
#include <cstring>
#include <climits>
#include <cctype>
#include <cassert>
#include <functional>
#include <iterator>
#include <iomanip>
using namespace std;
//#pragma comment(linker,"/STACK:102400000,1024000") #define sti set<int>
#define stpii set<pair<int, int> >
#define mpii map<int,int>
#define vi vector<int>
#define pii pair<int,int>
#define vpii vector<pair<int,int> >
#define rep(i, a, n) for (int i=a;i<n;++i)
#define per(i, a, n) for (int i=n-1;i>=a;--i)
#define clr clear
#define pb push_back
#define mp make_pair
#define fir first
#define sec second
#define all(x) (x).begin(),(x).end()
#define SZ(x) ((int)(x).size())
#define lson l, mid, rt<<1
#define rson mid+1, r, rt<<1|1 const int INF = 0x1f1f1f1f;
const int maxn = ;
const int maxm = maxn*maxn;
const int maxv = maxn*;
const int maxe = maxv*maxv*;
bool M[maxn][maxn];
int V[maxe], F[maxe], F_[maxe], nxt[maxe];
int dis[maxv], head[maxv], head_[maxv], Q[maxv];
int pre[maxn];
int X[maxm], Y[maxm];
int s, t;
int n, m; void addEdge(int u, int v, int c) {
#ifndef ONLINE_JUDGE
printf("u = %d, v = %d, c = %d\n", u, v, c);
#endif
V[m] = v;
F_[m] = c;
nxt[m] = head[u];
head[u] = m++; V[m] = u;
F_[m] = ;
nxt[m] = head[v];
head[v] = m++;
} int find(int x) {
if (x == pre[x])
return x;
return pre[x] = find(pre[x]);
} void merge(int x, int y) {
int fx = find(x);
int fy = find(y); if (fx != fy)
pre[fx] = fy;
} bool bfs() {
int l = , r = ;
int u, v, k; memset(dis, , sizeof(dis));
Q[r++] = s;
dis[s] = ; while (l < r) {
u = Q[l++];
for (k=head[u]; k!=-; k=nxt[k]) {
v = V[k];
if (!dis[v] && F[k]) {
dis[v] = dis[u] + ;
if (v == t)
return false;
Q[r++] = v;
}
}
} return true;
} int dfs(int u, int val) {
if (u==t || val==)
return val; int ret = ;
int tmp, v; for (int& k=head_[u]; k!=-; k=nxt[k]) {
v = V[k];
if (F[k] && dis[v]==dis[u]+ && (tmp=dfs(v, min(val, F[k])))>) {
F[k] -= tmp;
F[k^] += tmp;
ret += tmp;
val -= tmp;
if (val == )
break;
}
} return ret;
} int Dinic() {
int ret = , tmp; while () {
if (bfs())
break; memcpy(head_, head, sizeof(head));
while () {
tmp = dfs(s, INF);
if (tmp == )
break;
ret += tmp;
}
} return ret;
} int main() {
ios::sync_with_stdio(false);
#ifndef ONLINE_JUDGE
freopen("data.in", "r", stdin);
freopen("data.out", "w", stdout);
#endif int tt;
int c, K, f;
int x, y;
int l, r, mid;
int ans, tmp; scanf("%d", &tt);
while (tt--) {
scanf("%d %d %d %d", &n, &c, &K, &f);
rep(i, , c)
scanf("%d %d", &X[i], &Y[i]);
rep(i, , n+)
pre[i] = i;
while (f--) {
scanf("%d %d", &x, &y);
merge(x, y);
} m = ;
s = *n+;
t = s + ;
memset(head, -, sizeof(head));
memset(M, false, sizeof(M)); rep(i, , n+) {
find(i);
addEdge(s, i, );
addEdge(i+n*, t, );
}
rep(i, , n+)
addEdge(i, i+n, K); rep(i, , c) {
x = X[i];
y = Y[i];
rep(j, , n+) {
if (pre[x]==pre[j] && !M[j][y]) {
M[j][y] = true;
addEdge(j, y+n*, );
}
}
} rep(i, , n+) {
rep(j, , n+) {
if (!M[i][j]) {
addEdge(i+n, j+n*, );
}
}
} l = ;
r = n;
ans = ; while (l <= r) {
mid = (l + r) >> ;
for (int i=; i<*n; i+=) {
F[i] = F[i+] = mid;
F[i+] = F[i+] = ;
}
rep(i, *n, m)
F[i] = F_[i];
tmp = Dinic();
#ifndef ONLINE_JUDGE
printf("tmp = %d\n", tmp);
#endif
if (tmp >= mid*n) {
ans = mid;
l = mid + ;
} else {
r = mid - ;
}
} printf("%d\n", ans);
} #ifndef ONLINE_JUDGE
printf("time = %d.\n", (int)clock());
#endif return ;
}