Codeforces goodbye 2014 d
2015-01-12 00:00
169 查看
摘要: Codeforces goodbye 2014 d
/** * @brief good bye 2014 d * @file d.cpp * @author mianma * @created 2014/01/12 15:42 * @edited 2014/01/12 15:42 * @type dfs tree * @note fail */ #include <fstream> #include <iostream> #include <cstring> #include <vector> #include <set> #include <stack> #include <algorithm> #include <utility> #include <iomanip> using namespace std; #define max(a, b) ((a) > (b) ? (a) : (b)) #define min(a, b) ((a) > (b) ? (b) : (a)) #define abs(a) ((a) > 0 ? (a) : (0 - (a))) #define CLR(vec) memset(vec, 0, sizeof(vec)) #ifdef DEBUG ifstream in; ofstream out; #define CIN in #define COUT out #else #define CIN cin #define COUT cout #endif #define MAXN (100000 + 50) typedef long long int ll; int weight[MAXN]; /*store all the weight for all path*/ ll cuts[MAXN]; /*tot edge cross all cuts*/ ll counter[MAXN]; /*counter all node in this subtree*/ vector< pair<int, int> > tree[MAXN]; /*store tree for all pos*/ double e; /*expected val*/ double m; ll n, q; void dfs(int prev, int curr){ counter[curr] = 1; for(int i = 0; i < tree[curr].size(); i++){ int next = tree[curr][i].first; int edge = tree[curr][i].second; if(prev == next) continue; dfs(curr, next); counter[curr] += counter[next]; ll lft = counter[next]; ll rht = n - lft; ll tot = 0; /*count edge*/ if(1 != rht) /*lft choose 1, rht choose 2*/ tot += lft*((rht * (rht - 1))/2); if(1 != lft) /*lft choose 2, rht choose 1*/ tot += rht*((lft * (lft - 1))/2); cuts[edge] = tot; } #ifdef DEBUG COUT << curr << "->" << counter[curr] << endl; #endif } int main(void){ ios_base::sync_with_stdio(0); #ifdef DEBUG CIN.open("./in", ios::in); COUT.open("./out", ios::out); #endif CIN >> n; int a, b; m = n*(n - 1)*(n - 2)/6.0; for(int i = 1; i <= n - 1; i++){ CIN >> a >> b >> weight[i]; tree[a].push_back(make_pair(b, i)); tree[b].push_back(make_pair(a, i)); } dfs(0, a); e = 0.0; for(int i = 1; i <= n - 1; i++) e += cuts[i] * 2.0 * weight[i]; CIN >> q; COUT << fixed << setprecision(10); int r, w; for(int i = 1; i <= q; i++){ CIN >> r >> w; e -= cuts[r] * 2.0 * (weight[r] - w); weight[r] = w; COUT << e/m << "\n"; } return 0; }
相关文章推荐
- Codeforces goodbye 2014 b
- codeforces GoodBye 2014 *A *B C(未完)
- Codeforces goodbye 2014 a
- Codeforces goodbye 2014
- codeforces goodbye2014
- Codeforces goodbye 2014 c
- Codeforces gym 100548 F (2014-2015 icpc西安现场赛) 数学
- 【转】Codeforces GoodBye2015 New Year and Three Musketeers Codeforces 611E(贪心)
- 【中途相遇法】【STL】BAPC2014 K Key to Knowledge (Codeforces GYM 100526)
- Codeforces GYM 100548 K - Last Defence 2014-2015 ACM-ICPC, Asia Xian Regional Contest
- Codeforces GoodBye 2015
- Codeforces Zepto Code Rush 2014 -C - Dungeons and Candies
- Codeforces Gym 100548G The Problem to Slow Down You (Palindromic Tree 或 Hash水过) 2014西安现场赛G题
- Codeforces April Fools Day Contest 2014(附官方题解)
- 2014-2015 CT S02E01: Codeforces Trainings Season 2 Episode 1 题解
- Codeforces GYM 100548 F - Color 2014-2015 ACM-ICPC, Asia Xian Regional Contest
- 【最短路】BAPC2014 B Button Bashing (Codeforces GYM 100526)
- Codeforces Rockethon 2014 解题报告
- Codeforces GYM 100548 I - International Collegiate Routing Contest 2014 ACM Xian Regional Contest
- 【最短路】BAPC2014 B Button Bashing (Codeforces GYM 100526)