POJ 3237 Tree 樹鏈剖分,poj3237

來源:互聯網
上載者:User

POJ 3237 Tree 樹鏈剖分,poj3237

樹鏈剖分基礎題

#include <cstdio>#include <cstring>#include <algorithm>using namespace std;const int maxn = 10010;struct edge{int v, next;}e[maxn*2];int first[maxn], cnt;int top[maxn], dep[maxn], sz[maxn], f[maxn], son[maxn], rank[maxn], tid[maxn];int tp, tim;int d[maxn][3];int n;void AddEdge(int u, int v){e[cnt].v = v;e[cnt].next = first[u];first[u] = cnt++;e[cnt].v = u;e[cnt].next = first[v];first[v] = cnt++;}void init(){memset(first, -1, sizeof(first));cnt = 1;memset(son, -1, sizeof(son));tim = 0;}void dfs1(int u, int fa, int d){sz[u] = 1;dep[u] = d;f[u] = fa;for(int i = first[u]; i != -1; i = e[i].next){int v = e[i].v;if(v == fa)continue;dfs1(v, u, d+1);sz[u] += sz[v];if(son[u] == -1 || sz[son[u]] < sz[v])son[u] = v;}}void dfs2(int u, int tp){top[u] = tp;tid[u] = ++tim;rank[tid[u]] = u;if(son[u] == -1)return;dfs2(son[u], tp);for(int i = first[u]; i != -1; i = e[i].next){int v = e[i].v;if(v != f[u] && son[u] != v)dfs2(v, v);}}int ma[maxn<<2];int mi[maxn<<2];int lz[maxn<<2];void pushup(int l, int r, int rt){ma[rt] = max(ma[rt<<1], ma[rt<<1|1]);mi[rt] = min(mi[rt<<1], mi[rt<<1|1]);}void build(int l, int r, int rt){ma[rt] = 0;mi[rt] = 0;lz[rt] = 0;if(l == r){return;}int m = (l + r) >> 1;build(l, m, rt<<1);build(m+1, r, rt<<1|1);}void pushdown(int l, int r, int rt){if(l == r)return;if(lz[rt]){mi[rt<<1] = -mi[rt<<1];ma[rt<<1] = -ma[rt<<1];swap(mi[rt<<1], ma[rt<<1]);mi[rt<<1|1] = -mi[rt<<1|1];ma[rt<<1|1] = -ma[rt<<1|1];swap(mi[rt<<1|1], ma[rt<<1|1]);lz[rt<<1] ^= 1;lz[rt<<1|1] ^= 1;lz[rt] = 0;}}void update(int x, int y, int l, int r, int rt){if(x == l && y == r){mi[rt] = -mi[rt];ma[rt] = -ma[rt];swap(mi[rt], ma[rt]);lz[rt] ^= 1;return;}pushdown(l, r, rt);int m = (l + r) >> 1;if(y <= m)update(x, y, l, m, rt<<1);else if(x > m)update(x, y, m+1, r, rt<<1|1);else{update(x, m, l, m, rt<<1);update(m+1, y, m+1, r, rt<<1|1);}pushup(l, r, rt);}void update2(int x, int l, int r, int rt, int w){if(l == r){ma[rt] = mi[rt] = w;lz[rt] = 0;return;}pushdown(l, r, rt);int m = (l + r) >> 1;if(x <= m)update2(x, l, m, rt<<1, w);elseupdate2(x, m+1, r, rt<<1|1, w);pushup(l, r, rt);}int query(int x, int y, int l, int r, int rt){if(l == x && r == y)return ma[rt];pushdown(l, r, rt);int m = (l + r) >> 1;if(y <= m)return query(x, y, l, m, rt<<1);else if(x > m)return query(x, y, m+1, r, rt<<1|1);else{return max(query(x, m, l, m, rt<<1), query(m+1, y, m+1, r, rt<<1|1));}}void change(int u, int v){while(top[u] != top[v]){if(dep[top[u]] < dep[top[v]])swap(u, v);update(tid[top[u]], tid[u], 1, n, 1);u = f[top[u]];}if(u == v)return;if(dep[u] > dep[v])swap(u, v);update(tid[son[u]], tid[v], 1, n, 1);}int find(int u, int v){int ans = -999999999;while(top[u] != top[v]){if(dep[top[u]] < dep[top[v]])swap(u, v);ans = max(ans, query(tid[top[u]], tid[u], 1, n, 1));u = f[top[u]];}if(u == v)return ans;if(dep[u] > dep[v])swap(u, v);ans = max(ans, query(tid[son[u]], tid[v], 1, n, 1));return ans;}int main(){int T;scanf("%d", &T);while(T--){init();scanf("%d", &n);for(int i = 1; i < n; i++){int u, v, w;scanf("%d %d %d", &u, &v, &w);AddEdge(u, v);d[i][0] = u;d[i][1] = v;d[i][2] = w;}dfs1(1, 0, 0);dfs2(1, 1);build(1, n, 1);for(int i = 1; i < n; i++){if(dep[d[i][0]] > dep[d[i][1]])swap(d[i][0], d[i][1]);update2(tid[d[i][1]], 1, n, 1, d[i][2]);}char s[10];while(scanf("%s", s) && strcmp(s, "DONE")){if(s[0] == 'Q'){int x, y;scanf("%d %d", &x, &y);printf("%d\n", find(x, y));}else if(s[0] == 'C'){int x, y;scanf("%d %d", &x, &y);update2(tid[d[x][1]], 1, n, 1, y);}else{int x, y;scanf("%d %d", &x, &y);change(x, y);}}} return 0;}



