TopCoder SRM 561 Div 1 - Problem 1000 Orienteering

傳送門:https://284914869.github.io/AEoj/561.html

題目簡述:

題外話:

剛開始看題沒看到|C|<=300。覺得|C|^2能作,碼了很久,但始終解決不了一棵樹中多條直徑去重的問題。後來才發現|C|<=300,暴力就能夠了。html

不知道有哪位大佬會|C|^2的作法😀ios

思路:

很顯然,若length爲樹中通過全部S中的點的最短路徑長度,git

若size爲虛樹中全部邊的長度和,github

若dis爲虛樹中的最遠點對的距離(即直徑長度)數組

那麼length=size*2-dis。spa

直觀感覺就是這樣的,無需證實😜3d

那麼只要求出size的指望,dis的指望便可。code

size的指望很好求,只要求出每條邊對size的貢獻便可。htm

對於當前邊,一端的聯通塊中有a個點屬於C,另外一端的聯通塊中有|C|-a個點屬於C。blog

那麼這條邊對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
相關文章
相關標籤/搜索