168输出为861java_AcWing 861. 二分图的最大匹配-java-关键处注释

news/2025/2/23 0:11:59/

import java.io.*;

import java.util.Arrays;

class Main {

static int n1, n2, m;

//邻接表形式存放左边到右边的边

static int idx;

static int[] h = new int[510];

static int[] e = new int[100010];

static int[] ne = new int[100010];

static {

Arrays.fill(h,-1);

}

//记录当前遍历的左边点对应的右边点是否已搜索过

static boolean[] flag = new boolean[510];

//记录右边点匹配的左边点

static int[] match = new int[510];

static void insert(int a, int b) {

e[idx] = b;

ne[idx] = h[a];

h[a] = idx++;

}

static boolean find(int left) {

for (int i = h[left]; i != -1; i = ne[i]) {

int right = e[i];

//没搜过,进入搜索,标记为已搜索避免递归中重复搜到导致死循环

if (!flag[right]) {

flag[right] = true;

if (match[right] == 0 || find(match[right])) {

match[right] = left;

return true;

}

}

}

return false;

}

public static void main(String[] args) throws IOException {

InputStreamReader isr = new InputStreamReader(System.in);

BufferedReader in = new BufferedReader(isr);

String[] strs = in.readLine().split(" ");

n1 = Integer.parseInt(strs[0]);

n2 = Integer.parseInt(strs[1]);

m = Integer.parseInt(strs[2]);

for (int i = 1; i<=m; i++) {

strs = in.readLine().split(" ");

int a = Integer.parseInt(strs[0]);

int b = Integer.parseInt(strs[1]);

insert(a,b);

}

in.close();

isr.close();

OutputStreamWriter osw = new OutputStreamWriter(System.out);

BufferedWriter out = new BufferedWriter(osw);

int res = 0;

for (int i = 1; i<=n1; i++) {

//关键:对于每个左边点,遍历所有相关的右边点前,先将flag数组清空

Arrays.fill(flag, false);

if (find(i)) res++;

}

out.write(String.valueOf(res));

out.flush();

out.close();

osw.close();

}

}


http://www.ppmy.cn/news/675939.html

相关文章

861. 二分图的最大匹配

给定一个二分图&#xff0c;其中左半部包含 n1n1 个点&#xff08;编号 1∼n11∼n1&#xff09;&#xff0c;右半部包含 n2n2 个点&#xff08;编号 1∼n21∼n2&#xff09;&#xff0c;二分图共包含 mm 条边。 数据保证任意一条边的两个端点都不可能在同一部分中。 请你求出…

CEA-861-D infoframe

1. infoframe是什么&#xff1f; Various types of auxiliary data can be carried from the Source to the DTV Monitor using InfoFrames. This section describes the InfoFrames that have been defined so far 将source端的auxiliary信息通过接口发送到sink端。 sink端应…

CTA-861标准解析EDID的VSDB与VDB

之前在某项目上做屏幕自适应分辨率时&#xff0c;按照vesa标准解析edid得出的分辨率不全导致自适应功能概率性失效&#xff0c;换为CTA 861标准解析后功能正常。此功能的代码对数据结构知识的要求不高&#xff0c;但是对C语言能力要求较高&#xff0c;特别是数位移、临界值的判…

LeetCode第 861 题:翻转矩阵后的得分(C++)

861. 翻转矩阵后的得分 - 力扣&#xff08;LeetCode&#xff09; 可以进行的操作是行变换或列变换&#xff0c;最终的目的是要使得最后的数字和最大。 行变换只会影响一个数字&#xff08;该行的数字&#xff09;。由于矩阵的 0/1 呈现的是二进制格式&#xff08;数字是按照行…

Angular实现一个简单的带tabs选项卡切换的首页导航功能

Angular版本&#xff1a;16.1.1 项目结构&#xff1a; angular.json配置&#xff1a; {"$schema": "./node_modules/angular/cli/lib/config/schema.json","version": 1,"newProjectRoot": "projects","projects"…

leetcode周赛补题 6.25

第107场双周赛补题 6892. 最大字符串配对数目 - 力扣&#xff08;LeetCode&#xff09; 思路&#xff1a;暴力模拟 class Solution { public:bool check(string a, string b){for(int i 0; i < 2; i ) if(a[i] ! b[1 - i]) return false;return true;}int maximumNumber…

密码学读书笔记系列(三):《商用密码应用与安全性评估》

密码学读书笔记系列&#xff08;三&#xff09;&#xff1a;《商用密码应用与安全性评估》 思考/前言第1章 密码基础知识1.1 密码应用概述1.2 密码应用安全性评估&#xff08;密评&#xff09;的基本原理1.3 密码技术发展1.4 密码算法1.5 密钥管理1.6 密码协议1.7 密码功能实现…

CSDN会员服务协议

特别提示 本协议系由北京创新乐知网络技术有限公司及其关联公司&#xff08;以下简称“CSDN”&#xff09;与所有使用CSDN会员服务的主体&#xff08;包括但不限于个人、团队等&#xff09;&#xff08;以下简称“用户”&#xff09;对CSDN会员服务的使用及相关服务所订立的有…