正文
增加 360 行删除 41 行
123433 行未改动3839404142434445464748495042 行未改动9394959697989910010130 行未改动13213313413513613713813914014114214314414514614714814915015115215315415515615715815916087 行未改动2482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822838 行未改动29229329429529629729829930043 行未改动344345346347348349350351352353354355100 行未改动45645745845946046146246346451 行未改动51651751851952052152252352452552656 行未改动58358458558658758858959059137 行未改动629630631632633634635636183 行未改动全部展开820821822823824# 1.数据结构## 1.1 动态开点线段树33 行未改动:::success[[$\textup{例1.1.1}$](https://www.luogu.com.cn/problem/CF915E)]下面是带懒标记的板子,有点丑。```cpp#include<bits/stdc++.h>#define IOS \ios::sync_with_stdio( false );\cin.tie( 0 ), cout.tie( 0 );using namespace std;const int MAXN = 15000010;int N, Q;int tot, root;struct tree{42 行未改动cin >> l >> r >> k;mdf( root, 1, N, l, r, k );cout << N - tr[root].sum << endl;}return 0;}```:::30 行未改动一直卡在最后统计答案要第一次扫到的时候存,不然它被合并之后就错了。```cpp#include<bits/stdc++.h>using namespace std;const int MAXN = 6e6 + 5;const int MAXZ = 1e5 + 5;int N, M;struct star{int nxt, to;}edge[MAXZ << 1];int head[MAXZ], cnt;void add( int u, int v){edge[++ cnt] = { head[u], v };head[u] = cnt;}int f[MAXZ][32], dep[MAXZ];void dfs( int u, int fa ){dep[u] = dep[fa] + 1;f[u][0] = fa;for( int i = 1; i <= 20; i ++ ){f[u][i] = f[f[u][i - 1]][i - 1];}for( int i = head[u]; i; i = edge[i].nxt ){int v = edge[i].to;if( v == fa ) continue;dfs( v, u );87 行未改动ddfs( 1, 0 );for( int i = 1; i <= N; i ++ ){cout << ans[i] << endl;// cout << tr[root[i]].pos << endl;}return 0;}```::::::success[[$\textup{例1.2.2}$](https://www.luogu.com.cn/problem/P3605)]稍易一些,就不说思路了。```cpp#include<bits/stdc++.h>#define int long longusing namespace std;const int MAXN = 1e5 + 5;const int MAXT = 6e6 + 5;const int MAXP = 1e9 + 5;int N;int p[MAXN];struct star{int nxt, to;}edge[MAXN << 1];int head[MAXN], cnt;void add( int u, int v ){edge[++ cnt] = { head[u], v };head[u] = cnt;}int root[MAXN], tot;struct TREE{int ls, rs, cnt;8 行未改动return;}int mid = ( l + r ) >> 1;if( pos <= mid ) mdf( tr[p].ls, l, mid, pos );else mdf( tr[p].rs, mid + 1, r, pos );upd( p );}int qry( int p, int l, int r, int x ){43 行未改动add( i, k ), add( k, i );}dfs( 1, 0 );for( int i = 1; i <= N; i ++ ){cout << ans[i] << endl;}return 0;}```:::100 行未改动插入操作主要是要通过 $cmp$ 去比较每个影响到的节点中哪个最优,步骤如下:+ 判断两线段是否相交,不交直接判断高矮(无交集直接退出)+ 若相交,且当前递归区间被线段区间完全包含,从 $mid$ 位置再进行判断:树中存储的为 $mid$ 位置更大的那个,然后再看左右两边哪边大,递归。+ 若没被包含,则递归下去直到满足条件。51 行未改动注意精度问题,并且如果两个差小于某个精度,就比较编号大小(题目要求)```cpp#include<bits/stdc++.h>using namespace std;const int MAXN = 1e5 + 5;const int MOD = 39989;const int MOD1 = 1e9;const double eps = 1e-10;//用于调整精度56 行未改动cin >> N;while( N -- ){int op, k, x, y, xx, yy;cin >> op;// cerr << op << endl;if( op == 0 ){cin >> k;int x = ( k + lstans - 1 ) % MOD + 1;lstans = 0;37 行未改动讲一下这玩意的用处,比如区间和,直接维护十分迷茫,便拆开将重链合并起来用线段树维护,段与段之间暴力即可。有一个很好用的性质,就是注意到如果我们用 $dfn$ 序进行剖分,那么一段重区间的 $dfn$ 序应当是连续的!下面是例题。:::success[没有]最近没怎么做。183 行未改动全部展开好吧正经一点说:+ 把它按重链剖了;+ 先跑轻儿子,解决其中的询问;+ 出来,把重儿子跑了,然后1234567833 行未改动424344454647484942 行未改动929394959697989930 行未改动13013113213313413513613713813914014114214314414514614787 行未改动2352362372382392402412422432442452462472482492502512522532542552562578 行未改动26626726826927027127227327443 行未改动318319320321322323324325326327328100 行未改动42943043143243343443543643751 行未改动48949049149249349449549656 行未改动55355455555655755855956037 行未改动598599600601602603604605606607608609610611612613614615616617618619620621622183 行未改动全部展开806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143# 0 前言由于有一些板子过长,我就丢例题里了,剩下的会单独贴出来一份。# 1.数据结构## 1.1 动态开点线段树33 行未改动:::success[[$\textup{例1.1.1}$](https://www.luogu.com.cn/problem/CF915E)]下面是带懒标记的板子,有点丑。```cppconst int MAXN = 15000010;int N, Q;int tot, root;struct tree{42 行未改动cin >> l >> r >> k;mdf( root, 1, N, l, r, k );cout << N - tr[root].sum << endl;}return 0;}```:::30 行未改动一直卡在最后统计答案要第一次扫到的时候存,不然它被合并之后就错了。```cppconst int MAXN = 6e6 + 5;const int MAXZ = 1e5 + 5;int N, M;int f[MAXZ][32], dep[MAXZ];void dfs( int u, int fa ){dep[u] = dep[fa] + 1;f[u][0] = fa;for( int i = 1; i <= 20; i ++ ){f[u][i] = f[f[u][i - 1]][i - 1];}for( int i = head[u]; i; i = edge[i].nxt ){int v = edge[i].to;if( v == fa ) continue;dfs( v, u );87 行未改动ddfs( 1, 0 );for( int i = 1; i <= N; i ++ ){cout << ans[i] << endl;}return 0;}```::::::success[[$\textup{例1.2.2}$](https://www.luogu.com.cn/problem/P3605)]稍易一些,就不说思路了。```cppconst int MAXN = 1e5 + 5;const int MAXT = 6e6 + 5;const int MAXP = 1e9 + 5;int N;int p[MAXN];int root[MAXN], tot;struct TREE{int ls, rs, cnt;8 行未改动return;}int mid = ( l + r ) >> 1;if( pos <= mid ) mdf( tr[p].ls, l, mid, pos );else mdf( tr[p].rs, mid + 1, r, pos );upd( p );}int qry( int p, int l, int r, int x ){43 行未改动add( i, k ), add( k, i );}dfs( 1, 0 );for( int i = 1; i <= N; i ++ )cout << ans[i] << endl;return 0;}```:::100 行未改动插入操作主要是要通过 $cmp$ 去比较每个影响到的节点中哪个最优,步骤如下:+ 判断两线段是否相交,不交直接判断高矮(无交集直接退出)+ 若相交,且当前递归区间被线段区间完全包含,从 $mid$ 位置再进行判断:树中存储的为 $mid$ 位置更大的那个,然后再看左右两边哪边大,递归。+ 若没被包含,则递归下去直到满足条件。51 行未改动注意精度问题,并且如果两个差小于某个精度,就比较编号大小(题目要求)```cppconst int MAXN = 1e5 + 5;const int MOD = 39989;const int MOD1 = 1e9;const double eps = 1e-10;//用于调整精度56 行未改动cin >> N;while( N -- ){int op, k, x, y, xx, yy;cin >> op;if( op == 0 ){cin >> k;int x = ( k + lstans - 1 ) % MOD + 1;lstans = 0;37 行未改动讲一下这玩意的用处,比如区间和,直接维护十分迷茫,便拆开将重链合并起来用线段树维护,段与段之间暴力即可。有一个很好用的性质,就是注意到如果我们用 $dfn$ 序进行剖分,那么一段重区间的 $dfn$ 序应当是连续的!:::info[板子]```cppvoid dfs( int u, int fa ){siz[u] = 1;int nowmax = 0;for( int i = head[u]; i; i = edge[i].nxt ){int v = edge[i].to;if( v == fa ) continue;dfs( v, u );siz[u] += siz[v];if( siz[v] > nowmax ) nowmax = siz[v], hson[u] = v;}}```:::下面是例题。:::success[没有]最近没怎么做。183 行未改动全部展开好吧正经一点说:+ 把它按重链剖了;+ 先跑轻儿子,解决其中的询问;+ 出来,把重儿子跑了,然后把数据丢给这一轮的父亲,更新数据+ 最后暴力跑一边轻儿子,把他们的数据跑出来给父亲。和前面长链剖分优化dp的思路类似,就是独立处理子树然后选择最大的直接继承,这样就不用再跑一次重子树了。那么时间复杂度?重链不可能超过 $\log$ 条,于是为 $O( N \cdot \log{N} )$:::success[[$\textup{例2.?+1.1}$](https://www.luogu.com.cn/problem/CF570D)]几乎没什么改动的模板题。把询问离线下来,按照上面所说的在子树里面跑,跑出来该删的删,该继承的继承。最后再按原序回答即可,看代码清楚一点。```cppconst int MAXN = 500005;int N, M;struct qwq{int id, h;};vector<qwq> q[MAXN];struct star{int nxt, to;}edge[MAXN << 1];int head[MAXN], cnt;void add( int u, int v ){edge[++ cnt] = { head[u], v };head[u] = cnt;}int tim = 0;char col[MAXN];int dep[MAXN], hson[MAXN];int in[MAXN], out[MAXN], id[MAXN];//进入、离开的时间戳以及id[u]表示时间u对应的节点编号int siz[MAXN], ccnt[MAXN][30];//cnt[d][c]表示深度为d的节点中颜色c出现的次数bool ans[MAXN];//第i个查询的答案void dfs( int u, int fa ){dep[u] = dep[fa] + 1;siz[u] = 1;in[u] = ++ tim;id[tim] = u;int nowmax = 0;for( int i = head[u]; i; i = edge[i].nxt ){int v = edge[i].to;if( v == fa ) continue;dfs( v, u );siz[u] += siz[v];if( siz[v] > nowmax ) nowmax = siz[v], hson[u] = v;}out[u] = tim;}//在对应深度加入计数器void addnd( int u ){ ccnt[dep[u]][col[u] - 'a' + 1] ++; }//删除关于该节点的统计void dltnd( int u ){ ccnt[dep[u]][col[u] - 'a' + 1] = 0; }void addu( int u ){//将u对应的整颗子树加入统计(因为dfn序在同一子树应当是连续的)for( int i = in[u]; i <= out[u]; i ++ ){addnd( id[i] );}}void dltu( int u ){//删除同理for( int i = in[u]; i <= out[u]; i ++ ){dltnd( id[i] );}}bool chk( int d ){//查询深度d(当前子树)是否满足int tot = 0;for( int i = 1; i <= 26; i ++ ){if( ccnt[d][i] & 1 ){tot ++;if( tot > 1 ) return false;}}return true;}void dotsolve( int u, bool kep ){for( int i = head[u]; i; i = edge[i].nxt ){int v = edge[i].to;if( v == hson[u] ) continue;//先处理轻儿子并且不保留信息dotsolve( v, false );}if( hson[u] ){//处理重儿子(保留信息)dotsolve( hson[u], true );}for( int i = head[u]; i; i = edge[i].nxt ){int v = edge[i].to;if( v == hson[u] ) continue;//将轻儿子信息暴力合并进uaddu( v );}addnd( u );//计入当前节点for( qwq qqq : q[u] ){ans[qqq.id] = chk( qqq.h );//由于前面删除加入的特质,此处不会计入其他子树的节点}if( ! kep ) dltu( u );//如果是轻子节点就删除数据}int main(){ios::sync_with_stdio( 0 );cin.tie( 0 );cout.tie( 0 );cin >> N >> M;for( int i = 2; i <= N; i ++ ){int p;cin >> p;add( p, i );}for( int i = 1; i <= N; i ++ )cin >> col[i];for( int i = 1; i <= M; i ++ ){int u, d;cin >> u >> d;q[u].push_back({ i, d });}dfs( 1, 0 );dotsolve( 1, 0 );for( int i = 1; i <= M; i ++ ){cout << ( ans[i] ? "Yes\n" : "No\n" );}return 0;}```:::---# 3.图论## 3.? 网络流相关概念:+ 网络可以理解为是在一张有向图中,每条边的边权一般为其流量最大值。+ 增广路表示一条从源点到汇点剩余流量大于 $0$ 的路径。+ 残量网络表示每条边都有残余流量的网络。### 3.?.1 最大流常见问题:给定源汇点,最大化整个网络的流量。引入一个假的贪心,当有增广路的时候就走,更新。我们将其修复正确,即为 FF 算法。对于每一条边,我们对其建反边。当有流量经过原先边时,我们更新其反边权值为当前所消耗的流量。于是,实现了反贪,我们可以通过走反边来撤销流量。EK 算法,是其的一个实现(优化),该算法的核心是:在残量网络中用 bfs 找一个条源点到汇点的增广路,其余都与前面相同。看起来和我们原先的贪心很相似,但实际上我们会发现,这个算法使用的是 bfs,在时间和策略上都有保障。时间复杂度:$O(N \cdot M ^ 2)$啥?你说 EK 容易被卡太慢了?有没有更快的?有的。Dinic 算法可以高效地找出最大流,原理是通过在每次找增广路之后按照顺序标记分层图,再跑出答案。正确性不会证,但是因此有点似懂非懂?弧优化即为记录每个点找到上次扫到的那条边,减少循环,并不会证明。时间复杂度:$O(N ^ 2 \cdot M)$:::success[[$\textup{例3.?.1.1}$](https://www.luogu.com.cn/problem/P3376)]cnt 记得从 1 开始,这样异或出来才是反边。EK。```cppint N, M, S, T, ans;bool vis[MAXN];int dis[MAXN], pre[MAXN], flag[2510][2510];//dis[i]表示从源点到i的路径最多能推多少流 pre[i]记录到达i所用的边(在edge中的下标)//flag[u][v]表示u -> v的边在edge中的下标struct star{int nxt, to, w;//w -> 当前便的残量}edge[MAXN];int head[MAXN], cnt = 1;void add( int u, int v, int w ){edge[++ cnt] = { head[u], v, w };head[u] = cnt;edge[++ cnt] = { head[v], u, 0 };head[v] = cnt;}int bfs(){for( int i = 1; i <= N; i ++ ) vis[i] = 0;queue<int> q;q.push( S );vis[S] = 1, dis[S] = inf;while( !q.empty() ){int u = q.front();q.pop();for( int i = head[u]; i; i = edge[i].nxt ){if( edge[i].w == 0 ) continue;int v = edge[i].to;if( vis[v] ) continue;vis[v] = true;dis[v] = min( dis[u], edge[i].w );pre[v] = i, q.push( v );if( v == T ) return 1;}}return 0;//没找到增广路径}void upd(){int u = T;//从汇点回溯int nowflow = dis[T];while( u != S ){edge[pre[u]].w -= nowflow, edge[pre[u] ^ 1].w += nowflow;u = edge[pre[u] ^ 1].to;//反向边}ans += nowflow;}signed main(){cin >> N >> M >> S >> T;for( int i = 1; i <= M; i ++ ){int u, v, w;cin >> u >> v >> w;if( flag[u][v] == 0 ){add( u, v, w );flag[u][v] = cnt;} else edge[flag[u][v] - 1].w += w;//因为一次性建了两条边,于是要减一}while( bfs() )//可以找到增广路径upd();cout << ans;return 0;}```Dinic。```cppint N, M, S, T;int dep[MAXN], cur[MAXN];//标记分层图深度 cur[i]表示点i循环到了哪一条边struct star{int nxt, to, w;}edge[MAXN << 1];int head[MAXN], cnt = 1;void add( int u, int v, int w ){edge[++ cnt] = { head[u], v, w };head[u] = cnt;edge[++ cnt] = { head[v], u, 0 };head[v] = cnt;}int ans;bool bfs(){queue<int> q;for( int i = 1; i <= N; i ++ ) dep[i] = 0;dep[S] = 1, q.push( S );while( !q.empty() ){int u = q.front();q.pop();for( int i = head[u]; i; i = edge[i].nxt ){int v = edge[i].to;if( edge[i].w && !dep[v] ){dep[v] = dep[u] + 1;q.push( v );}}}if( dep[T] == 0 ) return false;return true;}int dfs( int u, int dis ){//u->当前节点 dis->当前流量if( u == T ) return dis;for( int & i = cur[u]; i; i = edge[i].nxt ){//引用->同时更新cur数组int v = edge[i].to;if( dep[v] == dep[u] + 1 && edge[i].w != 0 ){int now = dfs( v, min( dis, edge[i].w ) );if( now ){edge[i].w -= now, edge[i ^ 1].w += now;return now;}}}return 0;}void dinic(){while( bfs() ){for( int i = 1; i <= N; i ++ ) cur[i] = head[i];//记得每次更新的到第一条边while( int now = dfs( S, INF ) ){ans += now;}}}signed main(){cin >> N >> M >> S >> T;for( int i = 1; i <= M; i ++ ){int u, v, w;cin >> u >> v >> w;add( u, v, w );}dinic();cout << ans;return 0;}```:::### 3.?.2 最小割