当前位置:网站首页>Luogu p2731 horse riding fence repair
Luogu p2731 horse riding fence repair
2022-04-23 04:58:00 【zcxxn】
On the exam, I forgot that I had studied Euler circuit .
It's not difficult to think for yourself , Bidirectional storage map , Set the point with odd degree of finding as the starting point , And then directly dfs search .
then WA The last two points ……
The positive solution is like this :
#include<cstdio>
#include<algorithm>
using namespace std;
inline int read(){
register int x=0,f=1;
register char ch=getchar();
while(ch>'9'||ch<'0'){if(ch=='-') f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=(x<<1)+(x<<3)+(ch^48);ch=getchar();}
return x*f;
}
const int N=1030;
int n;
int vis[N][N];//vis[i][j] Mark i and j Number of paths between
int cnt[N];// Store the degree of each point
int maxx,minn=1e9;
int s;// The starting point
int ans[N],num;
inline void dfs(int x){
for(register int i(minn);i<=maxx;++i){
if(vis[x][i]){
vis[x][i]--,vis[i][x]--;
dfs(i);
}
}
ans[++num]=x;// Back save answer
}
signed main(){
n=read();
for(register int i(1);i<=n;++i){
int l=read(),r=read();
vis[l][r]++,vis[r][l]++,cnt[l]++,cnt[r]++;
maxx=max(maxx,max(l,r));
minn=min(minn,min(l,r));// Confirm the upper and lower boundaries
}
for(register int i(minn);i<=maxx;++i) if(cnt[i]&1){s=i;break;}// Points with odd degrees must be the starting point or the ending point
if(!s) s=minn;// If there is no
dfs(s);
for(register int i(num);i;--i) printf("%d\n",ans[i]);// Output in reverse order
return 0;
}
WA The storage of code is like this :
inline void print(){
printf("%d\n",s);
for(register int i(1);i<=n;++i) printf("%d\n",ans[i]);
exit(0);
}
inline void dfs(int x,int num){
if(num>n) {print();}
for(register int i(minn);i<=maxx;++i){
if(vis[x][i]){
vis[x][i]--,vis[i][x]--;ans[num]=i;
dfs(i,num+1);
}
}
}
It doesn't seem to make any difference , Until I made this set of data :
4
1 2
1 3
1 4
3 4
Drawing shows , Wrong way to find 2 Because of 2 No other Unicom nodes found , So I can't output anything .
Promote it. , Above solution Only serial number priority is guaranteed , The correctness of the current path is not guaranteed .
The backtracking and reverse order output of the positive solution can ensure that the upper layer of recursion is returned in this group of cases , Until the end of the traversal, find the legal answer . And when you go back, you don't vis An array of update , To delete the wrong path directly , Avoid duplicate searches .
Of course, backtracking is stored backwards , Don't forget to lose backwards .
debug victory qwq.
版权声明
本文为[zcxxn]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230450303952.html
边栏推荐
- Detailed explanation of hregionserver
- General enumeration constant class
- L2-011 play binary tree (build tree + BFS)
- Custom switch control
- 持续集成(CI)/持续交付(CD)如何彻底改变自动化测试
- Harmonious dormitory (linear DP / interval DP)
- Flink case - Kafka, MySQL source
- Innovation training (II) task division
- 2022/4/22
- Unity攝像頭跟隨鼠標旋轉
猜你喜欢
[WinUI3]編寫一個仿Explorer文件管理器
Making message board with PHP + MySQL
AQS源码阅读
DIY 一个 Excel 版的子网计算器
深度学习笔记 —— 微调
View, modify and delete [database] table
Learning Android II from scratch - activity
Set Chrome browser background to eye protection (eye escort / darkreader plug-in)
Learning Android V from scratch - UI
【数据库】表的查看、修改和删除
随机推荐
Arduino UNO r3+LCD1602+DHT11
The 8 diagrams let you see the execution sequence of async / await and promise step by step
MySQL -- execution process and principle of a statement
Innovation training (II) task division
[2022 ICLR] Pyraformer: Low-Complexity Pyramidal Attention for Long-Range 时空序列建模和预测
HRegionServer的详解
使用model.load_state_dict()时,出现AttributeError: ‘str‘ object has no attribute ‘copy‘
Solutions to the failure of sqoop connection to MySQL
C# List字段排序含有数字和字符
La caméra Unity tourne avec la souris
多线程基本概念(并发与并行、线程与进程)和入门案例
Spark small case - RDD, spark SQL
Opencv + clion face recognition + face model training
Deep learning notes - fine tuning
《2021多多阅读报告》发布,95后、00后图书消费潜力攀升
洛谷P2731骑马修栅栏
Leetcode 1547: minimum cost of cutting sticks
MySQL - index
js 判断数字字符串中是否含有字符
Set Chrome browser background to eye protection (eye escort / darkreader plug-in)