The Last Review
先过一遍,后边统一复习
容易忘的东西,常用的板子
欧拉路
欧拉回路:
1、 图G是连通的,不能有孤立点存在。
2、 对于无向图来说度数为奇数的点个数为0,因为必须有一点进有一点出,才能保证每条边恰好经过一次;对于有向图来说每个点的入度必须等于出度。
欧拉通路:
1、 图G是连通的,无孤立点存在。
2、 对于无向图来说,度数为奇数的的点可以有2个或者0个,并且这两个奇点其中一个为起点另外一个为终点。对于有向图来说,可以存在两个点,其入度不等于出度,其中一个入度比出度大1,为路径的起点;另外一个出度比入度大1,为路径的终点。
割点
void tarjan (i64 u, i64 fa) noexcept {
dfn[u] = low[u] = ++id; vis[u] = 1;
i64 chil = 0;
for (auto it : G[u]) {
if (!dfn[it]) {
chil++, tarjan (it, u), low[u] = std::min (low[u], low[it]);
if (u != fa && !tv[u] && low[it] >= dfn[u])
tv[u] = 1, F.push_back (u);
} else if (vis[it])
low[u] = std::min (low[u], dfn[it]);
}
if (u == fa && !tv[u] && chil > 1)
tv[u] = 1, F.push_back (u);
}
割边
跟缩点板子就差了个
if(low[g[i].v] > dfn[u]) ans++;
并且第二个判断改为
else if (g[i].v != fa) low[u] = min(low[u], dfn[g[i].v]);
void tarjan(int s, int fa){
low[s] = dfn[s] = ++node; vis[s] = 1;
for(int i = first[s]; i; i = g[i].nex){
if(!dfn[g[i].v]){
tarjan(g[i].v, s);
low[s] = min(low[s], low[g[i].v]);
if(low[g[i].v] > dfn[s]) ans++;
}
else if (g[i].v != fa) low[s] = min(low[s], dfn[g[i].v]);
}
}
树的直径
void dfs (i64 u, i64 fa) noexcept{
for (auto it : G[u]){
if (it != fa){
dfs (it, u);
ans = std::max(ans, dp[u] + dp[it] + 1);
dp[u] = std::max(dp[u], dp[it] + 1);
}
}
}
树的重心
void dfs (i64 u, i64 fa) noexcept{
size[u] = 1;
for (auto it : G[u]){
if (it != fa){
dfs (it, u); size[u] += size[it];
dp[u] = std::max(dp[u], size[it]);
}
}
dp[u] = std::max(dp[u], n - size[u]);
if (dp[u] <= n / 2)
flag[u] = 1;
}
最小点覆盖 = 最大流
最大独立集(在二分图中选择尽可能多的顶点,使得这些顶点之间没有边相连) = 总点数 - 最小点覆盖
最大流
inline i64 bfs () noexcept{
while (!p.empty()) p.pop();
for (i32 i = 1; i <= T; i++)
dis[i] = 0;
dis[S] = 1, p.push(S);
while (!p.empty()){
i64 nex = p.front(); p.pop();
for (i32 i = first[nex]; i; i = g[i].nex){
if (g[i].d > 0 && !dis[g[i].v]){
dis[g[i].v] = dis[nex] + 1;
if (g[i].v == T) return 1;
p.push(g[i].v);
}
}
}
return 0;
}
i64 dfs (i64 u, i64 flow) noexcept{
if (u == T) return flow;
i64 res = flow;
for (i32 i = cur[u]; i; i = g[i].nex){
cur[u] = i;
if (g[i].d > 0 && dis[g[i].v] == dis[u] + 1){
i64 k = dfs(g[i].v, std::min(flow, g[i].d));
if (!k) dis[g[i].v] = 0;
g[i].d -= k, g[i ^ 1].d += k, flow -= k;
}
}
return res - flow;
}
inline void Dinic () noexcept{
while (bfs()){
for (i32 i = 1; i <= T; i++)
cur[i] = first[i];
maxflow += dfs(S, INF);
}
}
费用流
inline i64 spfa() noexcept{
for (i32 i = 1; i <= T; i++) vis[i] = 0, dis[i] = INF;
while (!q.empty()) q.pop();
q.push(S), dis[S] = 0, vis[S] = 1;
while (!q.empty()){
i64 nex = q.front(); q.pop(); vis[nex] = 0;
for (i32 i = first[nex]; i; i = g[i].nex){
if (g[i].d > 0 && dis[nex] + g[i].val < dis[g[i].v]){
dis[g[i].v] = dis[nex] + g[i].val;
if (!vis[g[i].v]) vis[g[i].v] = 1, q.push(g[i].v);
}
}
}
for (i32 i = 1; i <= T; i++) vis[i] = 0;
return dis[T] == INF ? 0 : 1;
}
i64 dfs(i64 u, i64 flow) noexcept{
if (u == T) return flow;
i64 res = flow;
vis[u] = 1;
for (i32 i = cur[u]; i && flow; i = g[i].nex){
cur[u] = i;
if (!vis[g[i].v] && g[i].d > 0 && dis[u] + g[i].val == dis[g[i].v]){
i64 k = dfs(g[i].v, min(flow, g[i].d));
if (!k) dis[g[i].v] = INF;
mincost += k * g[i].val, g[i].d -= k, g[i ^ 1].d += k, flow -= k;
}
}
vis[u] = 0;
return res - flow;
}
inline void Dinic() noexcept{
while (spfa()){
for (i32 i = 1; i <= T; i++) cur[i] = first[i];
maxflow += dfs(S, INF) ;
}
}
Kmp
inline void Pre () noexcept{
i64 j = 0; kmp[1] = 0;
for (i32 i = 2; i <= lenb; i++){
while (j > 0 && B[j + 1] != B[i])
j = kmp[j];
if (B[j + 1] == B[i]) j++;
kmp[i] = j;
}
}
inline void Kmp () noexcept{
i32 j = 0;
for (i32 i = 1; i <= lena; i++){
while (j > 0 && A[i] != B[j + 1])
j = kmp[j];
if (A[i] == B[j + 1]) j++;
if (j == lenb) put(i - lenb + 1), j = kmp[j];
}
for (i32 i = 1; i <= lenb; i++)
print(kmp[i]), putchar(' ');
}
猫树
void build (i64 k, i64 l, i64 r, i64 dep) noexcept{
if (l == r) {pos[l] = k; return;}
i64 mid = (l + r) >> 1; cat[dep][mid] = s[mid];
for (i32 i = mid - 1; i >= l; i--)
cat[dep][i] = std::max(cat[dep][i + 1], s[i]);
for (i32 i = mid + 1; i <= r; i++)
cat[dep][i] = std::max(cat[dep][i - 1], s[i]);
build (k * 2, l, mid, dep + 1), build (k * 2 + 1, mid + 1, r, dep + 1);
}
inline i64 query (i64 l, i64 r) noexcept{
if (l == r) return s[l];
i64 d = lg[pos[l]] - lg[pos[l] ^ pos[r]];
return std::max(cat[d][l], cat[d][r]);
}
珂朵莉树
struct Chtholly {
i64 l, r; mutable i64 v;
Chtholly (i64 L, i64 R = 0, i64 V = 0) : l (L), r (R), v (V) {}
bool operator < (const Chtholly& nex) const {
return l < nex.l;
}
} ;
std::set <Chtholly> s;
inline auto split (i64 pos) noexcept {
auto it = s.lower_bound (Chtholly (pos));
if (it != s.end () && it -> l == pos) return it;
it--;
i64 l = it -> l, r = it -> r, v = it -> v;
s.erase (it); s.insert (Chtholly (l, pos - 1, v));
return s.insert (Chtholly (pos, r, v)).first;
}
inline void ins (i64 l, i64 r, i64 v) noexcept {
s.insert (Chtholly (l, r, v));
}
inline void assign (i64 l, i64 r, i64 v) noexcept {
auto itr = split (r + 1), itl = split (l);
s.erase (itl, itr), ins (l, r, v);
}
PBDS 平衡树
using namespace __gnu_pbds;
tree <std::pair<i64, i64>, null_type, std::less<std::pair<i64, i64> >, rb_tree_tag, tree_order_statistics_node_update> p;
std::unordered_map<i64, i64> vis;
i64 n;
int main() noexcept{
read(n);
while (n--){
i64 opt, x; read(opt, x);
if (opt == 1) p.insert({x, ++vis[x]});
else if (opt == 2) p.erase({x, vis[x]--});
else if (opt == 3) put(p.order_of_key({x, 0}) + 1);
else if (opt == 4) put(p.find_by_order(x - 1) -> first);
else if (opt == 5) put((*(--p.lower_bound({x, 0}))).first);
else if (opt == 6) put((*p.upper_bound({x, vis[x]})).first);
}
//fwrite(obuf,p3-obuf,1,stdout);
return 0;
}
次小生成树:
枚举每一条边,减去那一条路径上的最大的边,如果相等就减次大的,以此类推
模拟退火
inline void SA () noexcept {
while (true){
double T = 2000, d = 0.996, Tx = 1e-7;
while (T > Tx){
if ((double) clock () / CLOCKS_PER_SEC > 1.96)
put (ans), exit (0);
i64 x = random (1, n), y = random (1, n), w, deltaT;
std::swap (pos[x], pos[y]);
w = calc (), deltaT = ans - w;
if (deltaT < 0) ans = w;
else if (exp (-deltaT / T) * RAND_MAX < rand ())
std::swap (pos[x], pos[y]);
T *= d;
}
}
}
遗传算法
struct Individual{
std::vector<i64> p; i64 fitness;
bool operator < (const Individual& nex) const{
return fitness < nex.fitness;
}
inline i64 calc_fitness () noexcept{
}
Individual (std::vector<i64> t) noexcept{
this -> p = t;
this -> fitness = calc_fitness();
}
inline Individual mate () noexcept{
std::vector<i64> nex = this -> p;
i64 pos = random(0, 3);
for (i32 i = 0; i <= pos; i++){
i64 x = rand() % nex.size(), y = rand() % nex.size();
std::swap(nex[x], nex[y]);
}
return (Individual(nex));
}
};
int main() noexcept{
srand(71806291);
for (i32 i = 1; i <= n; i++)
if (!vis[i])
F.push_back(i);
std::vector<Individual> population;
for (i32 i = 1; i <= POPULATION; i++){
std::random_shuffle(F.begin(), F.end());
population.push_back(Individual(F));
}
i64 TEST = 300;
for (i64 T = TEST; T; T--){
if ((double)clock() / CLOCKS_PER_SEC > 0.75)
return put(ans), 0;
std::sort(population.begin(), population.end());
ans = population[0].fitness;
std::vector<Individual> _population;
i64 nex = (POPULATION * 10) / 100;
for (i32 i = 1; i <= nex; i++)
_population.push_back(population[i]);
while (_population.size() < population.size()){
Individual p = population[random(0, 50)], q = p.mate();
i64 delta = q.fitness - p.fitness;
if (delta < 0) _population.push_back(q);
else if (exp(-delta / T) * RAND_MAX > rand())
_population.push_back(q);
}
population = _population;
}
//fwrite(obuf,p3-obuf,1,stdout);
return 0;
}
差分约束
形如