当前位置:网站首页>登山小分队(dfs)
登山小分队(dfs)
2022-07-07 05:38:00 【why151】
题目描述:
主要思路:
开始看这个题目没看清楚,以为是每个人可以任意选一条路向上爬。
后来发现是每一条路上边一个人,然后我就没有思路了。
因为我不知道怎么判断那条路面临着同时过两个人的问题。
然后我看了dalao的代码,还是倒着从1开始,每次每条路从下一个节点往上上一个人,我觉得很妙!
#include<iostream>
#include<algorithm>
#include<cstring>
#include<vector>
using namespace std;
int num[1010];
vector<int> l[1010];
int f[1010];//记录每个点上有几个人
//每个节点上的一个人往上一个节点走一步
void dfs(int x,int last)
{
for(int i=0;i<l[x].size();i++)
{
int nowx=l[x][i];
if(nowx!=last)
{
if(f[nowx])
{
f[nowx]-=1;
f[x]+=1;
}
dfs(nowx,x);
}
}
}
int main()
{
int n;
cin>>n;
for(int i=1;i<n;i++)
{
int x,y;
cin>>x>>y;
l[x].push_back(y);
l[y].push_back(x);
num[x]+=1;
num[y]+=1;
}
int sum=0;
for(int i=2;i<=n;i++)
{
if(num[i]==1) f[i]=1,sum+=1;
}
int ans=0;
while(f[1]!=sum)
{
dfs(1,0);
ans+=1;
}
cout<<ans<<endl;
return 0;
}
边栏推荐
- POJ - 3616 Milking Time(DP+LIS)
- Splunk中single value视图使用将数值替换为文字
- Obsidan之数学公式的输入
- [machine learning] watermelon book data set_ data sharing
- 如何在HarmonyOS应用中集成App Linking服务
- [paper reading] icml2020: can autonomous vehicles identify, recover from, and adapt to distribution shifts?
- How to understand distributed architecture and micro service architecture
- Basic data types and string types are converted to each other
- Coquette data completes the cloud native transformation through rainbow to realize offline continuous delivery to customers
- 2-3 lookup tree
猜你喜欢
[untitled]
iptables 之 state模块(ftp服务练习)
2-3查找树
opencv学习笔记三——图像平滑/去噪处理
Lua 编程学习笔记
opencv学习笔记二——图像基本操作
[quick start of Digital IC Verification] 11. Introduction to Verilog testbench (VTB)
Golang compilation constraint / conditional compilation (/ / +build < tags>)
打通法律服务群众“最后一公里”,方正璞华劳动人事法律自助咨询服务平台频获“点赞”
Splunk子查询模糊匹配csv中字段值为*
随机推荐
Opencv learning notes 1 -- several methods of reading images
Don't stop chasing the wind and the moon. Spring mountain is at the end of Pingwu
Learn how to compile basic components of rainbow from the source code
Download and install orcale database11.2.0.4
Caractéristiques de bisenet
Snyk dependency security vulnerability scanning tool
How to understand distributed architecture and micro service architecture
opencv学习笔记四——膨胀/腐蚀/开运算/闭运算
2 - 3 arbre de recherche
Are you holding back on the publicity of the salary system for it posts such as testing, development, operation and maintenance?
AVL平衡二叉搜索树
[quick start of Digital IC Verification] 13. SystemVerilog interface and program learning
饥荒云服管理脚本
Deit learning notes
Pytoch (VI) -- model tuning tricks
MES系統,是企業生產的必要選擇
使用BiSeNet实现自己的数据集
Automatic upgrading of database structure in rainbow
Kotlin combines flatmap for filtering and zip merge operators
Transformation function map and flatmap in kotlin