当前位置:网站首页>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
边栏推荐
- Windows remote connection to redis
- JS engine loop mechanism: synchronous, asynchronous, event loop
- Progress of innovation training (III)
- QPushbutton 槽函数被多次触发
- Innovation training (VI) routing
- JS détermine si la chaîne de nombres contient des caractères
- redis和mysql区别
- The difference between static pipeline and dynamic pipeline
- js 判斷數字字符串中是否含有字符
- Sword finger offer: symmetric binary tree (recursive iteration leetcode 101)
猜你喜欢

Teach you how to build the ruoyi system by Tencent cloud
![[2021] Spatio-Temporal Graph Contrastive Learning](/img/7d/67a0bfa0adecee24bbe291a25ae906.png)
[2021] Spatio-Temporal Graph Contrastive Learning

AQS源码阅读

Wechat payment function

Deep learning notes - fine tuning

泰克示波器DPO3054自校准SPC失败维修

MySQL -- execution process and principle of a statement

Windows remote connection to redis

Simply drag objects to the item bar

Sword finger offer: the median in the data stream (priority queue large top heap small top heap leetcode 295)
随机推荐
[WinUI3]編寫一個仿Explorer文件管理器
Windows remote connection to redis
Manually write smart pointer shared_ PTR function
Day. JS common methods
退出vim的方法
Deep learning notes - semantic segmentation and data sets
什么是指令周期,机器周期,和时钟周期?
La caméra Unity tourne avec la souris
Unity攝像頭跟隨鼠標旋轉
QPushbutton 槽函数被多次触发
Unity rawimage background seamlessly connected mobile
vscode ipynb文件没有代码高亮和代码补全解决方法
The object needs to add additional attributes. There is no need to add attributes in the entity. The required information is returned
Pixel 5 5g unlocking tutorial (including unlocking BL, installing edxposed and root)
深度学习笔记 —— 数据增广
The unity camera rotates with the mouse
Record the ThreadPoolExecutor main thread waiting for sub threads
【数据库】表的查看、修改和删除
Spell it! Two A-level universities and six B-level universities have abolished master's degree programs in software engineering!
Set Chrome browser background to eye protection (eye escort / darkreader plug-in)