P2731 骑马修栅栏 Riding the Fences 题解(欧拉回路)
题目链接
解题思路
存图+简单\(DFS\)。
坑点在于两种不同的输出方式。
#include<stdio.h>
#define N 1030
int n,g[N][N],deg[N],m=1024,M=-1;
void dfs(int p){
int i;
printf("%d\n",p);
for(i=m;i<=M;i++){
if(g[p][i]){
g[p][i]--;
g[i][p]--;
dfs(i);
}
}
}
int main(){
int i,a,b;
scanf("%d",&n);
for(i=0;i<n;i++){
scanf("%d%d",&a,&b);
if(a<m)m=a;
if(b<m)m=b;
if(a>M)M=a;
if(b>M)M=b;
g[a][b]++;g[b][a]++;
deg[a]++;deg[b]++;
}
int st=m;
for(i=m;i<=M;i++)
if(deg[i]&1){st=i;break;}
dfs(st);
return 0;
}
刚开始图方便直接在\(DFS\)内输出了,结果莫名其妙\(WA\)。想了半天才想出这样一个数据:
4
1 2
1 3
3 4
4 1
所以不能直接输出,应该存栈输出。
AC代码
#include<stdio.h>
#define N 1030
int n,g[N][N],deg[N],m=1024,M=-1,sta[N<<1],cnt;
void dfs(int p){
int i;
for(i=m;i<=M;i++){
if(g[p][i]){
g[p][i]--;
g[i][p]--;
dfs(i);
}
}
sta[++cnt]=p;
}
int main(){
int i,a,b;
scanf("%d",&n);
for(i=0;i<n;i++){
scanf("%d%d",&a,&b);
if(a<m)m=a;
if(b<m)m=b;
if(a>M)M=a;
if(b>M)M=b;
g[a][b]++;g[b][a]++;
deg[a]++;deg[b]++;
}
int st=m;
for(i=m;i<=M;i++)
if(deg[i]&1){st=i;break;}
dfs(st);
for(i=cnt;i;i--)printf("%d\n",sta[i]);
return 0;
}
P2731 骑马修栅栏 Riding the Fences 题解(欧拉回路)的更多相关文章
- 洛谷P2731 骑马修栅栏 Riding the Fences
P2731 骑马修栅栏 Riding the Fences• o 119通过o 468提交• 题目提供者该用户不存在• 标签USACO• 难度普及+/提高 提交 讨论 题解 最新讨论 • 数据有问题题 ...
- 洛谷 P2731 骑马修栅栏 Riding the Fences
P2731 骑马修栅栏 Riding the Fences 题目背景 Farmer John每年有很多栅栏要修理.他总是骑着马穿过每一个栅栏并修复它破损的地方. 题目描述 John是一个与其他农民一样 ...
- 洛谷 P2731 骑马修栅栏 Riding the Fences 解题报告
P2731 骑马修栅栏 Riding the Fences 题目背景 Farmer John每年有很多栅栏要修理.他总是骑着马穿过每一个栅栏并修复它破损的地方. 题目描述 John是一个与其他农民一样 ...
- P2731 骑马修栅栏 Riding the Fences
题目描述 John是一个与其他农民一样懒的人.他讨厌骑马,因此从来不两次经过一个栅栏.你必须编一个程序,读入栅栏网络的描述,并计算出一条修栅栏的路径,使每个栅栏都恰好被经过一次.John能从任何一个顶 ...
- luogu P2731 骑马修栅栏 Riding the Fences
入度为奇数的点,搜他. 最好邻接矩阵... #include<cstdio> #include<iostream> #define R register int using n ...
- 欧拉回路--P2731 骑马修栅栏 Riding the Fences
实在懒得复制题干了 *传送 1.定义 *如果图G(有向图或者无向图)中所有边一次仅且一次行遍所有顶点的通路称作欧拉通路. *如果图G中所有边一次仅且一次行遍所有顶点的回路称作欧拉回路. *具有欧拉回路 ...
- 「USACO」「LuoguP2731」 骑马修栅栏 Riding the Fences(欧拉路径
Description Farmer John每年有很多栅栏要修理.他总是骑着马穿过每一个栅栏并修复它破损的地方. John是一个与其他农民一样懒的人.他讨厌骑马,因此从来不两次经过一个栅栏.你必须编 ...
- USACO Section 3.3 骑马修栅栏 Riding the Fences
题目背景 Farmer John每年有很多栅栏要修理.他总是骑着马穿过每一个栅栏并修复它破损的地方. 题目描述 John是一个与其他农民一样懒的人.他讨厌骑马,因此从来不两次经过一个栅栏.你必须编一个 ...
- LG2731 骑马修栅栏 Riding the Fences
题意 John是一个与其他农民一样懒的人.他讨厌骑马,因此从来不两次经过一个栅栏.你必须编一个程序,读入栅栏网络的描述,并计算出一条修栅栏的路径,使每个栅栏都恰好被经过一次.John能从任何一个顶点( ...
随机推荐
- np.random.randint()的返回值
返回的是数组而非int 比如返回x,y 为[1][2] 而非1,2 容易在只有一维一列时没有意识到 其他函数的返回值也要注意
- WSL2+Terminal+VScode配置调试
最近几天一直想找个方法把VMware虚拟机和远程连接工具MobaXterm这一组配合替换掉,因为每次开启虚拟机操作Ubuntu都需要占用很大的内存,而且要等好久好久才能开启!!!后面还要使用MobaX ...
- 1. mac 手动安装nodejs搭建vue环境
为什么选择手动安装nodejs呢? 因为使用mac自动安装还要更新homebrew,还要安装xcode tool, 太费劲了,不如手动安装, 卸载起来也方便 再一个, 我是后台开发者, 对前端页面, ...
- git push bug
git push bug fast-forwards $ git push $ git push --help # git pull $ gp To http://git.xgqfrms.xyz:88 ...
- CSS :nth-of-type() bug
CSS :nth-of-type() bug .tools-hover-box-list-item { pointer-events: auto; box-sizing: border-box; di ...
- SwiftUI render WKWebView
SwiftUI render WKWebView // // ContentView.swift // webview-app // // Created by 夏凌晨 on 2020/10/27. ...
- 微信小程序 HTTP API
微信小程序 HTTP API promise API https://www.npmtrends.com/node-fetch-vs-got-vs-axios-vs-superagent node-f ...
- GitHub Actions & GitHub Secrets
GitHub Actions & GitHub Secrets tokens & private variable GitHub Secrets https://github.com/ ...
- Big O Complexity Graph
Big O Complexity Graph Big O === O() 算法复杂度速查表 数据结构 数组排序算法 Quicksort O(n log(n)) O(n log(n)) O(n^2) O ...
- css animation & animation-fill-mode
css animation & animation-fill-mode css animation effect https://developer.mozilla.org/en-US/doc ...