Codeforces161d-distance in tree-like DP

Source: Internet
Author: User

Question: give you a tree and ask you how many conditions the distance between the trees is K.

Solution: tree-like DP maintains the depth of each node (1-K,

Solution code:

1 // file name: 161d. CPP 2 // Author: darkdream 3 // created time: sunday, September 11, August 03, 2014 4 5 # include <vector> 6 # include <list> 7 # include <map> 8 # include <set> 9 # include <deque> 10 # include <stack> 11 # include <bitset> 12 # include <algorithm> 13 # include <functional> 14 # include <numeric> 15 # include <utility> 16 # include <sstream> 17 # include <iostream> 18 # include <iomanip> 19 # include <cstdio> 20 # include <cmath> 2 1 # include <cstdlib> 22 # include <cstring> 23 # include <ctime> 24 # define ll long long25 26 using namespace STD; 27 28 vector <int> A [50004]; 29 int M [50005] [500]; 30 int visit [50004]; 31 int N, K; 32 ll ans = 0; 33 void DFS (int t) 34 {35 int lT = A [T]. size (); 36 m [T] [0] = 1; 37 for (INT I = 0; I <lt; I ++) 38 {39 int TT = A [T] [I]; 40 if (! Visit [TT]) 41 {42 visit [TT] = 1; 43 DFS (TT); 44 for (Int J = 0; j <K; j ++) 45 ans + = m [T] [k-j-1] * m [TT] [J]; 46 for (Int J = 0; j <K; j ++) 47 {48 m [T] [J + 1] + = m [TT] [J]; 49} 50} 51} 52} 53 int main () {54 scanf ("% d", & N, & K); 55 int Ta, TB; 56 memset (M, 0, sizeof (m )); 57 memset (visit, 0, sizeof (visit); 58 for (INT I = 1; I <= n-1; I ++) 59 {60 scanf ("% d", & TA, & TB); 61 A [Ta]. push_back (TB); 62 A [TB]. push_back (TA); 63} 64 visit [1] = 1; 65 DFS (1); 66 printf ("% i64d \ n", ANS); 67 return 0; 68}
View code

 

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.