当前位置:网站首页>登山小分队(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;
}
边栏推荐
- Caractéristiques de bisenet
- GFS distributed file system
- Thirteen forms of lambda in kotlin
- 2-3查找樹
- Golang 编译约束/条件编译 ( // +build <tags> )
- Splunk查询csv lookup table数据动态查询
- Improve the delivery efficiency of enterprise products (1) -- one click installation and upgrade of enterprise applications
- Understanding of out covariance, in inversion and invariance in kotlin
- 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"
- 23 Chengdu instrument customization undertaking_ Discussion on automatic wiring method of PCB in Protel DXP
猜你喜欢

Splunk查询csv lookup table数据动态查询

Opencv learning note 4 - expansion / corrosion / open operation / close operation

Famine cloud service management script

Deit learning notes

GFS分布式文件系统

Coquette data completes the cloud native transformation through rainbow to realize offline continuous delivery to customers

The single value view in Splunk uses to replace numeric values with text

Rainbow combines neuvector to practice container safety management

rsync远程同步

Open3D ISS关键点
随机推荐
iptables 之 state模块(ftp服务练习)
Data type - integer (C language)
Bisenet features
23 Chengdu instrument customization undertaking_ Discussion on automatic wiring method of PCB in Protel DXP
Ebpf cilium practice (1) - team based network isolation
Grpc, oauth2, OpenSSL, two-way authentication, one-way authentication and other column directories
Use of out covariance and in inversion in kotlin
rsync远程同步
The reified keyword in kotlin is used for generics
Interface as a parameter (interface callback)
Practice of combining rook CEPH and rainbow, a cloud native storage solution
One click deployment of highly available emqx clusters in rainbow
What is the method of manual wiring in PCB design in 22protel DXP_ Chengdu electromechanical Development Undertaking
Explore creativity in steam art design
[kuangbin]专题十五 数位DP
Ebpf cilium practice (2) - underlying network observability
How to realize the high temperature alarm of the machine room in the moving ring monitoring system
How to understand distributed architecture and micro service architecture
Tips for using jeditabletable
Opencv learning notes II - basic image operations