QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#480875 | #898. 二分图最大匹配 | ItheaValgulious# | TL | 0ms | 0kb | C++14 | 2.1kb | 2024-07-16 19:22:56 | 2024-07-16 19:22:56 |
answer
#include<iostream>
#include<climits>
#include<queue>
#include<cstdio>
#include<algorithm>
#include<vector>
#include<cstring>
#include<array>
#define endl '\n'
using namespace std;
using ll = long long;
using pi2 = array<int,2>;
using pi3 = array<int,3>;
const int N=1e5,M=4e5+50;
struct Edge{
int u,v,w,nxt;
} edges[M*2];
int head[N],ecnt;
void add_edge_(int u,int v,int w){
edges[++ecnt]={u,v,w,head[u]};
head[u]=ecnt;
}
void add_edge(int u,int v,int w){
add_edge_(u,v,w),add_edge_(v,u,0);
}
int cpy[N],dis[N];
bool bfs(int s,int t){
memcpy(cpy,head,sizeof(cpy)),memset(dis,0x3f,sizeof(dis));
queue<int> q;
q.push(s),dis[s]=0;
while(q.size()){
int u=q.front();
q.pop();
for(int i=head[u];i;i=edges[i].nxt){
if(edges[i].w&&dis[edges[i].v]>dis[edges[i].u]+1){
dis[edges[i].v]=dis[edges[i].u]+1;
q.push(edges[i].v);
}
}
}
return dis[t]<0x3f3f3f3f;
}
ll dfs(int u,int t,int minf){
if(u==t||!minf)return minf;
ll f,flow=0;
for(int &i=cpy[u];i;i=edges[i].nxt){
if(edges[i].w&&dis[edges[i].v]==dis[edges[i].u]+1){
if(f=dfs(edges[i].v,t,min(edges[i].w,minf))){
flow+=f,minf-=f,edges[i].w-=f,edges[((i-1)^1)+1].w+=f;
if(!minf)return flow;
}else{
dis[edges[i].v]=-1;
}
}
}
return flow;
}
ll dinic(int s,int t){
ll flow=0;
while(bfs(s,t))
flow+=dfs(s,t,INT_MAX);
return flow;
}
signed main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n1,n2,m,s,t;
cin>>n1>>n2>>m;
s=n1+n2+1,t=s+1;
for(int i=1;i<=m;i++){
int u,v;
cin>>u>>v;
u++,v++;
add_edge(u,v+n1,1);
}
for(int i=1;i<=n1;i++)add_edge(s,i,1);
for(int i=1;i<=n2;i++)add_edge(i+n1,t,1);
int f=dinic(s,t);
cout<<f<<endl;
for(int i=1;i<=ecnt;i++){
if(edges[i].u<=n1&&edges[i].v<=n1+n2&&edges[i].w==0){
cout<<edges[i].u-1<<" "<<edges[i].v-n1-1<<endl;
}
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Time Limit Exceeded
input:
100000 100000 200000 78474 45795 32144 46392 92549 13903 73460 34144 96460 92850 56318 77066 77529 84436 76342 51542 77506 99268 76410 89381 1778 61392 43607 96135 84268 74827 14857 35966 32084 94908 19876 174 1481 94390 12423 55019 64368 92587 81295 7902 25432 46032 36293 61128 73555 84836 8418 102...