From 7568f48ded0ff85d88657c937d2334ea96fc2e59 Mon Sep 17 00:00:00 2001 From: cyp0633 Date: Tue, 27 Jul 2021 12:32:54 +0800 Subject: [PATCH] =?UTF-8?q?CSP=205-5=20park=20=E7=8C=AB=E4=B8=8E=E9=A4=90?= =?UTF-8?q?=E5=8E=85=E7=9A=84=E6=95=85=E4=BA=8B=20AC=20on=20Codeforces=20A?= =?UTF-8?q?ll=20WA=20on=20CG?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CSP-Training/5/5-park.cpp | 58 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 CSP-Training/5/5-park.cpp diff --git a/CSP-Training/5/5-park.cpp b/CSP-Training/5/5-park.cpp new file mode 100644 index 0000000..cede9db --- /dev/null +++ b/CSP-Training/5/5-park.cpp @@ -0,0 +1,58 @@ +//猫与餐厅的故事 Kefa and Park +#include +#include +#include +using namespace std; +bool isCat[100001], visited[100001]; +int head[100001], nxt[100000 * 2], to[100000 * 2], n, m, count; +void dfs(int currentPos, int currConsecutiveCat); +void insert(int source, int dest, int i); +int main() +{ + int temp1, temp2; + scanf("%d%d", &n, &m); + for (int i = 1; i <= n; i++) + { + scanf("%d", &temp1); + isCat[i] = (int)temp1; + } + for (int i = 1; i <= n - 1; i++) + { + scanf("%d%d", &temp1, &temp2); + insert(temp1, temp2, i * 2 - 1); + insert(temp2, temp1, i * 2); + } + dfs(1, isCat[1]); + printf("%d\n", count); + return 0; +} +void dfs(int currentPos, int currConsecutiveCat) //当前访问结点下标,当前连续猫数,最大连续猫数 +{ + if (currConsecutiveCat > m || visited[currentPos]) //猫太多或者已经访问过 + { + return; + } + visited[currentPos] = true; //标记结点已访问过 + if (nxt[head[currentPos]] == 0 && currentPos != 1) //是叶子结点,头的下一个就没有了 + { + count++; + return; + } + for (int i = head[currentPos]; i != 0; i = nxt[i]) + { + if (isCat[to[i]]) + { + dfs(to[i], currConsecutiveCat + 1); + } + else + { + dfs(to[i], 0); + } + } +} +void insert(int source, int dest, int i) +{ + nxt[i] = head[source]; + head[source] = i; + to[i] = dest; +} \ No newline at end of file