A-breadth-first Search by FoxpowerTime
limit:2000MS
Memory Limit:131072KB
64bit IO Format:%lld &%llu Submit Status
Description
A-breadth-first Search by Foxpowerproblem Statement
Fox Ciel went to JAG kingdom by bicycle, but she forgot a place where she parked her bicycle. So she needs-to-search it from a bicycle-parking area before returning home.
The parking area is formed as a unweighted rooted treeTT withNn Vertices, numbered11 throughNN. Each vertex have a space for parking one or more bicycles. Ciel thought that she parked her bicycle near the vertex11, so she decided-search it from there by the Breadth-first search. That's, she searches it at the vertices in the increasing order of their distances from the vertex 11. I F multiple vertices has the same distance, she gives priority to the vertices in the order of searching at their parents. If multiple vertices has the same parent, she searches at the vertex with minimum number at first.
Unlike a computer, she can ' t go to a next vertex by random access. Thus, if she goes to the vertexJJ after the vertexII, she needs to walk the distance between the verticesIi and J "> j j. BFS by Fox Power Perhaps takes a long time, so she asks your to calculate the total m Oving distance in the worst case starting from the Vertex 1 "> 1 1.
Input
The input is formatted as follows.
NN
P2 P2P3 P3p4 P4 ? ? P-npn
The first line contains an integerNN (1≤n≤5 1≤n≤105), which is the number of vertices on the unweighted rooted treeTT. The second line containsn−1 n−1 integerspI Pi (1≤pI<i 1≤pi<i), which is the parent of the vertexIi. The Vertex 1 "> 1 1 is a root node, So p 1 "> p1 p1 does not exist.
Output
Print the total moving distance in the worst case on one line.
Sample Input 1
41 1 2
Output for the Sample Input 1
6
Sample Input 2
41 1 3
Output for the Sample Input 2
4
Sample Input 3
111 1 3 3 2 4 1 3 2 9
Output for the Sample Input -
25
Test Instructions: There is a tree, the distance between the directly connected nodes are 1, a person standing in the root node, from the depth from low to high search, from each time can only be the same depth of the search to the next level to search, ask at least how much distance it takes to access all the nodes again
#include <iostream> #include <cstdio> #include <cstring> #include <cstdlib> #include <cmath > #include <vector> #include <queue> #include <stack> #include <map> #include <algorithm > #include <set>using namespace std;typedef long long ll;typedef unsigned long long Ull; #define MM (A, B) memset (A, B , sizeof (a)); const double EPS = 1e-10;const int inf =0x7f7f7f7f;const double pi=acos ( -1); const int Maxn=100000+10;int C[ma xn][22],deep[maxn],vis[maxn];vector<vector<int> > G (MAXN); void dfs (int u,int par) {c[u][0]=par; for (int i=1;i<=20;i++) c[u][i]=c[c[u][i-1]][i-1]; for (int i=0;i<g[u].size (); i++) {int v=g[u][i]; if (V==par) continue; deep[v]=deep[u]+1; DFS (V,U); }}int LCA (int u,int v) {if (Deep[u]<deep[v]) swap (U,V); for (int i=20;i>=0;i--) if (Deep[c[u][i]]>=deep[v]) u=c[u][i]; if (u==v) return u; for (int i=20;i>=0;i--) if (C[u][i]^c[v][i]) { U=c[u][i]; V=c[v][i]; } return c[u][0];} int dis (int u,int v) {return Deep[u]+deep[v]-2*deep[lca (u,v)];} int main () {int n; while (~SCANF ("%d", &n)) {for (int i=1;i<=n;i++) g[i].clear (); deep[1]=0; for (int i=2;i<=n;i++) {int x;scanf ("%d", &x); G[x].push_back (i); } dfs (a); int Pre=1;ll ans=0; Queue<int> Q; Q.push (1); while (Q.size ()) {int U=q.front (); Q.pop (); for (int i=0;i<g[u].size (); i++) {int v=g[u][i]; Ans+=dis (PRE,V); Pre=v; Q.push (v); }} printf ("%lld\n", ans); } return 0;}
Analysis: The first question of the LCA multiplication method, you need to set a pre to represent where you were previously, and then for the node you currently need to access, the distance between the pre and U is good, through the LCA to ask
#7 div1 A breadth-first Search by Foxpower online LCA (multiplication), analog