From a504cfa2ffc955d4743c4ad09f8ebbf72018e977 Mon Sep 17 00:00:00 2001 From: cyp0633 Date: Tue, 27 Jul 2021 22:08:19 +0800 Subject: [PATCH] =?UTF-8?q?CSP=205-6=20=E6=97=85=E8=A1=8C=E7=9A=84?= =?UTF-8?q?=E6=9C=9F=E6=9C=9B=E5=80=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CSP-Training/5/6-journey.cpp | 53 ++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 CSP-Training/5/6-journey.cpp diff --git a/CSP-Training/5/6-journey.cpp b/CSP-Training/5/6-journey.cpp new file mode 100644 index 0000000..3cfede7 --- /dev/null +++ b/CSP-Training/5/6-journey.cpp @@ -0,0 +1,53 @@ +//CF893C Journey +#include +#include +using namespace std; +bool visited[100001]; +int head[100001], nxt[200000], to[200000], n, sons[100001]; +double expectation; +void dfs(int currentPos, double possibility, int depth); +void insert(int source, int dest, int i); +int main() +{ + scanf("%d", &n); + int tempSource, tempDest; + for (int i = 0; i < n - 1; i++) + { + scanf("%d %d", &tempSource, &tempDest); + insert(tempSource, tempDest, i * 2 + 1); + insert(tempDest, tempSource, i * 2 + 2); + } + sons[1] /= 2; + for (int i = 2; i <= n; i++) //除根节点外,子节点数=连接边数-1 + { + sons[i] = sons[i] / 2 - 1; + } + dfs(1, 1.0, 0); + printf("%.7lf\n", expectation); //HNU CG强制要求7位小数,不然不过 +} +void dfs(int currentPos, double possibility, int depth) +{ + if (visited[currentPos]) + { + return; + } + if (sons[currentPos] == 0) + { + expectation += possibility * depth; + return; + } + visited[currentPos] = true; + double sonPossibility = possibility / sons[currentPos]; + for (int i = head[currentPos]; i != 0; i = nxt[i]) + { + dfs(to[i], sonPossibility, depth + 1); + } +} +void insert(int source, int dest, int i) +{ + nxt[i] = head[source]; + to[i] = dest; + head[source] = i; + sons[source]++; + sons[dest]++; +} \ No newline at end of file