当前位置:网站首页>登山小分队(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;
}
边栏推荐
- Exercise arrangement 2.10, 11
- go写一个在一定时间内运行的程序
- Installation and configuration of PLSQL
- Automatic upgrading of database structure in rainbow
- IELTS review progress and method use [daily revision]
- Rsync remote synchronization
- MES系统,是企业生产的必要选择
- ES6_ Arrow function
- opencv学习笔记四——膨胀/腐蚀/开运算/闭运算
- Analyzing the influence of robot science and technology development concept on Social Research
猜你喜欢
Don't stop chasing the wind and the moon. Spring mountain is at the end of Pingwu
Go语言中,函数是一种类型
Bisenet features
[untitled]
AVL平衡二叉搜索树
In go language, function is a type
Explore creativity in steam art design
Opencv learning notes II - basic image operations
The field value in Splunk subquery fuzzy matching CSV is*
Rainbow combines neuvector to practice container safety management
随机推荐
Basic data types and string types are converted to each other
Snyk 依赖性安全漏洞扫描工具
Splunk查询csv lookup table数据动态查询
MySQL introduction - crud Foundation (establishment of the prototype of the idea of adding, deleting, changing and searching)
Opencv learning note 5 - gradient calculation / edge detection
Deit learning notes
Teach you how to select PCB board by hand (II)
Interface as a parameter (interface callback)
Use of any superclass and generic extension function in kotlin
Using helm to install rainbow in various kubernetes
Input of mathematical formula of obsidan
Practice of combining rook CEPH and rainbow, a cloud native storage solution
Battery and motor technology have received great attention, but electric control technology is rarely mentioned?
opencv学习笔记三——图像平滑/去噪处理
opencv学习笔记一——读取图像的几种方法
单场带货涨粉10万,农村主播竟将男装卖爆单?
POJ - 3616 Milking Time(DP+LIS)
Pvtv2--pyramid vision transformer V2 learning notes
MES system is a necessary choice for enterprise production
2-3查找樹