当前位置:网站首页>登山小分队(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;
}
边栏推荐
- [paper reading] icml2020: can autonomous vehicles identify, recover from, and adapt to distribution shifts?
- Grpc, oauth2, OpenSSL, two-way authentication, one-way authentication and other column directories
- Implementation method of data platform landing
- Open3D ISS关键点
- opencv学习笔记四——膨胀/腐蚀/开运算/闭运算
- [quick start of Digital IC Verification] 11. Introduction to Verilog testbench (VTB)
- Input and output of floating point data (C language)
- Standard function let and generic extension function in kotlin
- POJ - 3784 Running Median(对顶堆)
- POJ - 3616 Milking Time(DP+LIS)
猜你喜欢

Through the "last mile" of legal services for the masses, fangzheng Puhua labor and personnel law self-service consulting service platform has been frequently "praised"
![[hard core science popularization] working principle of dynamic loop monitoring system](/img/d4/0c0281aec5a4f444528e8cfd401598.jpg)
[hard core science popularization] working principle of dynamic loop monitoring system

单元测试报告成功率低

Analyzing the influence of robot science and technology development concept on Social Research

Teach you how to select PCB board by hand (II)

Fluentd is easy to use. Combined with the rainbow plug-in market, log collection is faster

Low success rate of unit test report

使用SwinUnet训练自己的数据集

XCiT学习笔记

Explore creativity in steam art design
随机推荐
All about PDF crack, a complete solution to meet all your PDF needs
Input of mathematical formula of obsidan
Using nocalhost to develop microservice application on rainbow
Practice of combining rook CEPH and rainbow, a cloud native storage solution
[quick start of Digital IC Verification] 12. Introduction to SystemVerilog testbench (svtb)
2-3 lookup tree
PVTV2--Pyramid Vision TransformerV2学习笔记
Automatic upgrading of database structure in rainbow
21 general principles of wiring in circuit board design_ Provided by Chengdu circuit board design
The truth of robot education in hands-on practice
Learn how to compile basic components of rainbow from the source code
Tips for using jeditabletable
POJ - 3784 running medium
[kuangbin] topic 15 digit DP
ES6_ Arrow function
[quick start of Digital IC Verification] 10. Verilog RTL design must know FIFO
字符串操作
DeiT学习笔记
如何在HarmonyOS应用中集成App Linking服务
Arm GIC (IV) GIC V3 register class analysis notes.