QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#665891 | #898. 二分图最大匹配 | Zhou_JK | RE | 0ms | 0kb | C++23 | 2.0kb | 2024-10-22 15:45:28 | 2024-10-22 15:45:47 |
answer
#include<bits/stdc++.h>
using namespace std;
constexpr int N=100005;
struct Hopcroft_Karp
{
int n,m;
vector<int>g[N];
int pa[N],pb[N];
Hopcroft_Karp(){}
Hopcroft_Karp(int _n,int _m):n(_n),m(_m){}
void init(int _n,int _m)
{
n=_n,m=_m;
return;
}
void add_edge(int u,int v)
{
g[u].push_back(v);
return;
}
int dis[N];
bool bfs()
{
queue<int>q;
for(int u=0;u<n;u++)
{
if(pa[u]==-1)
{
dis[u]=0;
q.push(u);
}
else dis[u]=-1;
}
bool found=false;
while(!q.empty())
{
int u=q.front();
q.pop();
for(int v:g[u])
{
if(pb[v]!=-1&&dis[pb[v]]==-1)
{
dis[pb[v]]=dis[u]+1;
q.push(pb[v]);
}
else if(pb[v]==-1) found=true;
}
}
return found;
}
bool dfs(int u)
{
for(int v:g[u])
if(pb[v]==-1||(dis[pb[v]]==dis[u]+1&&dfs(pb[v])))
{
pa[u]=v,pb[v]=u;
return true;
}
dis[u]=-1;
return false;
}
int max_matching()
{
fill(pa,pa+n,-1);
fill(pb,pb+m,-1);
int res=0;
while(bfs())
{
for(int u=0;u<n;u++)
if(pa[u]==-1&&dfs(u)) res++;
}
return res;
}
};
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr),cout.tie(nullptr);
int n,m,e;
cin>>n>>m>>e;
Hopcroft_Karp solver(n,m);
for(int i=1;i<=e;i++)
{
int u,v;
cin>>u>>v;
u--,v--;
solver.add_edge(u,v);
}
int res=solver.max_matching();
cout<<res<<"\n";
for(int i=0;i<n;i++)
cout<<solver.pa[i]+1<<" ";
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Runtime Error
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...