Skip to content

Commit 5cde89f

Browse files
committed
Solved problem 839C from codeforces
1 parent c11d855 commit 5cde89f

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed

CodeForces/839C. Journey.cpp

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
Idea:
3+
- DFS and calculate the answer.
4+
*/
5+
6+
#include <bits/stdc++.h>
7+
8+
using namespace std;
9+
10+
int const N = 1e5 + 1;
11+
bool vis[N];
12+
int n;
13+
vector<vector<int> > g;
14+
15+
double rec(int u, int p) {
16+
vis[u] = true;
17+
18+
double ret = 0, prop = 1.0 / (g[u].size() - (p != -1));
19+
for(int i = 0, v; i < g[u].size(); ++i) {
20+
v = g[u][i];
21+
22+
if(!vis[v])
23+
ret += (rec(v, u) + 1) * prop;
24+
}
25+
26+
return ret;
27+
}
28+
29+
int main() {
30+
scanf("%d", &n);
31+
g.resize(n);
32+
for(int i = 0, a, b; i < n - 1; ++i) {
33+
scanf("%d %d", &a, &b);
34+
--a, --b;
35+
g[a].push_back(b);
36+
swap(a, b);
37+
g[a].push_back(b);
38+
}
39+
40+
printf("%.10lf\n", rec(0, -1));
41+
42+
return 0;
43+
}

CodeForces/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,7 @@
378378
- [837A. Text Volume](http://codeforces.com/contest/837/problem/A)
379379
- [837B. Flag of Berland](http://codeforces.com/contest/837/problem/B)
380380
- [837C. Two Seals](http://codeforces.com/contest/837/problem/C)
381+
- [839C. Journey](http://codeforces.com/contest/839/problem/C)
381382
- [842A. Kirill And The Game](http://codeforces.com/contest/842/problem/A)
382383
- [842B. Gleb And Pizza](http://codeforces.com/contest/842/problem/B)
383384
- [845A. Chess Tourney](http://codeforces.com/contest/845/problem/A)

0 commit comments

Comments
 (0)