Governance of POJ 1741 trees

Source: Internet
Author: User

The number of points on the tree whose distance is less than or equal to K

The n2 algorithm is definitely not good, because points

This requires sub-governance. For more information, see the paper of lacquer film.

This question is about the division of points.

An important problem is that, to prevent degradation, we need to find the center of gravity of the tree and divide it down. The so-called center of gravity is to delete this node, the number of Tree nodes with the most remaining nodes is the minimum.

For each division, we first calculate the center of gravity. to calculate the center of gravity, we need to perform two dfs operations. For the first time, we need to calculate the size of the subtree with each node as the root, and the second is to find the center of gravity from these nodes.

After finding the center of gravity, you need to calculate the distance from all nodes to the center of gravity to see how many pairs are less than or equal to K. The method used here is to store all the distances in an array for fast sorting, this is nlogn, and then it is solved in a classic opposite search O (n) time. However, only the vertices of the paths that pass through the center of gravity in the result satisfying the conditions smaller than or equal to K are valid. That is to say, the values on the same subtree are definitely not counted. Therefore, for each subtree, subtract the vertex that satisfies the condition in the subtree.

The final complexity is n logn, where each fast sorting is nlogn, And the recursive depth is logn.


[Cpp]
# Include <iostream>
# Include <algorithm>
# Include <cstring>
# Include <string>
# Include <cstdio>
# Include <cmath>
# Include <queue>
# Include <map>
# Include <set>
# Define eps 1e-5
# Define maxn11111
# Define MAXM 55555
# Define INF 1000000000
Using namespace std;
Struct EDGE
{
Int v, next, w;
} Edge [MAXM];
Int head [MAXN], e;
Int n, k, vis [MAXN], ans, root, num;
Void init ()
{
Memset (vis, 0, sizeof (vis ));
Memset (head,-1, sizeof (head ));
E = ans = 0;
}
Void add (int u, int v, int w)
{
Edge [e]. v = v;
Edge [e]. w = w;
Edge [e]. next = head [u];
Head [u] = e ++;
}
Int mx [MAXN], size [MAXN], mi, dis [MAXN];
Void dfssize (int u, int fa) // process the size of the subtree
{
Size [u] = 1;
Mx [u] = 0;
For (int I = head [u]; I! =-1; I = edge [I]. next)
{
Int v = edge [I]. v;
If (v! = Fa &&! Vis [v])
{
Dfssize (v, u );
Size [u] + = size [v];
If (size [v]> mx [u]) mx [u] = size [v];
}
}
}
Void dfsroot (int r, int u, int fa) // calculate the center of gravity
{
If (size [r]-size [u]> mx [u]) mx [u] = size [r]-size [u];
If (mx [u] <mi) mi = mx [u], root = u;
For (int I = head [u]; I! =-1; I = edge [I]. next)
{
Int v = edge [I]. v;
If (v! = Fa &&! Vis [v]) dfsroot (r, v, u );
}
}
Void dfsdis (int u, int d, int fa) // calculate the distance
{
Dis [num ++] = d;
For (int I = head [u]; I! =-1; I = edge [I]. next)
{
Int v = edge [I]. v;
If (v! = Fa &&! Vis [v]) dfsdis (v, d + edge [I]. w, u );
}
}
Int calc (int u, int d)
{
Int ret = 0;
Num = 0;
Dfsdis (u, d, 0 );
Sort (dis, dis + num );
Int I = 0, j = num-1;
While (I <j) // classic
{
While (dis [I] + dis [j]> k & I <j) j --;
Ret + = j-I;
I ++;
}
Return ret;
}
Void dfs (int u)
{
Mi = n;
Dfssize (u, 0 );
Dfsroot (u, u, 0 );
Ans + = calc (root, 0 );
Vis [root] = 1;
For (int I = head [root]; I! =-1; I = edge [I]. next)
{
Int v = edge [I]. v;
If (! Vis [v])
{
Ans-= calc (v, edge [I]. w );
Dfs (v );
}
}
}
Int main ()
{
While (scanf ("% d", & n, & k )! = EOF)
{
If (! N &&! K) break;
Init ();
Int u, v, w;
For (int I = 0; I <n-1; I ++)
{
Scanf ("% d", & u, & v, & w );
Add (u, v, w );
Add (v, u, w );
}
Dfs (1 );
Printf ("% d \ n", ans );
}
Return 0;
}

# Include <iostream>
# Include <algorithm>
# Include <cstring>
# Include <string>
# Include <cstdio>
# Include <cmath>
# Include <queue>
# Include <map>
# Include <set>
# Define eps 1e-5
# Define maxn11111
# Define MAXM 55555
# Define INF 1000000000
Using namespace std;
Struct EDGE
{
Int v, next, w;
} Edge [MAXM];
Int head [MAXN], e;
Int n, k, vis [MAXN], ans, root, num;
Void init ()
{
Memset (vis, 0, sizeof (vis ));
Memset (head,-1, sizeof (head ));
E = ans = 0;
}
Void add (int u, int v, int w)
{
Edge [e]. v = v;
Edge [e]. w = w;
Edge [e]. next = head [u];
Head [u] = e ++;
}
Int mx [MAXN], size [MAXN], mi, dis [MAXN];
Void dfssize (int u, int fa) // process the size of the subtree
{
Size [u] = 1;
Mx [u] = 0;
For (int I = head [u]; I! =-1; I = edge [I]. next)
{
Int v = edge [I]. v;
If (v! = Fa &&! Vis [v])
{
Dfssize (v, u );
Size [u] + = size [v];
If (size [v]> mx [u]) mx [u] = size [v];
}
}
}
Void dfsroot (int r, int u, int fa) // calculate the center of gravity
{
If (size [r]-size [u]> mx [u]) mx [u] = size [r]-size [u];
If (mx [u] <mi) mi = mx [u], root = u;
For (int I = head [u]; I! =-1; I = edge [I]. next)
{
Int v = edge [I]. v;
If (v! = Fa &&! Vis [v]) dfsroot (r, v, u );
}
}
Void dfsdis (int u, int d, int fa) // calculate the distance
{
Dis [num ++] = d;
For (int I = head [u]; I! =-1; I = edge [I]. next)
{
Int v = edge [I]. v;
If (v! = Fa &&! Vis [v]) dfsdis (v, d + edge [I]. w, u );
}
}
Int calc (int u, int d)
{
Int ret = 0;
Num = 0;
Dfsdis (u, d, 0 );
Sort (dis, dis + num );
Int I = 0, j = num-1;
While (I <j) // classic
{
While (dis [I] + dis [j]> k & I <j) j --;
Ret + = j-I;
I ++;
}
Return ret;
}
Void dfs (int u)
{
Mi = n;
Dfssize (u, 0 );
Dfsroot (u, u, 0 );
Ans + = calc (root, 0 );
Vis [root] = 1;
For (int I = head [root]; I! =-1; I = edge [I]. next)
{
Int v = edge [I]. v;
If (! Vis [v])
{
Ans-= calc (v, edge [I]. w );
Dfs (v );
}
}
}
Int main ()
{
While (scanf ("% d", & n, & k )! = EOF)
{
If (! N &&! K) break;
Init ();
Int u, v, w;
For (int I = 0; I <n-1; I ++)
{
Scanf ("% d", & u, & v, & w );
Add (u, v, w );
Add (v, u, w );
}
Dfs (1 );
Printf ("% d \ n", ans );
}
Return 0;
}


 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.