POJ2230 Watchcow(欧拉回路 + dfs)

POJ2230 Watchcow 欧拉回路[dfs+邻接表] 思路:很显然是一个欧拉无向图。 因为要遍历每个边两遍而且方向不同。 所以可以看成是有向图来处理。 以下代码是 dfs+邻接表。 这里注意的dfs和普通的dfs不一样在于终止条件。 以前的dfs都是找到某个点后终止,现在是直到遇到某个点,那个点已经无路可走了,再终止。(因为题目已经保证要求的路径存在) 还有就是有些人估计疑惑为什么没用到栈来输出。 其实不用栈的原因很简单,就是欧拉回路的 阅读详情

Watchcow
Time Limit: 3000MS Memory Limit: 65536K
Total Submissions: 6493 Accepted: 2829 Special Judge

Description

Bessie's been appointed the new watch-cow for the farm. Every night, it's her job to walk across the farm and make sure that no evildoers are doing any evil. She begins at the barn, makes her patrol, and then returns to the barn when she's done. 

If she were a more observant cow, she might be able to just walk each of M (1 <= M <= 50,000) bidirectional trails numbered 1..M between N (2 <= N <= 10,000) fields numbered 1..N on the farm once and be confident that she's seen everything she needs to see. But since she isn't, she wants to make sure she walks down each trail exactly twice. It's also important that her two trips along each trail be in opposite directions, so that she doesn't miss the same thing twice. 

A pair of fields might be connected by more than one trail. Find a path that Bessie can follow which will meet her requirements. Such a path is guaranteed to exist.

Input

* Line 1: Two integers, N and M. 

* Lines 2..M+1: Two integers denoting a pair of fields connected by a path.

Output

* Lines 1..2M+1: A list of fields she passes through, one per line, beginning and ending with the barn at field 1. If more than one solution is possible, output any solution.

Sample Input

4 5
1 2
1 4
2 3
2 4
3 4

Sample Output

1
2
3
4
2
1
4
3
2
4
1

Hint

OUTPUT DETAILS: 

Bessie starts at 1 (barn), goes to 2, then 3, etc...


给出一个图,每次从1出发,经过每条边恰好两次,最终回到1,输出路径。


建图的时候要反向建,而后开始dfs。


AC代码:


#include "iostream"
#include "cstdio"
#include "cstring"
#include "algorithm"
#include "vector"
using namespace std;
const int MAXN = 1e4 + 10;
struct node
{
	/* data */
	int v;
	bool vis;
};
int n, m;
vector<node> v[MAXN];
void dfs(int x)
{
	for(int i = 0; i < v[x].size(); ++i)
		if(!v[x][i].vis) {
			v[x][i].vis = true;
			dfs(v[x][i].v);
		}
	printf("%d\n", x);
}
int main(int argc, char const *argv[])
{
	while(scanf("%d%d", &n, &m) != EOF) {
		while(m--) {
			int x, y;
			scanf("%d%d", &x, &y);
			node tmp;
			tmp.v = y;
			tmp.vis = false;
			v[x].push_back(tmp);
			tmp.v = x;
			v[y].push_back(tmp);
		}
		dfs(1);
	}
	return 0;
}


Neurocomputing 投稿指南 Generation Information:选择论文所属领域。上传附件(利益声明,手稿,摘要)审稿人偏好:可以选择期待的审稿人。 阅读详情

相关推荐

solidworks实体显示线框

solidworks实体莫名其妙显示为有点虚的线框

qq_35858902的博客 1万+

(欧拉回路)Watchcow(P2230)

#include #include #include #include #include #include #include using namespace std; #define N 10001 vector v[N]; int n,m; void dfs(int k) { int i,j; while (v[k].size()) { j=v[k][v[k].siz

jiangjiashi的博客 4963

three.js开发3D地图记录(二)

添加地图点击事件,点击区域,地图区域改变颜色并浮起来。

anwebDesigner的博客 1694

POJ 2230 Watchcow && USACO Watchcow 2005 January Silver (欧拉回路)

本文链接:http://www.cnblogs.com/Ash-ly/p/5398425.html 题意:   Bessie 最近做了农场看守,他每天晚上的工作就是巡视农场并且保证没有坏人破坏农场。从谷仓出发去巡视,并且最终回到谷仓。   Bessie 视力不是很好,不能像其他农场的看守一样,对农场的每一条连接不同场地的路走一遍就可以发现是不是有异常情况,他需要每条路都走两遍,并且...

dimaishu7553的博客 139

MATLAB小白也能搞定!BCT脑网络分析工具箱安装与入门指南

本文为零基础用户提供了一份详尽的BCT脑网络分析工具箱安装与入门指南。从MATLAB环境配置、BCT工具箱下载与路径添加,到连接矩阵构建、节点度与聚类系数等基础指标计算,手把手带领神经科学研究者跨越编程门槛,快速掌握脑影像数据分析的核心流程。

threejs5artist的博客 1091

西门子828D/840DSL数控机床产量数据采集实战:一个C#程序员的踩坑与填坑记录

