Test instructions: Given a tree, then let you find out its diameter, which is the furthest distance in two points.
Analysis: Obviously this is a tree DP, there should be three ways, namely two times DFS, two bfs, and one DFS, I only wrote the latter two.
The code is as follows:
Two times BFS:
#include <iostream> #include <cstdio> #include <cstring> #include <vector> #include <queue >using namespace Std;const int maxn = 1e5 + 5;int d[maxn];bool VIS[MAXN], Vvis[maxn];//vis is the mark of BFS, Vvis is the total Mark Vector<i Nt> G[MAXN], w[maxn];//idea is to first find a point in the tree, and then from this point to find, find the furthest point,//And then two from the furthest point to find the farthest point, the maximum value of all the points is the diameter of the tree//d[i] represents the longest distance to node I int BFS ( int root) {memset (Vis, false, sizeof (VIS)); memset (d, 0, sizeof (d)); Queue<int> Q; Q.push (root); int ans = root, m = 0; Vis[root] = Vvis[root] = true; while (!q.empty ()) {root = Q.front (); Q.pop (); for (int i = 0; i < g[root].size (); ++i) {int u = g[root][i]; if (Vis[u]) continue; Q.push (U); Vis[u] = Vvis[u] = true; D[u] = D[root] + w[root][i]; if (D[u] > m) {ans = u; m = D[u]; }}} return ans;} void init (int n) {for (int i = 1; I <= n; ++i) {g[i].clear (); W[i].cLear (); Vvis[i] = false; }}int Main () {int n, m, U, V, l; Char ch; while (CIN >> n >> m) {init (n); while (m--) {scanf ("%d%d%d%c", &u, &v, &l, &ch); G[u].push_back (v); W[u].push_back (l); G[v].push_back (U); W[v].push_back (l); } int ans = 0; for (int i = 1; I <= n; ++i) if (!vvis[i]) ans = max (ans, D[bfs (BFS (i))]); two times BFS, the first is to find the farthest point, the second is to find the farthest point of the farthest point cout << ans << endl; } return 0;}
DFS at a time:
#include <iostream> #include <cstdio> #include <cstring> #include <vector> #include <queue >using namespace Std;const int maxn = 1e5 + 5;int F[MAXN], G[MAXN], ll[maxn];vector<int> G[MAXN], w[maxn];//idea is mainly Find the furthest point of a point and add a little farther away is the diameter of the tree int dfs (int root, int fa) {if (F[root]! =-1) return f[root]; if (! G[root].size ()) return f[root] = 0; int m = 0, ans = root; for (int i = 0; i < g[root].size (); ++i) {int u = g[root][i]; if (U = = FA) continue; if (Dfs (U, root) + w[root][i] > m) {m = F[u] + w[root][i]; ans = u; }} Ll[root] = ans; int mm = 0; for (int i = 0; i < g[root].size (); ++i) {int u = g[root][i]; if (U = = FA) continue; if (F[u] + w[root][i] > mm && u = ll[root]) mm = F[u] + w[root][i]; } G[root] = mm; return f[root] = m;} void init (int n) {for (int i = 1; I <= n; ++i) {g[i].clear (); W[i].clear (); F[i] = g[i] = LL[i] =-1; }}int Main () {int n, m, U, V, l; Char ch; while (CIN >> n >> m) {init (n); while (m--) {scanf ("%d%d%d%c", &u, &v, &l, &ch); G[u].push_back (v); W[u].push_back (l); G[v].push_back (U); W[v].push_back (l); } int ans = 0; for (int i = 1; I <= n; ++i) if (f[i] = = 1) Dfs (I,-1); for (int i = 1; I <= n; ++i) ans = max (ans, f[i]+g[i]); cout << ans << endl; } return 0;}
POJ 1985 Cow Marathon (tree-shaped DP, diameter of tree)