Travel Plan __ Tree DP

Source: Internet
Author: User
There are N cities in a country, numbered 0 to N-1, they are connected by a N-1 road, the road is two-way, you can reach any city along the road. You have a travel plan, this plan is to start with the city numbering K, go to a city you haven't been to every day, and go through a city that's not visited as much as possible (if there are 2 routes, the number of cities that have not been there are as many as the least numbered) until all the cities have gone sightseeing. Now give a map of traffic between cities T, and departure location K, you design a travel plan to meet the above conditions. For example:
(K = 2)

The 1th day from 2 to 0 (cities 1 and 0 become gone) 2nd days from 0 to 6 (Cities 4 and 6 become gone) 3rd days from 6 to 3 (City 3 to Gone) 4th days from 3 to 5 (city 5 becomes gone)
The input data for the above illustration are: 0 1 2 2 1 4. A total of 7 nodes, in addition to node 0, a total of 6 rows of data. The 1th number 0 indicates that 1 to 0 have 1 roads. The 2nd number 1 indicates that 2 to 1 have 1 roads. Input
Line 1th: 2 number n,k (1 <= N <= 50000, 0 <= K <= N-1)
2-n + 1 lines: one number per line, representing the path between nodes.
Output
The road map for outbound travel, that is, the number of cities arriving daily.
Input sample
7 2
0
1
2
2
1 4
Output sample
2
0
6
3

5

#include <iostream> #include <cstdio> #include <algorithm> #include <cstring> #include <

Vector> using namespace std;

const int MAXN = 1e5 + 10;

struct Node {int to, Prev;} EDGE[MAXN];
int tot = 0;

int HEAD[MAXN], DEEP[MAXN];
    void Add (int u, int v) {tot++;
    Edge[tot].to = v;
    Edge[tot].prev = Head[u];
Head[u] = tot;

    } struct Node2 {int num, depth, CNT;
        Node2 (int n, int d, int c = 0): num (n), depth (d), CNT (c) {} BOOL operator < (const NODE2 &b) Const {
        if (depth = = b.depth) {return num < B.num;
    return depth > b.depth;

}
};

Vector<node2> vn2;

int PRE[MAXN];
    void Dfs (int x, int pr) {pre[x] = PR;
    BOOL flag = FALSE;
        for (int i = head[x]; i > 0; i = edge[i].prev) {int v = edge[i].to;
        if (v = = PR) {continue;
        else {flag = true; } Deep[v] =DEEP[X] + 1;
    DFS (v, x);
    } if (!flag) {Vn2.push_back (Node2 (x, deep[x));
	BOOL CMP (Node2 A, Node2 b) {if (a.cnt = = b.cnt) {return a.num < B.num;
return a.cnt > b.cnt;

} bool VIS[MAXN];
    int main () {int n, K;

    CIN >> N >> K;
    int V;
        for (int i = 1; i < n; i++) {cin >> V;
        Add (V, i);
    Add (i, v);
    } Deep[k] = 1;
    Vis[k] = true;

    DFS (k,-1);           
    Sort (Vn2.begin (), Vn2.end ());
        for (int i = 0; i < vn2.size (); i++) {Node2 leaf = vn2[i];
        int num = Leaf.num;
        int cnt = 0;
                while (Num!=-1) {if (!vis[num]) {cnt++;
            Vis[num] = true;
            } else {break;
        num = Pre[num];
    } vn2[i].cnt = cnt;
    } cout << k << Endl; Sort (Vn2.begin (), VN2.End (), CMP); for (int i = 0; i < vn2.size (); i++) {if (vn2[i].cnt) {cout << vn2[i].num <& Lt
        Endl
} 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.