本文详细记录了C#程序员在西门子828D/840DSL数控机床产量数据采集实战中的经验与教训。从硬件连接到软件配置,再到数据读取与异常处理,文章分享了工业现场编程的特殊挑战和解决方案,特别是针对数控系统的TCP/IP通信、PLC变量地址映射及数据解析等关键技术点。

weixin_42741930的博客 596

watchcow-欧拉回路

Bessie’s been appointed the new watch-cow for the farm. Every night, it’s her job to walk across the farm and make sure that no evildoers are doing any evil. She begins at the barn, makes her patrol, ...

Godblessuam的博客 249

BPMN2.0规范及流程引擎选型方案

本文主要简明扼要的介绍了BPMN2.0规范的内容,可让读者快速度的了解BPMN2.0所涵盖的主要内容和图形样式。也较为详细的阐述了如何绘制标准的流程图,以及在软件层面中如何实现业务流程功能,和技术选型。

比特桃 1万+

POJ 2230 通过模拟深入了解深搜有向边+链式前向星

题目分类:有向图的欧拉路(利用深度优先搜索+链式前向星) 题目大意:一个图,要将每条边恰好遍历两遍,而且要以不同的方向,还要回到原点。 定理:如果一个有向图所有顶点的入度等于出度,则该有向图存在欧拉回路。 解题思路:此题实质是建立了一个双向连接的有向图。关键在于理解深搜时从S出发,为什么一定会回到S。可以从反证法角度考虑。 假设遍历的顺序为S->A1->A2->A3->...->S->...->An,S在深搜过程中被遍历,则S的入度为1,出度为2,入度与出度不相

明月千里赴迢遥的博客 445

【大模型+知识图谱】大模型与知识图谱融合:技术演进、实践应用与未来挑战

大模型与知识图谱的融合不是简单拼接,而是认知智能的范式重构。当符号主义的精确性与连接主义的泛化能力深度交织,我们正站在通向可信AGI的关键路口。正如OpenKG技术委员会王昊奋教授所言:“2025年将是图模协同从技术探索走向规模落地的分水岭”。参考文献OpenKG 2024年度报告(同济大学王昊奋)大型语言模型与知识图谱协同研究综述(海豚实验室,2024)知识图谱与大模型结合方法概述(黄巍,2023)图模互补:知识图谱与大模型融合综述(优快云,2024)

知识靠谱的博客 1092

linux 内核OOM问题定位-SLAB_DEBUG

首先看slabinfo按的size排序查看 slub object,可以查看 space 总占用大小,及objects个数判断泄漏点 为kmalloc-128。那个泄露多看/sys/kernel/debug的slab/kmalloc*-的alloc-traces和free-traces。SLAB或者SLUB有一个用就可以,改menuconfig没有clean会覆盖之前的更改,现在看一下编译的过程。或者配置defconfig,配置完之后需要clean后再编译。1,配置menuconfig。

陌上花开缓缓归以的博客 899

poj 2230 Watchcow

// 题意: n个顶点,m条无向边,图连通,从顶点1出发,要将每条边恰好遍历两遍,而且要以不同的方向,// 最后还要回到顶点1,求出这样的路线, 题目保证有解.// 思路: 欧拉回路问题,可以将无向边转换为有向边,// 即将每条边拆成两条反向边,使其双向连通,g[a][b]=g[b][a]=1,// 然后根据建立的有向图求解该图的欧拉回路.// 注意用邻接表建图,邻接矩阵会 MLE#inclu...

weixin_30765577的博客 137

借助Gemini 3 Pro高效解决课题申报中的五大核心痛点,亲测有效经验分享

之前给大家分享过关于AI辅助课题申报的相关的内容,涵盖了申报书部分撰写技巧,但没针对性拆解各位同仁在实际申报时遇到的卡壳的地方。比如选题范围拿捏不准,定的太大,根本没法落地完成;定的太小,研究价值则体现不了;写综述时,容易单纯罗列文献,缺乏批判性视角,没办法自然融入自己的判断,找不到研究缺口;研究方法部分,可能担心方法设计不够严谨,别人难以复现.....针对这些申报中的高频痛点,接下来七哥会从五个核心要点出发,借助Gemini 3 Pro高效解决这些痛点。

智能AI让学术科研更容易 700

POJ 2230 Watchcow 欧拉回路的DFS解法(模板题)

Watchcow Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 9974 Accepted: 4307 Special Judge Description Bessie’s been appointed the new watch-cow for the farm. Every night, it’s her job t...

日拱一卒,功不唐捐 2767

大中型超市收银系统(可直接运行)

光步前台收银系统系统功能:商品销售、商品退货、挂单、收银员权限、会员销售、单品取消、整单取消、商品查询、脱机销售、脱机数据上传,基本资料下载,开钱箱、小票打印、收款结算,支持多币种付款方式(现金、储值卡、礼券、微信等),支持多种促销。运行环境:.net 4.0登陆信息用户名:1001 密码:123456内置商品信息,商品编码可输入00001000 - 00010000,测试效果相关功能键F5 结算 F9挂单 F11商品查询 F8退货 Insert单品取消 Del整单取消 F12开钱箱F2 会员 Ctrl+Z退出系统 Home 锁屏 相关源码待公开

