# (Tree moving Regulation) Luo Gu P3174 [HAOI2009] caterpillar (provincial election / NOi-)

Title Description

For a tree, we can be a strand and the strand sides connected to the out come into looks like a caterpillar, the more points, the greater the caterpillar. Such as trees left figure (Fig. 1) extracting a portion of the right side becomes a caterpillar (FIG. 2).

Input Format

In the first line of the text file worm.in two integers N, M, respectively, the number of edges and the number of nodes in the tree, the tree.

Next M rows, each row two integers a, b represent points a and b have connecting edges (a, b ≤ N). You can not assume that a pair of identical (a, b) appears more than once.

Output Format

An integer is written in a text file in worm.out, it represents the size of the largest caterpillars.

Sample input and output

Input # 1
13 12 
1 2 
1 5 
1 6 
3 2 
4 2 
5 7 
5 8 
7 9 
7 10 
7 11 
8 12 
8 13 
Output # 1
11

Description / Tips

40% of the data, N ≤ 50000

100% of the data, N ≤ 300000

problem analysis:

Equivalent to give you a tree, the tree to find a chain to meet the chain length and the number of sub-strand of the chain and the biggest!

Set F [i] [j] represents the degree of node i from node j to the maximum number of sub-chains, for any node f [i] [i] = c [i], c array storage node i

For node i and its sons: f [i] [ai] = c [i] + c [ai] -1;

For any node k on the point i, j chains, f [i] [j] = f [i] [k] + f [k] [j] -sz [k];

Tree DP? I was too dishes do not how to do qwq

First push a wave equation

The right to make the point  A [i] A [ i ] for the penetration point, then a chain of weight (answer) apparently  \ SUM A [i] sigma A [ i ] (?)

Then we find that each edge in the chain is overcharged once, set  S S for the chain length, the total overcharged  S. 1- S - . 1 edges

Because the answer is seeking the point, so it would go  + 1 + 1

and sotrulyThe answer is  \ A SUM [I] - (. 1-S) + 1'd [Sigma A [ I ] - ( S - . 1 ) + . 1

emm .... it seems still a little trouble,Then the equation of technology

\sum a[i]-(s-1)+1 == \sum a[i]-s+2 == \sum(a[i]-1)+2a[i](s1)+1==a[i]s+2==(a[i]1)+2

According to this formula, we have the right to all points  -1 - 1, then  \ SUM (A [I] -1) [Sigma ( A [ I ] - . 1 the length of the chain is selected)

thenPleasant mannerRun twice  DFS (BFS) D F S ( B F S ) required the longest path on the line, time and space complexity of  O (n-) O ( n- ) (Code Chicken good writing qwq)

Code:

#include<bits/stdc++.h>
struct E{int u,nxt;}e[600001];
int p[300001],a[300001],num,mx;
inline void add(int x,int y){e[++num]=(E){y,p[x]},p[x]=num;}
inline void dfs(int fa,int s,int dis){
if(dis>mx) mx=dis,num=s;
for(int i=p[s];i;i=e[i].nxt) if(e[i].u!=fa) dfs(s,e[i].u,dis+a[s]);
}
int main(){
int n,m,x,y;
scanf("%d%d",&n,&m),memset(a,-1,sizeof(a));
for(int i=1;i<n;i++) scanf("%d%d",&x,&y),add(x,y),add(y,x),a[x]++,a[y]++;
dfs(0,1,a[1]),mx=0,dfs(0,num,a[num]),printf("%d",mx+2);
return 0;
}

 

Guess you like

Origin www.cnblogs.com/little-cute-hjr/p/11449457.html