传送门:https://284914869.github.io/AEoj/561.html
题目简述:
题外话:
刚开始看题没看到|C|<=300。以为|C|^2能做,码了好久,但始终解决不了一棵树中多条直径去重的问题。后来才发现|C|<=300,暴力就可以了。
不知道有哪位大佬会|C|^2的做法??
思路:
很显然,若length为树中经过所有S中的点的最短路径长度,
若size为虚树中所有边的长度和,
若dis为虚树中的最远点对的距离(即直径长度)
那么length=size*2-dis。
直观感受就是这样的,无需证明??
那么只要求出size的期望,dis的期望即可。
size的期望很好求,只要求出每条边对size的贡献即可。
对于当前边,一端的联通块中有a个点属于C,另一端的联通块中有|C|-a个点属于C。
那么这条边对size的贡献是
接着是求出dis的期望。
一个很直观的想法是枚举直径,求出可能存在于这棵虚树中的点的数量。
由于一棵树可能有多个直径,我们取其中端点标号字典序最小的一个直径。
假设当前枚举的是直径a-b。c可能存在与这棵虚树中,当且仅当
且
这里pair(x1,y1)<pair(x2,y2)指的是x1<x2||x1==x2&&y1<y2
所以先预处理出dis数组,暴力枚举直径,暴力枚举每一个点判断是否合法,再求贡献即可。
时间复杂度O(|C|^3)
一些需要注意的细节:直接用组合数进行计算会出锅,因为组合数太大了。所以用另一种方式计算(详见代码)
代码:
#include <cstdio> #include <string> #include <vector> #include <cstring> #include <iostream> #include <algorithm> using namespace std; #define _CLASSNAME_ Orienteering #define _METHODNAME_ expectedLength #define _RC_ double #define _METHODPARMS_ vector <string> mp, int k #define ref(i,x,y)for(int i=x;i<=y;++i) #define def(i,x,y)for(int i=x;i>=y;--i) #define mp make_pair #define fi first #define se second #define pb push_back typedef long long LL; typedef pair<int, int> PII; char Mp[51][51]; int n, m, K, tot, Tot, id[51][51]; vector<int> E[2501]; void add(int x, int y) { if (!x || !y)return; //cout << x << " " << y << endl; E[x].pb(y); E[y].pb(x); } int sz[2501]; void initmap() { ref(i, 1, 2500)E[i].clear(); memset(id, 0, sizeof id); tot = 0; Tot = 0; ref(i, 1, n)ref(j, 1, m)if (Mp[i][j] == ‘*‘)id[i][j] = ++tot; Tot = tot; ref(i, 1, n)ref(j, 1, m)if (Mp[i][j] == ‘.‘)id[i][j] = ++Tot; ref(i, 1, n)ref(j, 1, m) { if (i < n)add(id[i][j], id[i + 1][j]); if (j < m)add(id[i][j], id[i][j + 1]); } n = Tot; } void dfs(int fa, int x) { if (E[x].empty())return; sz[x] = (x <= tot); ref(i, 0, E[x].size() - 1) { int y = E[x][i]; if (y == fa)continue; dfs(x, y); sz[x] += sz[y]; } } double work1() { dfs(0, 1); double res = 0; ref(i, 2, n) { res++; int a = sz[i], b = tot - a; if (K <= a) {//c[a][K]/c[tot][K] double ss = 1.0; ref(j, 0, K - 1)ss = ss * (a - j) / (tot - j); res -= ss; } if (K <= b) {//c[b][K]/c[tot][K] double ss = 1.0; ref(j, 0, K - 1)ss = ss * (b - j) / (tot - j); res -= ss; } } return res; } int dis[301][301]; void Dfs(int fa, int x, int S) { if (x <= tot)dis[0][x] = S; ref(i, 0, E[x].size() - 1) { int y = E[x][i]; if (y == fa)continue; Dfs(x, y, S + 1); } } double work2() { double res = 0.0; ref(i, 1, tot) { Dfs(0, i, 0); ref(j, 1, tot)dis[i][j] = dis[0][j]; } ref(i, 1, tot)ref(j, i + 1, tot) { int ss = 0; ref(k, 1, tot)if (k != i && k != j) { if (mp(dis[k][j], -k) < mp(dis[i][j], -i)) if (mp(dis[i][k], -k) < mp(dis[i][j], -j)) ++ss; } if (ss < K - 2)continue; //c[ss][K-2] / c[tot][k] double S = 1.0*dis[i][j] / (tot - K + 1) / (tot - K + 2) * K * (K - 1); ref(k, 0, K - 2 - 1)S = S*(ss - k) / (tot - k); res += S; } return res; } class _CLASSNAME_ { public: _RC_ _METHODNAME_(_METHODPARMS_) { n = mp.size(); m = mp[0].size(); K = k; ref(i, 1, n)ref(j, 1, m)Mp[i][j] = mp[i - 1][j - 1]; initmap(); double ans1 = work1(); double ans2 = work2(); return _RC_(ans1 * 2 - ans2); } // BEGIN CUT HERE public: void run_test(int Case) { if ((Case == -1) || (Case == 0)) test_case_0(); if ((Case == -1) || (Case == 1)) test_case_1(); if ((Case == -1) || (Case == 2)) test_case_2(); if ((Case == -1) || (Case == 3)) test_case_3(); if ((Case == -1) || (Case == 4)) test_case_4(); } private: template <typename T> string print_array(const vector<T> &V) { ostringstream os; os << "{ "; for (typename vector<T>::const_iterator iter = V.begin(); iter != V.end(); ++iter) os << ‘\"‘ << *iter << "\","; os << " }"; return os.str(); } void verify_case(int Case, const double &Expected, const double &Received) { cerr << "Test Case #" << Case << "..."; if (Expected == Received) cerr << "PASSED" << endl; else { cerr << "FAILED" << endl; cerr << "\tExpected: \"" << Expected << ‘\"‘ << endl; cerr << "\tReceived: \"" << Received << ‘\"‘ << endl; } } void test_case_0() { string Arr0[] = { "*#..#", ".#*#.", "*...*" }; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 2; double Arg2 = 3.8333333333333353; verify_case(0, Arg2, expectedLength(Arg0, Arg1)); } void test_case_1() { string Arr0[] = { "*#..#", ".#*#.", "*...*" }; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 4; double Arg2 = 8.0; verify_case(1, Arg2, expectedLength(Arg0, Arg1)); } void test_case_2() { string Arr0[] = { "#.#**", "....#", "#*#**", "**#*#", "#..##", "*#..#", ".#.#.", "....*" }; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 3; double Arg2 = 10.825000000000024; verify_case(2, Arg2, expectedLength(Arg0, Arg1)); } void test_case_3() { string Arr0[] = { "###################", "#*###############*#", "#.....#######.....#", "#*###*.#.*.#.*###*#", "#*####*.*#*.*####*#", "#*#####*###*#####*#", "###################" }; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 9; double Arg2 = 30.272233648704244; verify_case(3, Arg2, expectedLength(Arg0, Arg1)); } void test_case_4() { string Arr0[] = { "**##*.**#..#.*...*#...*#..#.##..#..#.#*...#.##*##.", ".#..###..#..#.#.##..#.#.*#.*..#..#.#*..##.#*...*..", "..#.....###.#*.##..#.#.#*..#.#..#....#..#...#*####", ".#.##*#.*#..#*#*.#.#...*.#.*#.#.##.#*.##.#.#..*...", "..*.*#*.###.#..#.#..##.##.*#..#.....#.....#..#.#.#", ".#.##.#..##..*#..#.#...#*##*#*..#.#.#.#.##.##.#.#*", "..##....#..#.#*#...*.##...#.#.####...#.#*.....#...", ".#.*#.##.*#*.#*.#.#.#..#.#..#.#*#.###..##.##.#.##*", ".*.#*..*.#.#...#.*##.#.**.#.*...**..*#..#.#.#*.#..", ".#*.#*##....##.#.#*..*.###.#.##.##.#.#.#....#.#*.#", "*.#..#*#.#*#*....#.#.#..*#**...##.#.#.**#*##.*.#..", ".#*.##..##..##.#.#..#.#.###.###...#...#*#..##*#.#.", "#..#*.#..*.###..#.#...#.###.#.#*#.#.#**##.#...*.#*", "..#..#.#.##.#..#.**.##*#.#**.**..#.#..#...#.##*#..", ".#*#.#.*..#.*#...#.#...#...#.##.#..*#*.##*....###.", ".*.#.#.#.#*#..*##.**.##*##..#.*#.#*###..*.#.##.#..", ".#......#...#.#.*#.#.#..#..#.#*#....#*.#*#.*#..*.#", "#..####..#*#...#*.#..#.###...#.#.#.###*#..##*##.#.", ".#.*..#.#...#.#..#.##...#..#.#.#.#.###..##..*.*.*.", ".#.#.#.#..##.*..#.*.#.##.#..##*...#.#..#.#.##.#.##", ".#..#*.#.#..#.##..##..#.*..#.*#.#...##....#...###.", ".#.#.#.#*.#.#..#.#..#..#.#.*#...#.##...#.##.##.*..", ".#...#.#.##.#.#..*#.*#..###..#.#.#*###.##...#*.##.", ".#.##.*.......*.#.*#.#.#*###..*...*..#.*.##.#.#..#", "...###*####*#.#..##*...#..#..##.#.#.#..##*#*.*.*#.", "#.#.#....*#..#.#.#.#.##..#*.#...#..#.#*#...#.##.*.", "..*.#*##.#.#*#.###...#..##.#.#.#*###*#.*#.#.*###.#", "##*##..##...#.....##.#.#.**#..#*.....##.#..#*.#.*.", ".....#.*.##..##.##*.*#...#.#.#.##.#*#.**..#..#.#.#", "##.#.#*##.#.#.*.*.#.#*#.#.#....*...#*##*##.#....#.", "*.**#**....*..##.#*.*.**..##.###.##.....##...##.**", "#.####.##*#*##..#.*#*#.##*...#.##..#.##....#*..##.", "....#...##.#...#*.#..##.##.#*..*.#....##.#.*##...#", "#.#..*##*..#.#..#..#..#*....#.##..##.#*##.##.*##..", "..#.#*.*.##.#.#*#.#*##.###.##...#............#*.#.", "#.#.##.#....*....*..##..*#.#.#.###.#.#.#.###..#..#", ".#**..#*#.#*#*#.#.#...*##....##.#*..#..#*..*#..#..", "...#*#.....#..#.#..#*#.*##.#..#.#.##..#.*#*#.#...#", ".#*.###.#.#.#.#.*#*##.##..#.#*..#...#.#.#..#*.*#..", "#*.#.#.#..#..#..#....*#.*##..##.#.#..#...##.#.#..#", "*.#..#..#...#..##.#*#..#.#*#.#.#.###..#.#*...#.#..", "#...#.#...#.#.#..#.*.#*.....**.*..#*##.#*.##....##", "#*#....#*#..#.*.###*#..#*##.##.#.#...#.*.##.##.##.", "..##*##*..#*#.#..#*.*##*.##.#...#.#.#.#.#..*#.##..", "#...#*##.#*#**.##.*#.*.##..*.#*#**....#**##...*.*#", "*#.##......*#.##.#.#.##**.#.#.#.#.#.##..#...#*#*#*", "*....##.#.#..#.....#..##.#....*....#.#.##.#.#.##**", "#.##*#...#..#.#.##..#..##.##.##.##........##.#*#.#", "..#...#.#*#*..*#..*#.*#.#......##.#.#.#*#..#..****", ".###.#..#...#.#..#..#.#...#.#.#...**.#..*#*.*##*#." }; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 150; double Arg2 = 1309.4951033725558; verify_case(4, Arg2, expectedLength(Arg0, Arg1)); } // END CUT HERE }; // BEGIN CUT HERE int main() { _CLASSNAME_ user; user.run_test(-1); getchar(); return 0; } // END CUT HERE
时间: 2024-11-08 23:55:51