当前位置:网站首页>PAT 1021. Traversal of the deep root (25 points) graph, DFS, calculating the number of connected components
PAT 1021. Traversal of the deep root (25 points) graph, DFS, calculating the number of connected components
2022-06-28 21:57:00 【Python ml】
1021. Deepest Root (25 branch )
#include <iostream>
#include <vector>
#include <algorithm>
#include <set>
using namespace std;
int maxheight=0;
vector<vector<int>>v;
vector<bool>visit;
set<int>s;
vector<int>temp;
void dfs(int node,int height){
visit[node]=true;
if(height>maxheight){
maxheight=height;
temp.clear();
temp.push_back(node);
}else if(height==maxheight){
temp.push_back(node);
}
for(int i=0;i<v[node].size();i++){
if(visit[v[node][i]]==false)
dfs(v[node][i],height+1);
}
}
int main() {
int n,a,b,cnt=0,s1=0;
cin>>n;
v.resize(n+1),visit.resize(n+1);
for(int i=0;i<n-1;i++){
cin>>a>>b;
v[a].push_back(b);
v[b].push_back(a);
}
for(int i=1;i<=n;i++){
if(visit[i]==false){
dfs(i,1);
if(i==1){
// for the first time dfs End of traversal , Record another farthest starting point s1, And the farthest node set ;
if(temp.size()!=0) s1=temp[0];
for(int j=0;j<temp.size();j++)
s.insert(temp[j]);
}
cnt++; // Connected component ++
}
}
if(cnt>1) printf("Error: %d components", cnt);
else{
temp.clear();
maxheight=0;
fill(visit.begin(),visit.end(),false);
dfs(s1,1);
for(int i=0;i<temp.size();i++)
s.insert(temp[i]);
for(auto it=s.begin();it!=s.end();it++)
cout<<*it<<endl;
}
return 0;
}
边栏推荐
- LeetCode877. Stone game
- If you are a C developer, look at these three explicit programming techniques
- QStringLiteral(str)
- LeetCode986. Intersection of interval lists
- LeetCode:合并两个有序链表_21
- SqlTransaction
- Deep interpretation of WiFi security vulnerability krack
- 开通股票炒股账号安全吗?是靠谱的吗?
- 构建实战化防御体系之立体防渗透
- Application practice | 1billion data second level correlation. Huolala's OLAP System Evolution Based on Apache Doris (with PPT download)
猜你喜欢

华为云的AI深潜之旅

What is an interface? What is interface testing?

接口测试流程

Application practice | 1billion data second level correlation. Huolala's OLAP System Evolution Based on Apache Doris (with PPT download)

Lumiprobe non fluorescent alkyne research - dbco NHS ester

Microsoft's exclusive payment function has also been perfectly unlocked

How to analyze the relationship between enterprise digital transformation and data asset management?

In one sentence, I will tell you the meaning of select 1, 2 and 3 in SQL injection, and explain the meaning of each part of SQL injection in detail

Alist+raidrive gives the computer a complete 8billion GB hard disk drive
![[Note: circuit intégré MOS analogique] référence de bande Gap (principe de base + mode courant + circuit en mode tension)](/img/cd/be62272d465ca990456c222b38df67.png)
[Note: circuit intégré MOS analogique] référence de bande Gap (principe de base + mode courant + circuit en mode tension)
随机推荐
Leetcode: merge two ordered linked lists_ twenty-one
Deep interpretation of WiFi security vulnerability krack
Security dilemma of NFT liquidity agreement - Analysis of the hacked event of NFT loan agreement xcarnival
LeetCode560. Subarray with and K
接口测试流程
Is it safe to open a stock trading account? Is it reliable?
Pyechart drawing multiple Y-axis line graphs
Constructing the three-dimensional anti penetration of the actual combat defense system
LeetCode560. 和为K的子数组
QT how the coordinates of one control are relatively fixed and displayed on another control (coordinate system)
In one sentence, I will tell you the meaning of select 1, 2 and 3 in SQL injection, and explain the meaning of each part of SQL injection in detail
How to analyze the relationship between enterprise digital transformation and data asset management?
Lua源码剖析:一. lua变量类型可变特性在C代码中实现。
Sword finger offer:[day 1 stack and queue (simple)] --- > stack containing min function
LeetCode:二叉树展开为链表_114
Hashicorp/raft introduction and source code analysis (III): introduction to cluster node recovery
Leetcode daily question - 522 Longest special sequence II
16 `bs object Node name Div. attribute contents ` children descendants get child nodes and descendants
Native implementation Net 5.0+ custom log
AI deep dive of Huawei cloud