求无向图中所有的环 C++ python

VIP求无向图中所有的环 C++ python
求无向图中所有的环
开通可读全文 ·1.1k 人阅读
最低0.47元/天 解锁文章
  • 顶点从1~n
  • 关键在于避免重复,可以给环定标准:第一个点是序号最小的(确定起点),第二个点是与第一个点相连两点中的较小的点(确定搜索方向)
  • 搜索每个长度和每个点做起点,是否成环
#include <iostream>
#include <vector>
#include <set>

using namespace std;

class Graph
{
   
   
public:
    vector<set<int>> ps;
    Graph(int n, vector<vector<int>> es)
    {
   
   
        ps.resize(n+1);
        for (auto i : es)
        {
   
   
            ps[i[0]].insert(i[1]);
            ps[i[1]].insert(i[0]);
        }
    }
};

struct cir {
   
   
    vector<int> path;
    vector<int
VIP 在一个无向图中找环 在一个无向图中找环 PART1:DFS Approach: 从每个未访问的节点运行 DFS。深度优先遍历可用于检测图中的循环。连通图的 DFS 生成一棵树。仅当图中存在回边Back Edge:从一个顶点指向其祖先顶点的边时,图中才存在环。回边Back Edge是将节点连接到自身(自循环)或其在 DFS 生成的树中的祖先之一的边。 要找到其任何祖先的回边Back Edge,保留一个访问过的数组,如果任何访问过的节点都有回边Back Edge,则存在一个循环并返回 true。 Algorithm: 使用给定 开通可读全文·6.5k 阅读·3 点赞·16 收藏 阅读全文

相关推荐

题解:洛谷 P6175 无向图的最小环问题

等知名刷题平台精心挑选而来,并结合各平台提供的算法标签和难度等级进行了系统分类。题目涵盖了从基础到进阶的多种算法和数据结构,旨在为不同阶段的编程学习者提供一条清晰、平稳的学习提升路径。个点的环,环上的节点不重复,并且环上的边的长度之和最小。该问题称为无向图的最小环问题。在本题中,你需要输出最小的环的边权和。输出边权和最小的环的边权和。给定一张无向图,求图中一个至少包含。#普及# #Floyd#本文分享的必刷题目是从。

团爸讲算法 1005

华为OD机试 - 无向图染色(Python/JS/C/C++ 2024 E卷 100分)

刷的越多,抽中的概率越大,私信哪吒,备注华为OD,加入华为OD刷题交流群,每一题都有详细的答题思路、详细的代码注释、发现新题目,随时更新。

学Java,找哪吒 1171

【华为OD机试2023】无向图染色问题 C++ Java Python

【华为OD机试2023】无向图染色问题 C++ Java Python 众所周知红黑树。是一种平衡树,它最突出的特性就是不能有两个相邻的红色结点。 那我们定义一个红黑图,也就是一张 无向图中,每个节点可能有红黑两种颜色,但我们必须保证没有两个相邻的红色节点。现在给出一张未染色的图,只能染红黑两色,问总共有多少种染色方案使得它成为一个红黑图。

Abmcar的博客 844

【100%通过率】华为OD机试真题 Python 实现【无向图染色】【2022.11 Q4新题】

给一个无向图染色,可以填红黑两种颜色,必须保证相邻两个节点不能同时为红色,输出有多少种不同的染色方案?

MISAYAONE的博客 9396

【华为OD机试 2023】无向图染色(C++ Java JavaScript Python)

给一个[无向图]染色,可以填红黑两种颜色,必须保证相邻两个节点不能同时为红色,输出有多少种不同的染色方案?数据范围:1

2301_77273748的博客 628

华为OD机考2025B卷 - 无向图染色(Java & Python& JS & C++ & C )

华为OD机考2025B卷 - 无向图染色(Java & Python& JS & C++ & C )

算法大师! 7578

华为OD机试 -无向图染色(C++ Java JavaScript Python )

给一个[无向图]染色,可以填红黑两种颜色,必须保证相邻两个节点不能同时为红色,输出有多少种不同的染色方案?

算法大师! 2593

华为OD机试题解 - 无向图染色问题解析与C++、Java、JavaScript、Python实现

无向图染色问题是图论中常见的组合问题,考察了对图的遍历、递归搜索以及剪枝优化的应用。在本题中,我们通过对四种语言的实现展示了如何使用 DFS 解决这一问题,并通过位运算优化了染色状态的记录。希望通过这篇文章,大家能对无向图染色问题的解题思路和实现细节有更深入的理解。

m0_57781768的博客 303

深入解析华为OD机试无向图染色问题(支持Python/C++/Java/JavaScript多语言实现)

通过以上解析与代码实现,我们解决了华为OD机试中的一道无向图染色问题。该问题本质上是一个二分图着色问题,考查了对递归、深度优先搜索(DFS)以及位运算的理解和运用能力。通过DFS递归遍历图中的所有节点,并结合回溯算法,我们可以有效地解决这个问题。此外,我们展示了该问题在Python、C++、Java、JavaScript多种编程语言中的实现方法,便于不同编程语言背景的读者进行学习与实践。

m0_57781768的博客 679

无向图染色 - 华为OD机试真题(A卷、Python题解)

给一个无向图染色,可以填红黑两种颜色,必须保证相邻两个节点不能同时为红色,输出有多少种不同的染色方案?

学习,你不是一个人在战斗 ~ 961302305(QQ,微信) 728

华为OD机试双机位C卷 - 无向图染色 (C++ & Python & JAVA & JS & GO)

华为OD机试双机位C卷 无向图染色,提供Java、C++、Python、Go、JavaScript源码实现,并提供完整思路讲解。华为od-无向图染色。华为OD机考 - 无向图染色。

qq_45776114的博客 1606

【华为OD机考真题】- 无向图染色问题 or 红黑图((2025B卷-200分)(Python版)

🔔本文收录于「最新最全华为OD机试真题(Python版)」专栏,手把手带你零基础教学华为OD机试。本题集提供最优题解思路,解题步骤,代码解析,复杂度分析及最优题解源码等,支持多语言题解,助你轻松拿捏OD机考,一举上岸!安利大家关注&&收藏&&订阅!题库正在疯狂收录中,up!up!up!! 🚫提醒:拒绝一切代考/替考,违法必究!专栏所写题库均搜集于互联网,经过精心筛选和整理,结合数位十多年大厂实战经验资深大佬经验所撰,欢迎订阅。    💗订阅福利:一次订阅,可永久免费阅读,提供在线答疑解惑,后续题库更新

**My Coding Family** 1074

【2023.3华为OD机试】 无向图染色(Java & C++& Python)

给一个无向图染色,可以填红黑两种颜色,必须保证相邻两个节点不能同时为红色,输出有多少种不同的染色方案?数据范围:1

KALA51773的博客 156

实战指南:从零开始用YOLOv8训练VisDrone无人机检测模型

本文提供了一份详细的实战指南,手把手教你从零开始使用YOLOv8训练一个无人机检测模型。内容涵盖VisDrone数据集下载与格式转换、YAML配置文件编写、模型训练与评估,以及最终的推理部署,旨在帮助开发者快速掌握无人机视角下的目标检测技术。

salt的博客 695

ENSP从下载到运行:用虚拟机搭建华为认证实验环境全流程(含USG6000V镜像配置)

本文提供了一份详细的华为ENSP仿真平台从下载、安装到配置USG6000V防火墙镜像的完整指南。内容涵盖前置软件安装、ENSP主程序配置、核心设备镜像导入及虚拟机资源分配策略,旨在帮助网络工程师和学习者快速搭建一个稳定、功能齐全的华为认证实验环境,为备考和模拟企业网络部署打下坚实基础。

weixin_29230865的博客 548

SAP批量数据导入实战:BDC、CATT、LSMW三大工具保姆级对比教程

本文深入对比了SAP系统中三大批量数据导入工具BDC、CATT和LSMW的核心定位、适用场景与实战操作。BDC适合复杂逻辑的编程式处理,CATT适合业务用户快速录制回放,LSMW则是数据迁移项目的结构化首选。文章提供了详细的选型决策框架、性能优化技巧及常见陷阱规避指南,帮助SAP顾问和开发者高效完成数据批量维护任务。

weixin_29213525的博客 156

HC32F460 FPU实战:从零开启硬件浮点加速

本文详细介绍了如何在HC32F460开发板上启用和优化FPU(浮点运算单元),以显著提升硬件浮点运算性能。从工程配置、关键代码修改到实战优化技巧,全面解析FPU加速的实现方法,并分享常见问题的解决方案,帮助开发者避免常见陷阱,充分发挥HC32F460的硬件潜力。

weixin_42620340的博客 352

STM32实战开发(133):基于STM32的智能GPS定位跟踪系统开发

本文详细介绍了如何基于STM32开发一个智能GPS定位跟踪系统。系统使用NEO-6M GPS模块获取实时定位数据,并通过SIM800L GPRS模块将定位信息上传到远程服务器。通过OLED屏幕实时显示位置数据,使用SD卡存储历史轨迹,并提供蜂鸣器报警功能。项目实现了GPS定位数据的获取、解析、存储和传输,为智能定位系统提供了完整的解决方案。

2201_76125393的博客 1645

Python 爬虫实战:拼多多商品评论情感分析(含词云生成与差评预警)

在电商行业竞争日益激烈的当下,商家对商品评论的分析需求愈发迫切。通过 Python 爬虫收集拼多多商品评论,并结合情感分析、词云可视化及差评预警技术,商家可精准洞察消费者反馈,优化商品与服务。本文将深度剖析从爬虫搭建到数据挖掘的关键环节,助力读者构建全流程电商评论分析系统。

yansideyucsdn的博客 1455

在 Eclipse 里快速用上 SAP Joule:ABAP 开发者的 AI 助手上手指南

本文介绍了SAP Joule for developers如何集成在ABAP开发工具中,为开发者提供AI辅助能力。主要内容包括: Joule作为ADT的增强层,直接嵌入开发流程,支持在项目资源管理器、代码编辑器等场景下通过上下文菜单调用AI功能。 核心使用方式:打开Joule视图后,可在开发对象、代码片段等上下文中使用自然语言交互,请求解释、生成测试或获取示例代码。 重点功能: Explain:解释现有代码逻辑,降低理解成本 ABAP Unit测试生成:快速创建与上下文相关的测试用例 支持在传输管理器等完整

2007 年 ~ 2025 年,深耕 SAP 技术 18 年 404

从零开始学习CANoe(六)—— CAPL 测试节点

创建CAPL Test Module的两种方式

蚂蚁小兵 1万+

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

WINCC7.3破解文件

WINCC7.3硬件够破解文件,有需要的下载!WINCC7.3硬件够破解文件,有需要的下载!

计算机控制技术及应用

《计算机控制技术及应用》通过总结作者的科研成果和吸收国内外的先进理论、方法和技术,以计算机控制系统的网络化、开放化、智能化和集成化发展趋势为主线索,主要介绍计算机控制系统的基本概念和计算机控制系统的典型形式,计算机控制系统的数学描述与基本分析方法,以及数字PID控制器、最少拍控制系统、达林算法、状态变量设计法等设计方法及其各种改进算法。重点突出计算机控制系统输入输出通道接口的设计方法和典型控制系统的设计案例与实现方法等。

IEM模型,地表粗糙度模拟

利用微波反演土壤水分时候,利用IEM模型可以实现对随机地表的模拟

上一篇: 多重背包单调队列优化详解
下一篇: colab使用记录
刀么克瑟拉莫
博客等级 码龄14年 68粉丝 268原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

刀么克瑟拉莫

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值