当前位置:网站首页>1021 deep root (25 points)
1021 deep root (25 points)
2022-06-29 10:14:00 【Momo 623】
- Use a random dot x Conduct dfs,dfs Record the distance in x Farthest point set
- Take a random point from the set , Conduct dfs, Get the farthest point set again
- The intersection of two sets + Is the farthest node
Liunuo :
The main idea of the topic : give n Nodes (1~n) Between n side , Ask if it can form a tree , If it cannot be constructed, the number of connected components it has is output , If it can form a tree , Output the height of the deepest tree , Root node of tree . If there are more than one , Output from small to large .
analysis : First, the depth first search determines how many connected components it has . If there are more than one , Then output Error: x components, If only one , Just two deep first searches , Start with a node dfs Keep the nodes with the highest height , Then start at any of these nodes dfs Get the nodes with the highest height , The union of these two sets of nodes is the result
#include <iostream>
#include <algorithm>
#include <vector>
#include <cstring>
#include <set>
using namespace std;
const int n = 1e5 + 10;
int N;
vector<int> v[n];
bool dp[n];
// set<int> ans;
set<int> ans;
vector<int> temp;
int maxt;
void dfs(int k, int l)
{
dp[k] = 1;
if (maxt < l)
{
maxt = l;
temp.clear();
temp.push_back(k);
}
if (maxt == l)
temp.push_back(k);
for (auto &e : v[k])
if (!dp[e])
dfs(e, l + 1);
}
int main()
{
cin >> N;
int x, y;
for (int i = 1; i < N; i++)
{
cin >> x >> y;
v[x].push_back(y);
v[y].push_back(x);
}
int cnt = 0;
for (int i = 1; i <= N; i++)
{
if (!dp[i])
{
cnt++;
dfs(i, 1);
if (i == 1)
for (auto &e : temp)
ans.insert(e);
}
}
if (cnt != 1)
printf("Error: %d components", cnt);
else
{
temp.clear();
maxt = 0;
memset(dp, 0, sizeof dp);
dfs(*ans.begin(), 1);
for (auto &e : temp)
ans.insert(e);
for (auto &e : ans)
cout << e << endl;
}
return 0;
}
边栏推荐
- Codeforces Round #641 Div2
- Application of keil5 integrated development environment for single chip microcomputer
- Recyclerview refreshes blinks and crashes when deleting items
- acwing271【杨老师的照相排列】【线性DP】
- Middle order traversal of Li Kou 94 binary tree
- L1-009 N个数求和 (20 分)
- URAL1517 Freedom of Choice 【后缀数组:最长公共连续子串】
- L2-031 深入虎穴 (25 分)
- To 3 --- 最后的编程挑战
- 走迷宫 bfs 中等+——最后的编程挑战
猜你喜欢

JVM之方法返回地址

自定义控件之侧滑关闭 Activity 控件

HDU 6778 car (group enumeration -- > shape pressure DP)

Six dimensional space BFS

Time varying and non time varying

JVM method return address

The collapsing "2.3 * 10 = 22" produced by multiplying float and int

Alternative implementation of Scrollview pull-down header amplification

Sixteen system counter and flow lamp

时变和非时变
随机推荐
Codeforces Round #657 Div. 2
Simulation problem of two stacks
如果我在北京,到哪里开户比较好?另外想问,现在在线开户安全么?
HDU 6778 Car (分组枚举-->状压 dp)
指针数组、数组指针和传参的相关问题
自定义控件之侧滑关闭 Activity 控件
蛇形填数
同花顺炒股软件可靠吗,安全吗?
Middle order traversal of Li Kou 94 binary tree
JVM之对象的内存布局
Flutter 基础组件之 Container
Related problems of pointer array, array pointer and parameter passing
2019.11.13训练总结
Using rancher to build kubernetes cluster
TLAB of JVM
L2-031 go deep into the tiger's den (25 points)
JVM四种调用方法的指令
JVM之方法的绑定机制
PHP内存马技术研究与查杀方法总结
container