当前位置:网站首页>登山小分队(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;
}
边栏推荐
- Openvscode cloud ide joins rainbow integrated development system
- Lua 编程学习笔记
- Learn how to compile basic components of rainbow from the source code
- Using helm to install rainbow in various kubernetes
- IP-guard助力能源企业完善终端防泄密措施,保护机密资料安全
- Laravel8 uses passport login and JWT (generate token)
- IELTS review progress and method use [daily revision]
- POJ - 3616 Milking Time(DP+LIS)
- Exercise arrangement 2.10, 11
- One click installation of highly available Nacos clusters in rainbow
猜你喜欢

Go语言中,函数是一种类型

Practice of combining rook CEPH and rainbow, a cloud native storage solution

Input and output of floating point data (C language)

Famine cloud service management script

Low success rate of unit test report

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

PLSQL的安装和配置

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

Learn how to compile basic components of rainbow from the source code

【无标题】
随机推荐
Analyzing the influence of robot science and technology development concept on Social Research
Virtual address space
Opencv learning note 3 - image smoothing / denoising
Implementation of navigation bar at the bottom of applet
POJ - 3616 Milking Time(DP+LIS)
Splunk子查询模糊匹配csv中字段值为*
Practice of combining rook CEPH and rainbow, a cloud native storage solution
Go语言中,函数是一种类型
Ebpf cilium practice (1) - team based network isolation
POJ - 3784 running medium
Several ways of lambda used in functions in kotlin (higher-order functions)
Pvtv2--pyramid vision transformer V2 learning notes
Open3d ISS key points
Coquette data completes the cloud native transformation through rainbow to realize offline continuous delivery to customers
Bisenet features
Laravel8 uses passport login and JWT (generate token)
JEditableTable的使用技巧
Interpreting the practical application of maker thinking and mathematics curriculum
字符串操作
饥荒云服管理脚本