当前位置:网站首页>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
边栏推荐
- 泰克示波器DPO3054自校准SPC失败维修
- Innovation training (II) task division
- QPushbutton 槽函数被多次触发
- Perfect test of coil in wireless charging system with LCR meter
- The unity camera rotates with the mouse
- 独立站运营 | FaceBook营销神器——聊天机器人ManyChat
- Use the built-in function of win to transfer files between two computers in the same LAN (the speed is the same as that between local disks)
- 深度学习笔记 —— 数据增广
- Flink case - Kafka, MySQL source
- 用LCR表完美测试无线充电系统中的线圈
猜你喜欢

拼了!两所A级大学,六所B级大学,纷纷撤销软件工程硕士点!

独立站运营 | FaceBook营销神器——聊天机器人ManyChat

Unity rawimage background seamlessly connected mobile
![[2022 ICLR] Pyraformer: Low-Complexity Pyramidal Attention for Long-Range 时空序列建模和预测](/img/7c/51ac43080d9721f1bdc1cd78cd685b.png)
[2022 ICLR] Pyraformer: Low-Complexity Pyramidal Attention for Long-Range 时空序列建模和预测

Opencv + clion face recognition + face model training

COM in wine (2) -- basic code analysis
![[WinUI3]編寫一個仿Explorer文件管理器](/img/3e/62794f1939da7f36f7a4e9dbf0aa7a.png)
[WinUI3]編寫一個仿Explorer文件管理器

Set Chrome browser background to eye protection (eye escort / darkreader plug-in)

Deep learning notes - data expansion

What are the redis data types
随机推荐
MySQL time function query
Better way to read configuration files than properties
Gets all dates between two times
vscode ipynb文件没有代码高亮和代码补全解决方法
Windows remote connection to redis
How can continuous integration (CI) / continuous delivery (CD) revolutionize automated testing
C. Tree infection (simulation + greed)
Excel uses the functions of replacement, sorting and filling to comprehensively sort out financial data
静态流水线和动态流水线的区别认识
Machine learning - linear regression
使用model.load_state_dict()时,出现AttributeError: ‘str‘ object has no attribute ‘copy‘
Teach you how to build the ruoyi system by Tencent cloud
[winui3] write an imitation Explorer file manager
Unity camera rotation with sliding effect (rotation)
[WinUI3]编写一个仿Explorer文件管理器
Innovation training (V) configuration information
Repair of self calibration SPC failure of Tektronix oscilloscope dpo3054
Opencv + clion face recognition + face model training
Unity rawimage background seamlessly connected mobile
Spark small case - RDD, spark SQL