版本比较
一些模板
正文
增加 110 行删除 257 行
425 行未改动全部展开426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467285 行未改动全部展开7537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081,067 行未改动全部展开425 行未改动全部展开}return (dfn[u]<dfn[v]?u:v);}```最大子段和```cppint f[N] = {0}, ans = -1e9;for(int i = 1; i <= n; i++) {f[i] = max(a[i], f[i - 1] + a[i]);ans = max(ans, f[i]);}```最长公共子序列```cppconst int N = 1e5 + 5;int n, a[N], b[N], f[N], c[N], r[N], ans;inline void ins(int k, int x) {for(; k < N; k += k & -k) {c[k] = max(c[k], x);}return ;}inline int ask(int k) {int ans = 0;for(; k; k -= k & -k) {ans = max(c[k], ans);}return ans;}for(int i = 1; i <= n; i++) {r[b[i]] = i;}for(int i = 1; i <= n; i++) {f[i] = ask(r[a[i]]) + 1;ins(r[a[i]], f[i]);ans = max(ans, f[i]);}```ST 表```cppint n,a[N],st[N][LOG];void work()285 行未改动全部展开{//}```辗转相除```cppinline int gcd(int a,int b){return !b ? a : gcd(b, a % b);}```LCA 最近公共祖先```cppinline int LCA(int x, int y) {if(d[x] < d[y]) {swap(x, y);}int t = d[x] - d[y];for(int i = 0; i < 20; i++) {if(t & (1 << i)) x = fa[x][i];}if(x == y) {return x;}for(int i = 19; i >= 0; i--) {if(fa[x][i] != fa[y][i]) {x = fa[x][i], y = fa[y][i];}}return fa[x][0];}```缩点```cpp#include<bits/stdc++.h>#define int long longusing namespace std;const int N = 1e5 + 5;struct Node {int u, v, ne;} e[N * 2], e2[N * 2];int n, m, w[N];int cnt, hd[N];int dfn[N], low[N];int s[N], f[N], d[N];int tot, dep[N], ans, h[N];bool vis[N];inline void add(int x,int y) {e[++cnt].u = x;e[cnt].v = y;e[cnt].ne = hd[x];hd[x] = cnt;}inline void tarjan(int u) {dfn[u] = low[u] = ++cnt;vis[u] = 1, s[++tot] = u;for(int i = hd[u], to; to = e[i].v, i; i = e[i].ne) {if(!dfn[to]) {tarjan(to);low[u] = min(low[u], low[to]);} else if(vis[to]) {low[u] = min(low[u], dfn[to]);}}if(low[u] == dfn[u]) {while(int k = s[tot--]) {f[k] = u;vis[k] = 0;if(k == u) {break;}w[u] += w[k];}}}signed main() {cin >> n >> m;for(int i = 1; i <= n; i++) {cin >> w[i];}for(int i = 1; i <= m; i++) {int x, y;cin >> x >> y;add(x, y);}cnt = 0;for(int i = 1; i <= n; i++) {if(!dfn[i]) {tarjan(i);}}cnt = 0;for(int x, y, i = 1; i <= m; i++) {x = f[e[i].u],y = f[e[i].v];if(x != y) {e2[++cnt].u = x;e2[cnt].v = y;e2[cnt].ne = h[x];h[x] = cnt;d[y]++;}}tot = 0;for(int i = 1; i <= n; i++) {if(f[i] == i && !d[i]) {s[++tot] = i;dep[i] = w[i];}}while(tot) {int u = s[tot--];for(int v, i = h[u]; v = e2[i].v, i; i = e2[i].ne) {dep[v] = max(dep[v], dep[u] + w[v]);--d[v];if(!d[v]) {s[++tot] = v;}}}for(int i = 1; i <= n; i++) {ans = max(ans, dep[i]);}cout << ans;return 0;}```点双连通分量```cpp#include<bits/stdc++.h>using namespace std;const int N = 5e5 + 5;int n, m, dfn[N], low[N];int cnt, tot, r;vector<int> e[N], ans[N];stack<int> stk;bool vis[N];inline void tarjan(int x) {dfn[x] = low[x] = ++cnt;if(!e[x].size() && r == x) {ans[++tot].push_back(x);return ;}stk.push(x);for(int i : e[x]) {if(!dfn[i]) {tarjan(i);low[x] = min(low[x], low[i]);if(dfn[x] <= low[i]) {++tot;int tmp;do {tmp = stk.top();stk.pop();ans[tot].push_back(tmp);} while(tmp != i);ans[tot].push_back(x);}} else {low[x] = min(low[x], dfn[i]);}}}int main() {ios::sync_with_stdio(0);cin.tie(0), cout.tie(0);cin >> n >> m;while(m--) {int u, v;cin >> u >> v;if(u == v) {continue;}e[u].push_back(v);e[v].push_back(u);}for(int i = 1; i <= n; i++) {if(!vis[i]) {r = i;tarjan(i);}}cout << tot << '\n';for(int i = 1; i <= tot; i++) {cout << ans[i].size() << ' ';for(int j : ans[i]) {cout << j << ' ';}cout << '\n';}return 0;}```边双连通分量```cpp#include <bits/stdc++.h>using namespace std;const int N = 5e5 + 5;int n, m, u, v, cnt;int dfn[N], low[N];vector<pair<int, int> > e[N];vector<vector<int> > ans;stack<int> st;void tarjan(int x, int las) {low[x] = dfn[x] = ++cnt;st.push(x);for(pair<int, int> i : e[x]) {if(i.second == (las ^ 1)) {continue;}if(!dfn[i.first]) {tarjan(i.first, i.second);low[x] = min(low[x], low[i.first]);} else {low[x] = min(low[x], dfn[i.first]);}}if(dfn[x] == low[x]) {vector<int> vec;vec.push_back(x);while(st.top() != x) {vec.push_back(st.top());st.pop();}st.pop();ans.push_back(vec);}}int main() {ios::sync_with_stdio(0);cin.tie(0), cout.tie(0);cin >> n >> m;for(int i(1); i <= m; ++i) {cin >> u >> v;e[u].push_back({v, i * 2});e[v].push_back({u, i * 2 + 1});}for(int i = 1; i <= n; ++i) {if(!dfn[i]) {tarjan(i, 0);}}cout << ans.size() << '\n';for(vector<int> i : ans) {cout << i.size() << ' ';for(int j : i) {cout << j << ' ';}cout << '\n';}return 0;}```高斯消元```cpp1,067 行未改动全部展开425 行未改动全部展开426427428429430431432433285 行未改动全部展开7197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608611,067 行未改动全部展开425 行未改动全部展开}return (dfn[u]<dfn[v]?u:v);}```ST 表```cppint n,a[N],st[N][LOG];void work()285 行未改动全部展开{//}```缩点```cppint n,m,head[N],ins[N],tot,scc[N],dfn[N],low[N],dfncnt,scccnt,siz[N];struct edge{int v,nxt;}eg[2*M];void add(int u,int v){eg[++tot].v=v;eg[tot].nxt=head[u];head[u]=tot;}stack<int> s;void tarjan(int u){ins[u]=1;s.push(u);dfn[u]=low[u]=++dfncnt;for(int i=head[u];i;i=eg[i].nxt){int v=eg[i].v;if(!dfn[v]){tarjan(v);low[u]=min(low[u],low[v]);}else if(ins[v]) low[u]=min(low[u],dfn[v]);}if(low[u]==dfn[u]){scccnt++;while(s.size()){int v=s.top();s.pop();scc[v]=scccnt;siz[scccnt]+=a[v];ins[v]=0;if(v==u) break;}}}```点双连通分量```cppint n,m,head[N],root,dfncnt,cut[N],tot,low[N],dfn[N],scccnt;stack<int> q;vector<int> scc[N];struct edge{int v,nxt;}eg[2*M];void add(int u,int v){eg[++tot].v=v;eg[tot].nxt=head[u];head[u]=tot;}void tarjan(int u){dfn[u]=low[u]=++dfncnt;q.push(u);if(u==root&&head[u]==0){scc[++scccnt].push_back(u);return;}int child=0;for(int i=head[u];i;i=eg[i].nxt){int v=eg[i].v;if(dfn[v]==0){tarjan(v);low[u]=min(low[u],low[v]);if(low[v]>=dfn[u]){child++;if(u!=root||child>1) cut[u]=1;scccnt++;while(1){int w=q.top();q.pop();scc[scccnt].push_back(w);if(w==v) break;}scc[scccnt].push_back(u);}}else low[u]=min(low[u],dfn[v]);}}```边双连通分量```cppint n,m,head[N],tot,dfn[N],dfncnt,low[N],scc[N],scccnt,ins[N];stack<int> st;struct edge{int v,nxt;}eg[2*M];void add(int u,int v){eg[++tot].v=v;eg[tot].nxt=head[u];head[u]=tot;}void tarjan(int u,int fa){int cnt=0;low[u]=dfn[u]=++dfncnt;st.push(u);ins[u]=1;for(int i=head[u];i;i=eg[i].nxt){int v=eg[i].v;if(v==fa){cnt++;if(cnt==2) low[u]=min(low[u],dfn[v]);continue;}if(!dfn[v]){tarjan(v,u);low[u]=min(low[u],low[v]);}else if(ins[v]) low[u]=min(low[u],dfn[v]);}if(low[u]==dfn[u]){scccnt++;while(st.size()){int v=st.top();st.pop();scc[v]=scccnt;ins[v]=0;if(u==v) break;}}}```高斯消元```cpp1,067 行未改动全部展开