当前位置:网站首页>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
边栏推荐
- Unity rawimage background seamlessly connected mobile
- js 判斷數字字符串中是否含有字符
- 信息学奥赛一本通 1212:LETTERS | OpenJudge 2.5 156:LETTERS
- C. Tree infection (simulation + greed)
- QPushbutton 槽函数被多次触发
- Sword finger offer: symmetric binary tree (recursive iteration leetcode 101)
- 独立站运营 | FaceBook营销神器——聊天机器人ManyChat
- JS determines whether the numeric string contains characters
- Unity3d practical skills - theoretical knowledge base (I)
- 深度学习笔记 —— 微调
猜你喜欢

Excel protects worksheets and workbooks from damage

Details related to fingerprint payment
![[winui3] Écrivez une copie du gestionnaire de fichiers Explorer](/img/3e/62794f1939da7f36f7a4e9dbf0aa7a.png)
[winui3] Écrivez une copie du gestionnaire de fichiers Explorer

【数据库】MySQL单表查询
![解决ValueError: Argument must be a dense tensor: 0 - got shape [198602], but wanted [198602, 16].](/img/99/095063b72390adea6250f7b760d78c.png)
解决ValueError: Argument must be a dense tensor: 0 - got shape [198602], but wanted [198602, 16].

持续集成(CI)/持续交付(CD)如何彻底改变自动化测试

POI export message list (including pictures)

Making message board with PHP + MySQL

MySQL - index

使用model.load_state_dict()时,出现AttributeError: ‘str‘ object has no attribute ‘copy‘
随机推荐
List remove an element
2022/4/22
Wine (COM) - basic concept
What are instruction cycles, machine cycles, and clock cycles?
getprop 属性
拼了!两所A级大学,六所B级大学,纷纷撤销软件工程硕士点!
[2022 ICLR] Pyraformer: Low-Complexity Pyramidal Attention for Long-Range 时空序列建模和预测
MySQL memo (for your own query)
Deep learning notes - data expansion
C list field sorting contains numbers and characters
C. Tree infection (simulation + greed)
Unity攝像頭跟隨鼠標旋轉
Innovation training (VII) FBV view & CBV view
JS determines whether the numeric string contains characters
《2021多多阅读报告》发布,95后、00后图书消费潜力攀升
Innovation training (V) configuration information
多线程基本概念(并发与并行、线程与进程)和入门案例
Field injection is not recommended using @ Autowired
MySQL - index
JS generates a specified number of characters according to some words