AcWing 861. 二分图的最大匹配. (Javascript)
原题链接
简单
作者:
cp777
,
2021-03-03 17:43:13
,
所有人可见
,
阅读 245
const N = 510;
let head = new Array(N).fill(-1);
let edge = [];
let next = [];
let idx = 0;
let add = (a, b) => {
edge[idx] = b;
next[idx] = head[a];
head[a] = idx++;
}
let st = new Int32Array(N); //该右边点是否被考虑匹配
let match = new Int32Array(N); //与右边集合点匹配的左边集合点 match[2] = 3; 左3右2匹配
let find = lpoint => { //左边集合点
for (let i = head[lpoint]; i !== -1; i = next[i]) { //遍历与lpoint相连的右边集合点
let j = edge[i];
if (!st[j]) { // 该右边点未被考虑过
st[j]++;
if (match[j] === 0 || find(match[j])) //该右边点未匹配 或者 与该右边点匹配的左边点能(且已经)与其他右边点匹配
{
match[j] = lpoint;
return true;
}
}
}
return false;
}
let n1 = 0,
n2 = 0,
m = 0;
let buf = '';
process.stdin.on('readable', function () {
let chunk = process.stdin.read();
if (chunk) buf += chunk.toString();
});
let getInputNums = line => line.split(' ').filter(s => s !== '').map(x => parseInt(x));
let getInputStr = line => line.split(' ').filter(s => s !== '');
process.stdin.on('end', function () {
buf.split('\n').forEach(function (line, lineIdx) {
if (lineIdx === 0) {
n1 = getInputNums(line)[0];
n2 = getInputNums(line)[1];
m = getInputNums(line)[2];
} else if (lineIdx <= m) {
let arr = getInputNums(line);
let a = arr[0];
let b = arr[1];
add(a, b);
if (lineIdx === m) {
let res = 0;
for (let i = 1; i <= n1; i++) {
st.fill(0);
if (find(i)) res++;
}
console.log(res);
}
}
});
});