【DP】 codeforces 500D New Year Santa Network

来源:互联网 发布:c语言一共有多少个函数 编辑:程序博客网 时间:2024/06/03 19:06

和BC的那个题有点像。。。

#include <iostream>#include <queue> #include <stack> #include <map> #include <set> #include <bitset> #include <cstdio> #include <algorithm> #include <cstring> #include <climits>#include <cstdlib>#include <cmath>#include <time.h>#define maxn 200005#define maxm 2000005#define eps 1e-10#define mod 1000000007#define INF 0x3f3f3f3f#define PI (acos(-1.0))#define lowbit(x) (x&(-x))#define mp make_pair#define ls o<<1#define rs o<<1 | 1#define lson o<<1, L, mid #define rson o<<1 | 1, mid+1, R#define pii pair<int, int>//#pragma comment(linker, "/STACK:16777216")typedef long long LL;typedef unsigned long long ULL;//typedef int LL;using namespace std;LL qpow(LL a, LL b){LL res=1,base=a;while(b){if(b%2)res=res*base;base=base*base;b/=2;}return res;}LL powmod(LL a, LL b){LL res=1,base=a;while(b){if(b%2)res=res*base%mod;base=base*base%mod;b/=2;}return res;}// headstruct Edge{int v, w, id;Edge *next;}*H[maxn], E[maxm], *edges;int size[maxn];int vis[maxn];LL aa[maxn];LL bb[maxn];LL w[maxn];int n, m;void addedges(int u, int v, int id){edges->v = v;edges->id = id;edges->next = H[u];H[u] = edges++;}void dfs(int u){size[u] = 1;for(Edge *e = H[u]; e; e = e->next) {if(!vis[e->v]) {vis[e->v] = 1;dfs(e->v);aa[e->id] = size[e->v];bb[e->id] = n - aa[e->id];size[u] += size[e->v];}}}void init(void){edges = E;memset(H, 0, sizeof H);}void read(void){int a, b, c;scanf("%d", &n);for(int i = 1; i < n; i++) {scanf("%d%d%d", &a, &b, &c);w[i] = c;addedges(a, b, i);addedges(b, a, i);}scanf("%d", &m);}void work(void){vis[1] = 1;dfs(1);int k, t;double ans = 0;for(int i = 1; i < n; i++) ans += (aa[i] - 1) * aa[i] * bb[i] * w[i] + aa[i] * bb[i] * (bb[i] - 1) * w[i];while(m--) {scanf("%d%d", &k, &t);ans -= (aa[k] - 1) * aa[k] * bb[k] * (w[k] - t) + aa[k] * bb[k] * (bb[k] - 1) * (w[k] - t);w[k] = t;printf("%.9f\n", ans * 6 / n / (n - 1) / (n - 2));}}int main(void){init();read();work();return 0;}


0 0
原创粉丝点击