蓝牙模块HM-10手册

蓝牙模块HM-10手册,该手册对蓝牙模块HM-10进行了详细的介绍和使用方法以及一些AT指令。

[欧拉] poj 2230 Watchcow

主题链接: http://poj.org/problem?id=2230 Watchcow Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 6055   Accepted: 2610   Special Judge Descri...

weixin_34211761的博客 115

jre9下载 jdk9

(Java SE Runtime Environment 9.0) 9.0u175 官方最新版 【x86|x64】

Watchcow POJ - 2230-欧拉回路dfs输出

题目链接http://poj.org/problem?id=2230 题目的意思是跑两边欧拉回路,方向相反,输出路径 其实可以看作有向图,dfs一下,把每条边都走过去结束然后输出,之前想太复杂了 #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> ...

qmi_q米的博客 289

深度学习工具包 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")

POJ2230 Watchcow 【欧拉回路】+【DFS】

Watchcow Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 5964   Accepted: 2561   Special Judge Description Bessie's been appointed the new watch-cow for

长风的专栏 1148

POJ - 2230 Watchcow(欧拉回路)

POJ - 2230 Watchcow(欧拉回路) #include<cstdio> #include<stack> using namespace std; const int N = 10010, M = 50010; int h[N],e[M*2],ne[M*2],idx=1; int n,m; stack<int> s,ans; void add(int a,int b) { e[idx]=b;ne[idx]=h[a];h[a]=idx++; } void e

00后的博客 361

poj 2230 Watchcow (DFS + 欧拉回路)

思路: 输出欧拉回路的路径。 对于存在欧拉回路的图,只需要对一条边深搜,搜到不能搜为止,容易证明终点一定是欧拉回路的起点。 代码: #include #include const int N = 10001; const int M = 50005; struct Edge{ int u; int v; int next; }edge[2 * M]; in

yyg的专栏 704

POJ 2230 Watchcow(欧拉回路+DFS)

http://poj.org/problem?id=2230 题意:给出无向图,要求走过所有边两次仅且两次,出发点为1求路径 思路:构造有向图,深搜,记录边的访问情况,输出 参考代码:http://hi.baidu.com/lewutian/item/5f3925ef9096f93987d9de13 #include #include using namespace std; cons

Learn as if you were to live forever 632

POJ 2230 Watchcow【有向图的欧拉回路+DFS遍历】

Watchcow Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 6869   Accepted: 2998   Special Judge Description Bessie's been appointed the new watch-cow for

mengxiang000000 964

欧拉回路输出(DFS,不用回溯!)Watchcow POJ 2230

Time Limit:3000MS Memory Limit:65536K Total Submissions:8109 Accepted:3551 Special Judge Description Bessie's been appointed the new watch-cow for the fa...

aocan6909的博客 145

poj 2230 Watchcow (欧拉回路+dfs)

Watchcow Time Limit : 6000/3000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other) Total Submission(s) : 2   Accepted Submission(s) : 2 Special Judge Problem Description Bessie's been a...

iteye_4729的博客 150

POJ - 2230 Watchcow(欧拉回路+dfs输出路径)

点击打开题目链接 Watchcow Time Limit:3000MS Memory Limit:65536K Total Submissions:7926 Accepted:3462 Special Judge Description Bessie's been appointed th

Chook_lxk的博客 752

图的类型与性质——欧拉图、(竞赛图、哈密顿图)

一、欧拉图:   无向图连通性的小问题——欧拉路问题,俗称一笔画问题。   给定一张无向图,若存在一条从节点S到节点T的路径,恰好不重不漏的经过每条边一次(可以重复经过图中的顶点),则称该路径为S到T的欧拉路。   特别的,若存在一条从节点S出发的路径,敲好不重不漏的经过每条边一次(可以重复经过图中的节点),最终回到起点S,则称该路径为欧拉回路。存在欧拉回路的无向图被称为欧拉图。   欧拉图的判定...

_Griefs 4131

POJ 2230 欧拉回路

题意 传送门 POJ 2230 Watchcow 题解 求图中每条无向边被正、反两个方向各经过一次的欧拉回路。将各边看做两条无向边,此时各点的度数都为偶数,存在欧拉回路,且各边拆成的两条边必然在欧拉回路上方向相反。DFSDFSDFS 求解欧拉回路,总时间复杂度 O(N+M)O(N+M)O(N+M)。 可以更简单地实现。无向边在邻接表中会以正、负两个方向分别保存一次,那么求解时仅对递归的有向边做标记,即可在原图直接求解欧拉回路。 #include <algorithm> #include <

neweryyy的博客 252
上一篇: POJ1368 & HDOJ1116 Play on Words(欧拉回路 + 并查集)
下一篇: POJ2513 Colored Sticks(字典树+并查集+欧拉回路)
GKHack
博客等级 码龄11年 48粉丝 291原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值