资料 题目: https://www.spoj.com/problems/PT07Z/
https://zh.wikipedia.org/wiki/%E6%A0%91_(%E5%9B%BE%E8%AE%BA)
谢谢岛老师的教学
岛娘blog
岛娘Github
岛娘Youtube
code source
Go 下面根据上面资料的wiki图来生成的点和边 看不见可能需要科学上网
点: 1 2 3 4 5 6
边: 14 24 34 45 56
设置顶点 使用 Map https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Map
1 2 let vertexLen = 6 ; let vertex = new Map ();
设置顶点和边 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 const setVertex = v => vertex.set(v, []);const setVertexEdge = (v1, v2 ) => { vertex.get(v1).push(v2); vertex.get(v2).push(v1); }; for (let i = 1 ; i <= vertexLen; i++) setVertex(i);let vertexEdge = [[1 , 4 ], [2 , 4 ], [3 , 4 ], [4 , 5 ], [5 , 6 ]];for (let i = 0 ; i < vertexEdge.length; i++) setVertexEdge(vertexEdge[i][0 ], vertexEdge[i][1 ]);
得到的集合
1 2 3 4 5 6 7 Map { 1 => [ 4 ], 2 => [ 4 ], 3 => [ 4 ], 4 => [ 1 , 2 , 3 , 5 ], 5 => [ 4 , 6 ], 6 => [ 5 ] }
dfs vertex 结构目前是这样点 ⬆️️️️️️️️️️ ⬆️️️️️️️️️️ ⬆️️️️️️️️️️
这个方法主要通过存放一个map保存访问状态
参考地址 https://www.geeksforgeeks.org/implementation-graph-javascript/
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 const dfs = startNode => { let visited = new Map (); for (let i = 1 ; i <= vertexLen; i++) visited.set(i, false ); const dfsFunc = (startNode, visited ) => { let z = 0 ; visited.set(startNode, true ); let get_next = vertex.get(startNode); for (let i = 0 ; i < get_next.length; i++) { let get_elem = get_next[i]; if (!visited.get(get_elem)) { z = Math .max(z, dfsFunc(get_elem, visited) + 1 ); } } return z; }; return dfsFunc(startNode, visited); };
dfs 下面这个是岛老师👨🏫教我方法
主要通过存父节点来判断
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 const dfs1 = startNode => { const dfsFunc = (startNode, parentNode = -1 ) => { let z = 0 ; let get_next = vertex.get(startNode); for (let i = 0 ; i < get_next.length; i++) { let get_elem = get_next[i]; if (get_elem === parentNode) continue ; z = Math .max(z, dfsFunc(get_elem, startNode) + 1 ); } return z; }; return dfsFunc(startNode); };
查看所有代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 let vertexLen = 6 ; let vertex = new Map (); const setVertex = v => vertex.set(v, []);const setVertexEdge = (v1, v2 ) => { vertex.get(v1).push(v2); vertex.get(v2).push(v1); }; for (let i = 1 ; i <= vertexLen; i++) setVertex(i);let vertexEdge = [[1 , 4 ], [2 , 4 ], [3 , 4 ], [4 , 5 ], [5 , 6 ]];for (let i = 0 ; i < vertexEdge.length; i++) setVertexEdge(vertexEdge[i][0 ], vertexEdge[i][1 ]); const dfs = startNode => { let visited = new Map (); for (let i = 1 ; i <= vertexLen; i++) visited.set(i, false ); const dfsFunc = (startNode, visited ) => { let z = 0 ; visited.set(startNode, true ); let get_next = vertex.get(startNode); for (let i = 0 ; i < get_next.length; i++) { let get_elem = get_next[i]; if (!visited.get(get_elem)) { z = Math .max(z, dfsFunc(get_elem, visited) + 1 ); } } return z; }; return dfsFunc(startNode, visited); }; const dfs1 = startNode => { const dfsFunc = (startNode, parentNode = -1 ) => { let z = 0 ; let get_next = vertex.get(startNode); for (let i = 0 ; i < get_next.length; i++) { let get_elem = get_next[i]; if (get_elem === parentNode) continue ; z = Math .max(z, dfsFunc(get_elem, startNode) + 1 ); } return z; }; return dfsFunc(startNode); }; let z = dfs(1 );console .log(z);let z1 = dfs1(1 );console .log(z1);console .log(vertex);
— 分割线 —
很遗憾上面的是有问题的
岛老师的作业批改
数据没有从 IO 读入读出。
第一个 dfs 求出的不是最远的端点。
正确的解法应该是先求最深的一个端点,然后用从这个端点再搜索一次.
因为 js 在 https://www.spoj.com 跑不过, 在http://codeforces.com 可以跑但是没找到题目,然后就选用了c++
在codeforces js输入输出
http://codeforces.com/blog/entry/10594
http://codeforces.com/blog/entry/64707
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 pair<int, int> dfs(int u, int p = -1) { pair <int , int > z = {0 , u}; for (auto v: edge[u]) { if (v == p) continue ; z = max(z, dfs(v, u)); } z.first += 1 ; return z; }
查看所有代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 #include <iostream> #include <vector> using namespace std ;const int N = 1e6 ;vector <int > edge[N];pair<int, int> dfs(int u, int p = -1) { pair <int , int > z = {0 , u}; for (auto v: edge[u]) { if (v == p) continue ; z = max(z, dfs(v, u)); } z.first += 1 ; return z; } int main () { int n; cin >> n; for (int i = 0 ; i < n - 1 ; i++) { int v, u; cin >> v >> u; edge[v].push_back(u); edge[u].push_back(v); } pair <int , int > z = dfs(1 ); z = dfs(z.second); cout << z.first-1 << endl ; }
希望看到的大佬可以多多指点迷津!!! 右边有我的联系方式💗💗