NOI比noip多考些什

大綱:
0. 演算法的時空分析
0.1 時間分析
0.2 空間分析
0.3 時空分配

1. 基礎演算法
1.1 枚舉
1.2 類比
1.3 遞推
1.4 貪心
1.5 遞迴
1.6 分治

2. 排序演算法
2.1 冒泡排序
2.2 選擇排序
2.3 桶排序
2.4 插入排序
2.5 歸併排序
2.6 快速排序
2.7 堆排序
2.8 二叉排序樹

3. 尋找演算法
3.1 順序尋找
3.2 二分尋找
3.3 二分答案

4. 搜尋演算法
4.1 BFS和DFS
4.2 簡單剪枝
4.3 記憶化搜尋

5. 動態規劃
5.1 動態規劃初步
5.2 背包問題
5.3 最大(小)代價子母樹

6. 排列組合
6.1 基本概念
6.2 二項式定理
6.3 康托展開
6.4 袋與球問題

7. 數論
7.1 素數判斷
7.2 最大公約數
7.3 擴充歐幾裡德
7.4 不定方程
7.5 幾類數列
7.6 數的進位

8. 線性表
8.1 數組和向量
8.2 堆棧
8.3 隊列
8.4 字串

9. 圖
9.1 圖的遍曆和拓撲排序
9.1.1 圖的遍曆
9.1.2 拓撲排序
9.2 最短路
9.2.1 Floyd演算法
9.2.2 Dijstra演算法
9.2.3 Bellman-Ford演算法
9.2.4 SPFA演算法
9.3 產生樹
9.3.1 Prim演算法
9.3.2 Kruskal演算法
9.4 圈和塊
9.4.1 最小環
9.4.2 負權環
9.4.3 連通塊

10. 樹
10.1 樹的遍曆
10.2 樹上距離問題
10.2.1 節點到根的距離
10.2.2 最近公用祖先
10.2.3 節點間的距離
10.2.4 樹的直徑
10.3 哈夫曼樹
10.4 二元堆積
10.5 樹形動態規劃
10.6 二叉排序樹
10.7 並查集

11. HASH
11.1 ELFhash
11.2 SDBM
11.3 BKDR
11.4 RK

12. 數論
12.1 矩陣乘法
12.2 高斯消元
12.3 異或方程組

13. 動態規劃
13.1 多維狀態動態規劃
13.2 狀態壓縮動態規劃
13.2.1 遞推
13.2.2 基於連通性
13.3 動態規劃最佳化
13.3.1 降低維度
13.3.2 優先隊列
13.3.3 單調隊列
13.3.4 矩陣加速
13.3.5 斜率最佳化
13.3.6 四邊形不等式

14. 二分圖
14.1 最大匹配
14.1.1 匈牙利演算法
14.1.2 最大流演算法
14.1.3 覆蓋集和獨立集
14.1.4 非二分圖最大匹配
14.2 帶權二分圖匹配
14.2.1 KM演算法
14.2.2 費用流演算法

15. 網路流
15.1 網路流初步
15.2 最大流
15.2.1 Dinic演算法
15.2.2 Sap演算法
15.2.3 有上下界的最大流
15.3 最小割
15.3.1 最小割
15.3.2 平面圖最小割
15.3.3 閉合圖
15.3.4 最小點權覆蓋集與最大點權獨立集
15.3.5 0/1分數規劃
15.......餘下全文>>
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.