Bzoj 3522 POI Hotel tree DP

Source: Internet
Author: User

Main topic

Give a tree, ask to choose three points, so that the distance between the three points equal to the scheme of how many kinds.

Ideas

These three points must not be a chain again, then it is certainly possible to determine a central point, so that the distance between the three points to the center point is equal.
Then we can enumerate the center point and count it for each depth. Although it looks like a O ( n 3 ) , but it's a running flight.

CODE
#define _crt_secure_no_warnings#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>#define MAX 5010using namespace STD;intPointsintHead[max], total;int_next[max <<1], Aim[max <<1];inline voidADD (intXintY) {_next[++total] = head[x];    Aim[total] = y; HEAD[X] = total;}intDeep[max];intNowvoidDFS (intXintLastintd) {++deep[d]; for(inti = head[x]; I i = _next[i]) {if(Aim[i] = = last)Continue; DFS (Aim[i], X, D +1); }}Long LongCnt[max], Ans[max];intMain () {Cin>> points; for(intX, y, i =1; I < points; ++i) {scanf("%d%d", &x, &y);    Add (x, y), add (y, x); }Long LongOutput =0; for(intx =1; x <= points; ++X) {memset(CNT,0,sizeof(CNT));memset(ANS,0,sizeof(ans)); for(inti = head[x]; I i = _next[i]) {memset(Deep,0,sizeof(deep)); DFS (Aim[i], X,1); for(intD =1; D <= points;                ++D) {output + = ans[d] * Deep[d];                Ans[d] + = cnt[d] * Deep[d];            Cnt[d] + = deep[d]; }        }    }cout<< output << Endl;return 0;}

Bzoj 3522 POI Hotel tree DP

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.