当前位置:网站首页>登山小分队(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;
}
边栏推荐
- Go语言中,函数是一种类型
- Opencv learning notes II - basic image operations
- [go ~ 0 to 1] obtain timestamp, time comparison, time format conversion, sleep and timer on the seventh day
- 下载和安装orcale database11.2.0.4
- POJ - 3616 Milking Time(DP+LIS)
- The field value in Splunk subquery fuzzy matching CSV is*
- All about PDF crack, a complete solution to meet all your PDF needs
- Thirteen forms of lambda in kotlin
- Implementation method of data platform landing
- 2-3查找樹
猜你喜欢
![[untitled]](/img/b5/348b1d8b5d34cf10e715522b9871f2.png)
[untitled]

Input and output of floating point data (C language)

Are you holding back on the publicity of the salary system for it posts such as testing, development, operation and maintenance?

A single game with goods increased by 100000, and the rural anchor sold men's clothes on top of the list?

BiSeNet的特点

National standard gb28181 protocol video platform easygbs adds streaming timeout configuration

AVL平衡二叉搜索树

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

Interpreting the practical application of maker thinking and mathematics curriculum

Arm GIC (IV) GIC V3 register class analysis notes.
随机推荐
Deit learning notes
国标GB28181协议视频平台EasyGBS新增拉流超时配置
MySQL introduction - crud Foundation (establishment of the prototype of the idea of adding, deleting, changing and searching)
Interface as a parameter (interface callback)
Grpc, oauth2, OpenSSL, two-way authentication, one-way authentication and other column directories
Leetcode 1984. Minimum difference in student scores
【Go ~ 0到1 】 第七天 获取时间戳,时间比较,时间格式转换,Sleep与定时器
opencv学习笔记一——读取图像的几种方法
A single game with goods increased by 100000, and the rural anchor sold men's clothes on top of the list?
数据中台落地实施之法
Standard function let and generic extension function in kotlin
Golang compilation constraint / conditional compilation (/ / +build < tags>)
Ebpf cilium practice (1) - team based network isolation
Iptables' state module (FTP service exercise)
单场带货涨粉10万,农村主播竟将男装卖爆单?
rsync远程同步
[quick start of Digital IC Verification] 13. SystemVerilog interface and program learning
Splunk query CSV lookup table data dynamic query
opencv学习笔记二——图像基本操作
Componentspace2022, assertions, protocols, bindings, and configuration files