a coding task

int solution(int n, vector<vector<int>> flattree) 
requirement :
1. build a tree from flattree.
2. flattree[i] for each valid i contains two elements and represents an edge that connects flattree[i][0] and flattree[i][1].
3. It is guaranteed that given graph is a tree, i.e. it is connected and has no cycles.
4. the tree may has more than one branch.
5.Guaranteed constraints:
flattree.length = n - 1,
flattree[i].length = 2,
0 ≤ flattree[i][j] < n.
6. the return is the max the diameter of the tree. It is the longest path in the two any vertex.

#include <vector>
#include <algorithm>

using namespace std;

vector<vector<int>> adj;

pair<int, int> dfs(int node, int parent) {
    pair<int, int> res = {0, node};
    for (int child : adj[node]) {
        if (child != parent) {
            pair<int, int> child_res = dfs(child, node);
            res = max(res, {child_res.first + 1, child_res.second});
        }
    }
    return res;
}

int solution(int n, vector<vector<int>> flattree) {
    adj = vector<vector<int>>(n);
    for (const auto &edge : flattree) {
        adj[edge[0]].push_back(edge[1]);
        adj[edge[1]].push_back(edge[0]);
    }
    pair<int, int> p = dfs(0, -1);
    pair<int, int> q = dfs(p.second, -1);
    return q.first;
}
 

  • 7
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
A robot consists of various components that work together to perform a specific task. Here are some of the main components of a robot: 1. Sensors: Sensors are devices that measure physical quantities such as temperature, pressure, light, and sound. They are used to detect objects and changes in the environment, which helps the robot to make decisions based on the data. 2. Actuators: Actuators are devices that convert electrical signals into physical motion. They are used to move the robot's joints, wheels, and other parts. 3. Power source: Robots need a power source to operate. This can be a battery, a fuel cell, or a power cord. 4. Control system: The control system is the brain of the robot. It receives data from the sensors, processes it, and sends commands to the actuators. 5. End effector: The end effector is the tool or device that is attached to the robot's arm. It is used to perform a specific task such as welding, painting, or picking up objects. 6. Frame: The frame is the structure that supports the robot's components. It is usually made of metal or plastic and provides stability and protection. 7. Communication system: Robots need to communicate with humans or other robots. This can be done through wireless communication, Bluetooth, or other means. 8. Programming: Robots need to be programmed to perform specific tasks. This can be done through software or coding. Overall, these components work together to make a robot function and perform tasks in a variety of industries and applications.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值