比赛 |
202110省实验桐柏一中普及组联赛 |
评测结果 |
AAAAAAAAAA |
题目名称 |
分配同桌 |
最终得分 |
100 |
用户昵称 |
ydtz |
运行时间 |
0.367 s |
代码语言 |
C++ |
内存使用 |
51.64 MiB |
提交时间 |
2021-10-18 19:07:35 |
显示代码纯文本
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define N 10005
#define M 4000005
int n,m,x,y,head[N],tot=1,cur[N],dep[N],s,t,maxflow;
bool vis[N];
struct node{
int to,next,w;
node (int to=0,int next=0,int w=0)
:to(to),next(next),w(w){}
}e[M];
ll read(){
ll w=0,f=1;
char ch=getchar();
while (ch>'9'||ch<'0'){
if (ch=='-') f=-1;
ch=getchar();
}
while (ch>='0'&&ch<='9'){
w=(w<<3)+(w<<1)+(ch^48);
ch=getchar();
}
return w*f;
}
void adde(int u,int v,int w){
e[++tot]=node(v,head[u],w);
head[u]=tot;
}
bool bfs(){
for (int i=1;i<=n+2;i++) cur[i]=head[i],vis[i]=0,dep[i]=1<<30;
dep[s]=0;
queue<int> q;
q.push(s);
while (!q.empty()){
int u=q.front();
q.pop();
vis[u]=0;
for (int i=head[u];i;i=e[i].next){
int v=e[i].to;
if (dep[v]>dep[u]+1&&e[i].w){
dep[v]=dep[u]+1;
if (!vis[v]){
q.push(v);
vis[v]=1;
}
}
}
}
if (dep[t]!=1<<30) return 1;
return 0;
}
int dfs(int u,int flow){
ll rlow=0;
if (u==t){
maxflow+=flow;
return flow;
}
int used=0;
for (int i=cur[u];i;i=e[i].next){
cur[u]=i;
int v=e[i].to;
if (e[i].w&&dep[v]==dep[u]+1){
if (rlow=dfs(v,min(flow-used,e[i].w))){
used+=rlow;
e[i].w-=rlow;
e[i^1].w+=rlow;
if (used==flow) break;
}
}
}
return used;
}
int dinic(){
while (bfs()){
dfs(s,1<<30);
}
return maxflow;
}
int main(){
freopen("tongzhuo.in","r",stdin);
freopen("tongzhuo.out","w",stdout);
n=read(),m=read();
s=n+1,t=n+2;
for (int i=1;i<=m;i++) adde(n+1,i,1),adde(i,n+1,0);
for (int i=m+1;i<=n;i++) adde(i,n+2,1),adde(n+2,i,0);
while (scanf("%d %d",&x,&y)==2){
adde(x,y,1),adde(y,x,0);
}
printf("%d\n",dinic());
return 0;
}