比赛 |
202110省实验桐柏一中普及组联赛 |
评测结果 |
AAAAAAAAAA |
题目名称 |
分配同桌 |
最终得分 |
100 |
用户昵称 |
yrtiop |
运行时间 |
0.029 s |
代码语言 |
C++ |
内存使用 |
1.85 MiB |
提交时间 |
2021-10-18 20:06:07 |
显示代码纯文本
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
const int maxn = 100005;
int n,m,s,t;
int head[maxn],ver[maxn << 1],nxt[maxn << 1],cap[maxn << 1],cnt = -1;
queue<int> q;
void add(int u,int v,int t) {
ver[++ cnt] = v;
nxt[cnt] = head[u];
head[u] = cnt;
cap[cnt] = t;
return ;
}
int level[maxn],cur[maxn];
bool vis[maxn];
bool bfs(int s,int t) {
memset(level , 0 , sizeof(level));
while(!q.empty())q.pop();
q.push(s);
level[s] = 1;
while(!q.empty()) {
int u = q.front();
q.pop();
for(int i = head[u];~ i;i = nxt[i]) {
int v = ver[i];
if(!level[v]&&cap[i]) {
level[v] = level[u] + 1;
q.push(v);
}
}
}
return level[t] != 0;
}
int dfs(int x,int t,int maxflow) {
if(x == t||!maxflow)return maxflow;
int f,flow = 0,rest = maxflow;
for(int& i = cur[x];~ i;i = nxt[i]) {
int v = ver[i];
if(level[v] == level[x] + 1&&(f = dfs(v , t , min(cap[i] , maxflow)))) {
if(!f) {
level[v] = 0;
}
flow += f;
maxflow -= f;
cap[i] -= f;
cap[i ^ 1] += f;
if(!maxflow)break ;
}
}
return flow;
}
int Dinic(int s,int t) {
int flow = 0;
while(bfs(s , t)) {
memcpy(cur , head , sizeof(head));
flow += dfs(s , t , 0x3f3f3f3f);
}
return flow;
}
int main() {
freopen("tongzhuo.in","r",stdin);
freopen("tongzhuo.out","w",stdout);
memset(head , -1 , sizeof(head));
scanf("%d%d",&n,&m);
int u,v;
while(~ scanf("%d%d",&u,&v)) {
add(u , v , 1);
add(v , u , 0);
}
s = ++ n;
t = ++ n;
for(int i = 1;i <= m;++ i) {
add(s , i , 1);
add(i , s , 0);
}
for(int i = m + 1;i <= n - 2;++ i) {
add(i , t , 1);
add(t , i , 0);
}
printf("%d\n",Dinic(s , t));
fclose(stdin);
fclose(stdout);
return 0;
}