当前位置:网站首页>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
边栏推荐
- Innovation training (XII) reptile
- Getprop property
- Innovation training (10)
- L2-011 玩转二叉树(建树+BFS)
- 多线程基本概念(并发与并行、线程与进程)和入门案例
- Sword finger offer: push in and pop-up sequence of stack
- La caméra Unity tourne avec la souris
- Wechat payment function
- DIY 一个 Excel 版的子网计算器
- Unity rawimage background seamlessly connected mobile
猜你喜欢
Basic concepts of multithreading (concurrency and parallelism, threads and processes) and entry cases
解决ValueError: Argument must be a dense tensor: 0 - got shape [198602], but wanted [198602, 16].
Innovation training (IX) integration
拼了!两所A级大学,六所B级大学,纷纷撤销软件工程硕士点!
The 8 diagrams let you see the execution sequence of async / await and promise step by step
Set Chrome browser background to eye protection (eye escort / darkreader plug-in)
Thoughts on a small program
【数据库】表的查看、修改和删除
持续集成(CI)/持续交付(CD)如何彻底改变自动化测试
POI export message list (including pictures)
随机推荐
Custom switch control
Basic concepts of multithreading (concurrency and parallelism, threads and processes) and entry cases
Manually write smart pointer shared_ PTR function
Alibaba tip: it is better to create threads manually
2022/4/22
AQS源码阅读
js 判斷數字字符串中是否含有字符
Pixel 5 5g unlocking tutorial (including unlocking BL, installing edxposed and root)
Day.js 常用方法
Windows remote connection to redis
Details related to fingerprint payment
Mac 进入mysql终端命令
TypeError: ‘Collection‘ object is not callable. If you meant to call the ......
Unity攝像頭跟隨鼠標旋轉
Deep learning notes - data expansion
Detailed explanation of hregionserver
La caméra Unity tourne avec la souris
Differences between redis and MySQL
Case of using stream load to write data to Doris
PHP 统计指定文件夹下文件的数量