CF-goodby 2014-D

本文介绍了一种通过计算每条边被选中的概率来解决特定网络问题的方法,并提供了详细的算法思路及C++实现代码。

题目连接 New Year Santa Network


解法:

直接算每个边被选中的概率,

然后直接把每个边的权值*概率相加即是答案.


求每个边选中的概率可以这样求:

假设一共有n个点,那么一共有 C(n, 3)也就是 n*(n-1)*(n-2)/6种选法;

然后对于每一条边,假设他左边有x个节点,右边有y个节点,

那么选择这条边的方法有cnt = C(x, 2)*y + C(y, 2)*x种,

那选到这条边的概率就是 cnt / tot;

上面的整个过程一个DFS就搞定;


而对于每次减少某条边的权值,则直接减去权值差*概率就可以了(可以自己想想)


下面是代码

#include <stdio.h>
#include <iostream>
#include <string.h>
#include <vector>
#include <math.h>
#include <queue>
#include <map>
#include <set>
#include <algorithm>
using namespace std;

#define FOR(i, j, k) for(int i=(j);i<=(k);i++)
#define REP(i, n) for(int i=0;i<(n);i++)
#define mst(x, y) memset(x, y, sizeof(x));
#define pii pair<int, int>
#define fr first
#define sc second
#define left myleft
#define right myright
#define ll long long
#define ull unsigned long long
#define seed 1331
#define mod ((int)1e9+7)
#define eps 1e-5
#define pdd pair<double, double>

int n;
vector <pii> edge[100009];
ll cnt[100009], len[100009], tot;
int son[100009];
double ans = 0;

ll C(ll a){
    if(a < 2) return 0;
    return a*(a-1)/2;
}

ll cal(int t){
    ll x = t, y = n - t, sum = 0;
    sum += C(x) * y;
    sum += C(y) * x;
    return sum;
}

int dfs(int u, int fa){
    son[u] = 1;
    REP(i, edge[u].size()){
        int v = edge[u][i].fr, id = edge[u][i].sc;
        if(v == fa) continue;
        int t = dfs(v, u);
        cnt[id] = cal(t);
        son[u] += t;
    }
    return son[u];
}

double D(ll x, ll y){
    double xx = x, yy = y;
    return xx / yy;
}

int main(){
   // freopen("in", "r", stdin);

    cin>>n;
    REP(i, n-1){
        int u, v, c;
        cin>>u>>v>>c;
        len[i+1] = c;
        edge[u].push_back(pii(v, i+1));
        edge[v].push_back(pii(u, i+1));
    }

    dfs(1, 0);
    tot = ((ll)n)*(n-1)*(n-2)/6;
    FOR(i, 1, n-1)
        ans += 2.0*len[i]*D(cnt[i], tot);

    int query;
    cin>>query;
    while(query -- ){
        int id, w;
        cin>>id>>w;
        ans -= 2.0*(len[id]-w)*D(cnt[id], tot);
        len[id] = w;
        printf("%.8lf\n", ans);
    }

    return 0;
}


