luogu P2018 消息传递
Posted ZlycerQan
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了luogu P2018 消息传递相关的知识,希望对你有一定的参考价值。
二次联通门 : luogu P2018 消息传递
/* luogu P2018 消息传递 树形dp 原来用优先队列做了一下, T了俩点 MMP 去看正解。。 复杂度一样好不好。。 每次到达一个点,记录其子树中所有的dp值 优先向大的一边转移 */ #include <cstdio> #include <iostream> #include <algorithm> #include <cstring> #define INF 1e8 const int BUF = 12312323; char Buf[BUF], *buf = Buf; inline void read (int &now) { for (now = 0; !isdigit (*buf); ++ buf); for (; isdigit (*buf); now = now * 10 + *buf - ‘0‘, ++ buf); } #define Max 1100 struct E { E *n; int to; }; E *list[Max], poor[Max << 1], *Tail = poor; int father[Max], dp[Max]; inline int max (int a, int b) { return a > b ? a : b; } inline int min (int a, int b) { return a < b ? a : b; } void Dp (int now, int Father) { int data[Max], C = 0; father[now] = Father; for (E *e = list[now]; e; e = e->n) if (e->to != Father) { Dp (e->to, now); data[++ C] = dp[e->to]; } std :: sort (data + 1, data + 1 + C); for (int i = 1; i <= C; ++ i) dp[now] = max (dp[now], data[i] + C - i + 1); } int Answer[Max]; int Main () { fread (buf, 1, BUF, stdin); int Maxn = INF; int N, M; read (N); register int i; int x; for (i = 2; i <= N; ++ i) { read (x); ++ Tail, Tail->to = i, Tail->n = list[x], list[x] = Tail; ++ Tail, Tail->to = x, Tail->n = list[i], list[i] = Tail; } for (i = 1; i <= N; ++ i) { Dp (i, 0); Answer[i] = dp[i]; Maxn = min (Maxn, Answer[i]); memset (dp, 0, sizeof dp); } printf ("%d\n", Maxn + 1); for (i = 1; i <= N; ++ i) if (Answer[i] == Maxn) printf ("%d ", i); return 0; } int ZlycerQan = Main (); int main (int argc, char *argv[]) {;}
以上是关于luogu P2018 消息传递的主要内容,如果未能解决你的问题,请参考以下文章