-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathTwo Permutations (Hard Version)
More file actions
73 lines (70 loc) · 1.74 KB
/
Two Permutations (Hard Version)
File metadata and controls
73 lines (70 loc) · 1.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
include <iostream>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <random>
#include <stack>
#include <queue>
#include <deque>
#include <algorithm>
#include <numeric>
#include <set>
#include <map>
#include <string>
#include <tuple>
#include <vector>
#include <cassert>
#include <functional>
//#define int long long
using namespace std;
double coords[5005][5005];
void solve()
{
int n, m; cin >> n >> m;
vector<vector<int>> adj(n+1);
for (int j = 0; j < m; j++) {
int u, v; cin >> u >> v;
adj[u].push_back(v);
}
vector<double> dp(n+1); dp[n] = 1;
for (int i = n - 1; i >= 1; i--) {
if (adj[i].empty()) {
dp[i] = 0;
continue;
}
sort(adj[i].begin(), adj[i].end(), [&](int a, int b){return dp[a] > dp[b];});
for (int j = 0; j < (int)adj[i].size(); j++) {
dp[i] += dp[adj[i][j]] * coords[adj[i].size()][j+1];
}
}
cout.precision(12);
cout << fixed;
cout << dp[1] << '\n';
}
signed main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
#endif
for (int p = 1; p <= 5000; p += 2) {
for (int i = 1; i <= p; i++) coords[p][i] = (double)1/p;
}
for (int p = 2; p <= 5000; p += 2) {
for (int i = 1; i <= p; i++) {
if (i == 1) {
coords[p][i] = (double)1/p;
}
else if (i == p) {
coords[p][i] = 0;
}
else {
coords[p][i] = (double)(i-2)/p * coords[p-2][i-2] + (double)(p-i)/p * coords[p-2][i-1];
}
}
}
ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
int T; cin >> T; while (T--)
solve();
}