nvidia@ubuntu:~$ sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys EB3E94ADBE1229CF Executing: /tmp/apt-key-gpghome.yUehSg7N9i/gpg.1.sh --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys EB3E94ADBE1229CF gpg: 密钥 EB3E94ADBE1229CF:公钥 “Microsoft (Release signing) <gpgsecurity@microsoft.com>” 已导入 gpg: 处理的总数:1 gpg: 已导入:1 nvidia@ubuntu:~$ nvidia@ubuntu:~$ cd /etc/apt/sources.list.d nvidia@ubuntu:/etc/apt/sources.list.d$ sudo rm *ros*.list # 删除名称含"ros"的源文件 nvidia@ubuntu:/etc/apt/sources.list.d$ # 清华源(Ubuntu 18.04) nvidia@ubuntu:/etc/apt/sources.list.d$ sudo sh -c 'echo "deb https://mirrors.tuna.tsinghua.edu.cn/ros/ubuntu $(lsb_release -sc) main" > /etc/apt/sources.list.d/ros-latest.list' nvidia@ubuntu:/etc/apt/sources.list.d$ sudo apt update # 应不再报错 命中:3 http://ports.ubuntu.com/ubuntu-ports focal InRelease 命中:5 http://ppa.launchpad.net/deadsnakes/ppa/ubuntu focal InRelease 获取:6 https://packages.microsoft.com/repos/code stable InRelease [3,590 B] 获取:7 https://mirrors.tuna.tsinghua.edu.cn/ros/ubuntu focal InRelease [4,679 B] 命中:8 http://ports.ubuntu.com/ubuntu-ports focal-updates InRelease 命中:1 https://repo.download.nvidia.cn/jetson/common r35.5 InRelease 命中:2 https://repo.download.nvidia.cn/jetson/t194 r35.5 InRelease 命中:9 http://ports.ubuntu.com/ubuntu-ports focal-backports InRelease 命中:4 https://repo.download.nvidia.cn/jetson/ffmpeg r35.5 InRelease 命中:10 http://ports.ubuntu.com/ubuntu-ports focal-security InRelease 错误:6 https://packages.microsoft.com/repos/code stable InRelease 由于没有公钥,无法验证下列签名: NO_PUBKEY EB3E94ADBE1229CF 获取:11 https://mirrors.tuna.tsinghua.edu.cn/ros/ubuntu focal/main arm64 Packages [826 kB] 正在读取软件包列表... 完成 W: GPG 错误:https://packages.microsoft.com/repos/code stable InRelease: 由于没有公钥,无法验证下列签名: NO_PUBKEY EB3E94ADBE1229CF E: 仓库 “https://packages.microsoft.com/repos/code stable InRelease” 没有数字签名。 N: 无法安全地用该源进行更新,所以默认禁用该源。 N: 参见 apt-secure(8) 手册以了解仓库创建和用户配置方面的细节。 nvidia@ubuntu:/etc/apt/sources.list.d$ sudo apt upgrade -y 正在读取软件包列表... 完成 正在分析软件包的依赖关系树 正在读取状态信息... 完成 正在计算更新... 完成 下列软件包是自动安装的并且现在不需要了: libdc1394-25 使用'sudo apt autoremove'来卸载它(它们)。 The following security updates require Ubuntu Pro with 'esm-infra' enabled: libgstreamer-plugins-base1.0-dev libstdc++-10-dev libsoup-gnome2.4-1 libopenjp2-7 poppler-utils gstreamer1.0-alsa libcups2 libprotoc-dev linux-libc-dev xserver-common libpoppler-dev libxml2-utils libpython3.8-dev gir1.2-soup-2.4 gstreamer1.0-plugins-base-apps openssl libblockdev-swap2 ruby2.7 xserver-xorg-core libprotoc17 gir1.2-gdkpixbuf-2.0 libgdk-pixbuf2.0-0 libssh-4 libpython3.8-minimal libsqlite3-dev libwbclient0 git-man libmysqlclient-dev libsystemd0 gcc-10-base gstreamer1.0-plugins-good libgs9 python2.7-minimal libsqlite3-0 python3-protobuf python3-urllib3 libsnmp-base bind9-host libitm1 libcgraph6 libtiff-dev sudo libpython2.7 python2.7 python3-pip libpython3.8 python3.8 xserver-xorg-legacy git libblockdev-crypto2 udev gstreamer1.0-plugins-base libasan6 libblockdev-loop2 libblockdev-fs2 libgstreamer-plugins-good1.0-dev libblockdev-part2 python3-requests liblab-gamut1 libgstreamer-plugins-good1.0-0 libudev1 libsoup2.4-1 gstreamer1.0-pulseaudio systemd-timesyncd libpoppler-private-dev libgcc1 samba-libs xserver-xephyr protobuf-compiler libgcc-10-dev gstreamer1.0-gtk3 libpmix2 libtiff5 udisks2 libsnmp35 libtsan0 libubsan1 libruby2.7 libprotobuf-lite17 libgfortran5 python3.8-minimal libgstreamer-gl1.0-0 systemd-sysv libblockdev2 libxml2-dev libpam-systemd libgstreamer-plugins-base1.0-0 libcdt5 xwayland gstreamer1.0-x ghostscript liblsan0 libpathplan4 systemd libgomp1 libgdk-pixbuf2.0-bin libssh-gcrypt-4 gir1.2-gst-plugins-base-1.0 libssl-dev libblockdev-utils2 ghostscript-x libgvpr2 libgdk-pixbuf2.0-common libsmbclient libgdk-pixbuf2.0-dev libmysqlclient21 libnss-systemd libgs9-common libblockdev-part-err2 libgcc-s1 libxml2 libpython2.7-minimal libpython3.8-stdlib libgnutls30 libudisks2-0 python3.8-dev libatomic1 libssl1.1 libcc1-0 libgvc6 libprotobuf17 libcupsimage2 libpython2.7-stdlib libpoppler-glib8 libstdc++6 libpoppler97 python3-scipy libprotobuf-dev libopenjp2-7-dev graphviz python-pip-whl bind9-libs gstreamer1.0-gl libtiffxx5 libxslt1.1 Learn more about Ubuntu Pro at https://ubuntu.com/pro 升级了 0 个软件包,新安装了 0 个软件包, 要卸载 0 个软件包,有 0 个软件包未被升级。 nvidia@ubuntu:/etc/apt/sources.list.d$
11-20
nvidia@ubuntu:/etc/apt/sources.list.d$ sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys EB3E94ADBE1229CF Executing: /tmp/apt-key-gpghome.6xmQzuUwNC/gpg.1.sh --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys EB3E94ADBE1229CF gpg: 密钥 EB3E94ADBE1229CF:“Microsoft (Release signing) <gpgsecurity@microsoft.com>” 未改变 gpg: 处理的总数:1 gpg: 未改变:1 nvidia@ubuntu:/etc/apt/sources.list.d$ curl -sSL https://packages.microsoft.com/keys/microsoft.asc | sudo gpg --dearmor -o /usr/share/keyrings/microsoft.gpg nvidia@ubuntu:/etc/apt/sources.list.d$ echo "deb [signed-by=/usr/share/keyrings/microsoft.gpg] https://packages.microsoft.com/repos/code stable main" | sudo tee /etc/apt/sources.list.d/vscode.list deb [signed-by=/usr/share/keyrings/microsoft.gpg] https://packages.microsoft.com/repos/code stable main nvidia@ubuntu:/etc/apt/sources.list.d$ sudo apt update && sudo apt upgrade -y 命中:2 https://mirrors.tuna.tsinghua.edu.cn/ros/ubuntu focal InRelease 命中:4 http://ports.ubuntu.com/ubuntu-ports focal InRelease 命中:5 http://ppa.launchpad.net/deadsnakes/ppa/ubuntu focal InRelease 获取:7 https://packages.microsoft.com/repos/code stable InRelease [3,590 B] 命中:8 http://ports.ubuntu.com/ubuntu-ports focal-updates InRelease 命中:1 https://repo.download.nvidia.cn/jetson/common r35.5 InRelease 命中:3 https://repo.download.nvidia.cn/jetson/t194 r35.5 InRelease 命中:6 https://repo.download.nvidia.cn/jetson/ffmpeg r35.5 InRelease 命中:9 http://ports.ubuntu.com/ubuntu-ports focal-backports InRelease 命中:10 http://ports.ubuntu.com/ubuntu-ports focal-security InRelease 获取:11 https://packages.microsoft.com/repos/code stable/main arm64 Packages [20.2 kB] 已下载 20.2 kB,耗时 7秒 (2,993 B/s) 正在读取软件包列表... 完成 正在分析软件包的依赖关系树 正在读取状态信息... 完成 有 1 个软件包可以升级。请执行 ‘apt list --upgradable’ 来查看它们。 W: 目标 Packages (main/binary-arm64/Packages) 在 /etc/apt/sources.list.d/vscode.list:1 和 /etc/apt/sources.list.d/vscode_official.list:1 中被配置了多次 W: 目标 Packages (main/binary-all/Packages) 在 /etc/apt/sources.list.d/vscode.list:1 和 /etc/apt/sources.list.d/vscode_official.list:1 中被配置了多次 W: 目标 Translations (main/i18n/Translation-zh_CN) 在 /etc/apt/sources.list.d/vscode.list:1 和 /etc/apt/sources.list.d/vscode_official.list:1 中被配置了多次 W: 目标 Translations (main/i18n/Translation-zh) 在 /etc/apt/sources.list.d/vscode.list:1 和 /etc/apt/sources.list.d/vscode_official.list:1 中被配置了多次 W: 目标 Translations (main/i18n/Translation-en) 在 /etc/apt/sources.list.d/vscode.list:1 和 /etc/apt/sources.list.d/vscode_official.list:1 中被配置了多次 W: 目标 DEP-11 (main/dep11/Components-arm64.yml) 在 /etc/apt/sources.list.d/vscode.list:1 和 /etc/apt/sources.list.d/vscode_official.list:1 中被配置了多次 W: 目标 DEP-11 (main/dep11/Components-all.yml) 在 /etc/apt/sources.list.d/vscode.list:1 和 /etc/apt/sources.list.d/vscode_official.list:1 中被配置了多次 W: 目标 DEP-11-icons-small (main/dep11/icons-48x48.tar) 在 /etc/apt/sources.list.d/vscode.list:1 和 /etc/apt/sources.list.d/vscode_official.list:1 中被配置了多次 W: 目标 DEP-11-icons (main/dep11/icons-64x64.tar) 在 /etc/apt/sources.list.d/vscode.list:1 和 /etc/apt/sources.list.d/vscode_official.list:1 中被配置了多次 W: 目标 DEP-11-icons-hidpi (main/dep11/icons-64x64@2.tar) 在 /etc/apt/sources.list.d/vscode.list:1 和 /etc/apt/sources.list.d/vscode_official.list:1 中被配置了多次 W: 目标 Packages (main/binary-arm64/Packages) 在 /etc/apt/sources.list.d/vscode.list:1 和 /etc/apt/sources.list.d/vscode_official.list:1 中被配置了多次 W: 目标 Packages (main/binary-all/Packages) 在 /etc/apt/sources.list.d/vscode.list:1 和 /etc/apt/sources.list.d/vscode_official.list:1 中被配置了多次 W: 目标 Translations (main/i18n/Translation-zh_CN) 在 /etc/apt/sources.list.d/vscode.list:1 和 /etc/apt/sources.list.d/vscode_official.list:1 中被配置了多次 W: 目标 Translations (main/i18n/Translation-zh) 在 /etc/apt/sources.list.d/vscode.list:1 和 /etc/apt/sources.list.d/vscode_official.list:1 中被配置了多次 W: 目标 Translations (main/i18n/Translation-en) 在 /etc/apt/sources.list.d/vscode.list:1 和 /etc/apt/sources.list.d/vscode_official.list:1 中被配置了多次 W: 目标 DEP-11 (main/dep11/Components-arm64.yml) 在 /etc/apt/sources.list.d/vscode.list:1 和 /etc/apt/sources.list.d/vscode_official.list:1 中被配置了多次 W: 目标 DEP-11 (main/dep11/Components-all.yml) 在 /etc/apt/sources.list.d/vscode.list:1 和 /etc/apt/sources.list.d/vscode_official.list:1 中被配置了多次 W: 目标 DEP-11-icons-small (main/dep11/icons-48x48.tar) 在 /etc/apt/sources.list.d/vscode.list:1 和 /etc/apt/sources.list.d/vscode_official.list:1 中被配置了多次 W: 目标 DEP-11-icons (main/dep11/icons-64x64.tar) 在 /etc/apt/sources.list.d/vscode.list:1 和 /etc/apt/sources.list.d/vscode_official.list:1 中被配置了多次 W: 目标 DEP-11-icons-hidpi (main/dep11/icons-64x64@2.tar) 在 /etc/apt/sources.list.d/vscode.list:1 和 /etc/apt/sources.list.d/vscode_official.list:1 中被配置了多次 正在读取软件包列表... 完成 正在分析软件包的依赖关系树 正在读取状态信息... 完成 正在计算更新... 完成 下列软件包是自动安装的并且现在不需要了: libdc1394-25 使用'sudo apt autoremove'来卸载它(它们)。 The following security updates require Ubuntu Pro with 'esm-infra' enabled: libgstreamer-plugins-base1.0-dev libstdc++-10-dev libsoup-gnome2.4-1 libopenjp2-7 poppler-utils gstreamer1.0-alsa libcups2 libprotoc-dev linux-libc-dev xserver-common libpoppler-dev libxml2-utils libpython3.8-dev gir1.2-soup-2.4 gstreamer1.0-plugins-base-apps openssl libblockdev-swap2 ruby2.7 xserver-xorg-core libprotoc17 gir1.2-gdkpixbuf-2.0 libgdk-pixbuf2.0-0 libssh-4 libpython3.8-minimal libsqlite3-dev libwbclient0 git-man libmysqlclient-dev libsystemd0 gcc-10-base gstreamer1.0-plugins-good libgs9 python2.7-minimal libsqlite3-0 python3-protobuf python3-urllib3 libsnmp-base bind9-host libitm1 libcgraph6 libtiff-dev sudo libpython2.7 python2.7 python3-pip libpython3.8 python3.8 xserver-xorg-legacy git libblockdev-crypto2 udev gstreamer1.0-plugins-base libasan6 libblockdev-loop2 libblockdev-fs2 libgstreamer-plugins-good1.0-dev libblockdev-part2 python3-requests liblab-gamut1 libgstreamer-plugins-good1.0-0 libudev1 libsoup2.4-1 gstreamer1.0-pulseaudio systemd-timesyncd libpoppler-private-dev libgcc1 samba-libs xserver-xephyr protobuf-compiler libgcc-10-dev gstreamer1.0-gtk3 libpmix2 libtiff5 udisks2 libsnmp35 libtsan0 libubsan1 libruby2.7 libprotobuf-lite17 libgfortran5 python3.8-minimal libgstreamer-gl1.0-0 systemd-sysv libblockdev2 libxml2-dev libpam-systemd libgstreamer-plugins-base1.0-0 libcdt5 xwayland gstreamer1.0-x ghostscript liblsan0 libpathplan4 systemd libgomp1 libgdk-pixbuf2.0-bin libssh-gcrypt-4 gir1.2-gst-plugins-base-1.0 libssl-dev libblockdev-utils2 ghostscript-x libgvpr2 libgdk-pixbuf2.0-common libsmbclient libgdk-pixbuf2.0-dev libmysqlclient21 libnss-systemd libgs9-common libblockdev-part-err2 libgcc-s1 libxml2 libpython2.7-minimal libpython3.8-stdlib libgnutls30 libudisks2-0 python3.8-dev libatomic1 libssl1.1 libcc1-0 libgvc6 libprotobuf17 libcupsimage2 libpython2.7-stdlib libpoppler-glib8 libstdc++6 libpoppler97 python3-scipy libprotobuf-dev libopenjp2-7-dev graphviz python-pip-whl bind9-libs gstreamer1.0-gl libtiffxx5 libxslt1.1 Learn more about Ubuntu Pro at https://ubuntu.com/pro 下列软件包将被升级: code 升级了 1 个软件包,新安装了 0 个软件包, 要卸载 0 个软件包,有 0 个软件包未被升级。 需要下载 105 MB 的归档。 解压缩后会消耗 1,585 kB 的额外空间。 获取:1 https://packages.microsoft.com/repos/code stable/main arm64 code arm64 1.106.1-1763289434 [105 MB] 已下载 105 MB,耗时 15秒 (6,888 kB/s) 正在预设定软件包 ... (正在读取数据库 ... 系统当前共安装有 253785 个文件和目录。) 准备解压 .../code_1.106.1-1763289434_arm64.deb ... 正在解压 code (1.106.1-1763289434) 并覆盖 (1.105.1-1760482225) ... 正在设置 code (1.106.1-1763289434) ... 正在处理用于 mime-support (3.64ubuntu1) 的触发器 ... 正在处理用于 gnome-menus (3.36.0-1ubuntu1) 的触发器 ... 正在处理用于 shared-mime-info (1.15-1) 的触发器 ... 正在处理用于 desktop-file-utils (0.24-1ubuntu3) 的触发器 ... nvidia@ubuntu:/etc/apt/sources.list.d$
最新发布
11-20
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值