图论算法模板
前言
有兴趣可以看一下(qwq
No.1 dijkstra
P4779 【模板】单源最短路径(标准版)
#import "bits/stdc++.h"
#define N 100005
using namespace std;
typedef pair<long long, int> PII;
long long n, m, s, x, y, z;
struct Edge
{
long long from, to, len;
Edge(int from, int to, int len) : from(from), to(to), len(len) {}
};
vector<Edge> edges[N];
long long dis[N];
bool vis[N];
void dijkstra()
{
memset(dis, 127, sizeof(dis));
priority_queue<PII, vector<PII>, greater<PII>> q;
q.push(PII(0, s));
dis[s] = 0;
while (!q.empty())
{
PII u = q.top(); q.pop();
int to = u.second; long long to_dis = u.first;
if (vis[to]) continue;
vis[to] = true;
for (auto y : edges[to])
{
long long len = to_dis + y.len;
if (vis[y.to]) continue;
if (dis[y.to] > len)
{
dis[y.to] = len;
q.push(PII(dis[y.to], y.to));
}
}
}
}
main()
{
scanf("%lld%lld%lld", &n, &m, &s);
for (int i = 1; i <= m; i++)
{
scanf("%lld%lld%lld", &x, &y, &z);
edges[x].push_back(Edge(x, y, z));
}
dijkstra();
for (int i = 1; i <= n; i++)
printf("%lld ", dis[i]);
return 0;
}
No.2 spfa
- P1807 最长路
#import "bits/stdc++.h"
#define fast \
std::ios::sync_with_stdio(false); \
std::cin.tie(nullptr);
#define int long long
#define N 50005
#define INF 2147483647
using namespace std;
typedef pair<int, int> PII;
int n, m, dis[N];
struct Edge
{
int from, to, len;
Edge(int from, int to, int len) : from(from), to(to), len(len) {}
};
vector<Edge> edges[N];
bool vis[N];
void spfa()
{
memset(dis, 127, sizeof(dis));
queue<int> q;
q.push(1);
dis[1] = 0;
while (!q.empty())
{
int u = q.front();
q.pop();
vis[u] = true;
for (Edge e : edges[u])
{
int len = dis[u] + e.len;
if (dis[e.to] > len)
{
dis[e.to] = len;
if (!vis[e.to])
{
vis[e.to] = true;
q.push(e.to);
}
}
}
vis[u] = false;
}
return;
}
signed main()
{
fast;
cin >> n >> m;
if (m < (n - 1)) {cout << -1; exit(0); }
if (n == 0 || m == 0)
{
cout << -1;
exit(0);
}
for (int i = 1, u, v, w; i <= m; i++)
{
cin >> u >> v >> w;
edges[u].push_back(Edge(u, v, -w));
}
spfa();
if (dis[N] >= INF)
cout << -1;
else
cout << (-dis[n]);
return 0;
}
- P3385 【模板】负环
#include <bits/stdc++.h>
#define N 2005
#define int long long
using namespace std;
int T, n, m, x, y, z;
struct Edge
{
int to, len;
Edge(int to, int len) : to(to), len(len) {}
};
vector<Edge> edges[N];
int dis[N], cnt[N];
bool vis[N];
bool spfa()
{
memset(cnt, 0, sizeof cnt);
memset(dis, 127, sizeof dis);
memset(vis, 0, sizeof vis);
queue<int> q;
q.push(1);
vis[1] = 1;
dis[1] = 0;
cnt[1]++;
while (!q.empty())
{
int u = q.front();
vis[u] = false;
q.pop();
for (Edge e : edges[u])
{
int len = dis[u] + e.len;
if (dis[e.to] > len)
{
dis[e.to] = len;
if (!vis[e.to])
{
vis[e.to] = 1;
q.push(e.to);
cnt[e.to]++;
if (cnt[e.to] > n)
return true;
}
}
}
}
return false;
}
signed main()
{
ios::sync_with_stdio(false);
std::cin.tie(0);
std::cout.tie(0);
cin >> T;
while (T--)
{
cin >> n >> m;
for (int i = 1; i <= m; i++)
{
cin >> x >> y >> z;
if (z >= 0)
edges[x].push_back(Edge(y, z)), edges[y].push_back(Edge(x, z));
else
edges[x].push_back(Edge(y, z));
}
if (spfa())
cout << "YES" << endl;
else
cout << "NO" << endl;
for (int i = 1; i <= n; i++)
edges[i].clear();
}
return 0;
}
- P1144 最短路计数
#include <bits/stdc++.h>
#define MOD 100003
#define N 1000005
#define M 4000005
#define int long long
using namespace std;
const int INF = 1 << 30;
int n, m, x, y, dis[N], ans[N];
bool vis[N];
struct Edge
{
int to, len;
Edge() {}
Edge(int to, int len) : to(to), len(len) {}
};
vector<Edge> edges[M];
void spfa()
{
memset(dis, 127, sizeof dis);
queue<int> q;
q.push(1);
dis[1] = 0;
ans[1] = 1;
while (!q.empty())
{
int u = q.front();
vis[u] = true;
for (Edge e : edges[u])
{
long long len = dis[u] + e.len;
if (dis[e.to] > len)
{
dis[e.to] = len;
ans[e.to] = ans[u];
if (!vis[e.to])
{
vis[e.to] = true;
q.push(e.to);
}
}
else if (dis[e.to] == dis[u] + 1)
{
ans[e.to] += ans[u];
ans[e.to] %= MOD;
}
}
vis[u] = false;
q.pop();
}
}
signed main()
{
scanf("%lld%lld", &n, &m);
for (int i = 1; i <= m; i++)
{
scanf("%lld%lld", &x, &y);
edges[x].push_back(Edge(y, 1));
edges[y].push_back(Edge(x, 1));
}
spfa();
for (int i = 1; i <= n; i++)
printf("%lld\n", ans[i]);
return 0;
}
- P1462 通往奥格瑞玛的道路
#include <bits/stdc++.h>
#define int long long
#define N 100005
#define INF 1000000005
using namespace std;
typedef pair<int, int> PII;
int n, m, b, x, y, z, dis[N], f[N];
bool vis[N];
struct Edge
{
int from, to, len;
Edge(int from, int to, int len) : from(from), to(to), len(len) {}
};
vector<Edge> edges[N];
bool spfa(int x)
{
if (x < f[1])
return false;
memset(dis, 127, sizeof(dis));
memset(vis, 0, sizeof(vis));
priority_queue<PII, vector<PII>, greater<PII>> q;
q.push(PII(0, 1));
dis[1] = 0;
while (!q.empty())
{
PII u = q.top();
q.pop();
int to = u.second;
int to_dis = u.first;
if (vis[to])
continue;
vis[to] = true;
for (auto y : edges[to])
{
if (f[y.to] > x)
continue;
int len = to_dis + y.len;
if (vis[y.to])
continue;
if (f[y.to] <= x && dis[y.to] > len)
{
dis[y.to] = len;
q.push(PII(dis[y.to], y.to));
}
// if (dis[y.to] > len)
// {
// dis[y.to] = len;
// if (!vis[y.to] && f[y.to] <= x)
// {
// vis[y.to] = true;
// q.push(PII(dis[y.to], y.to));
// }
// }
// if (f[y.to] <= x && dis[y.to] > len)
// {
// }
}
// vis[to] = false;
}
if (dis[n] <= b)
return true;
return false;
}
signed main()
{
scanf("%lld%lld%lld", &n, &m, &b);
for (int i = 1; i <= n; i++)
scanf("%lld", &f[i]);
for (int i = 1; i <= m; i++)
{
scanf("%lld%lld%lld", &x, &y, &z);
// if (x == y)
// continue;
edges[x].push_back(Edge(x, y, z));
edges[y].push_back(Edge(y, x, z));
}
if (!spfa(INF))
{
// puts("AFK");
printf("AFK");
return 0;
}
int l = 1, r = INF, mid;
bool flag;
while (l <= r)
{
mid = (l + r) >> 1;
flag = spfa(mid);
if (flag)
r = mid - 1;
else
l = mid + 1;
// cout << l << endl;
}
printf("%lld", l);
return 0;
}
No.3 floyd
- B3647 【模板】Floyd
#include <bits/stdc++.h>
#define int long long
#define INF 1000000007
#define N 105
using namespace std;
int n, m, u, v, w, dis[N][N];
signed main()
{
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
cin >> n >> m;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
dis[i][j] = (i == j ? 0 : INF);
for (int i = 1; i <= m; i++)
{
cin >> u >> v >> w;
dis[u][v] = min(dis[u][v], w);
dis[v][u] = min(dis[v][u], w);
}
for (int k = 1; k <= n; k++)
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
dis[i][j] = min(dis[i][j], dis[k][j] + dis[i][k]);
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= n; j++)
cout << dis[i][j] << ' ';
cout << endl;
}
return 0;
}
- B3611 【模板】传递闭包
#include <bits/stdc++.h>
#define int long long
#define INF 1000000007
#define N 105
using namespace std;
int n, m, u, v, w, dis[N][N];
signed main()
{
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
cin >> n;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
cin >> dis[i][j];
for (int k = 1; k <= n; k++)
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
dis[i][j] |= dis[i][k] & dis[k][j];
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= n; j++)
cout << dis[i][j] << ' ';
cout << endl;
}
return 0;
}
#include <bits/stdc++.h>
#define N 105
using namespace std;
bitset<N> a[N];
int n;
int main()
{
cin >> n;
for (int i = 1, u; i <= n; i++)
for (int j = 1; j <= n; j++)
{
cin >> u;
a[i][j] = u;
}
for (int k = 1; k <= n; k++)
for (int i = 1; i <= n; i++)
if (a[i][k])
a[i] |= a[k];
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= n; j++)
cout << a[i][j] << ' ';
cout << endl;
}
return 0;
}
No.4 topo sort
- B3644 【模板】拓扑排序 / 家谱树
#include <bits/stdc++.h>
using namespace std;
const int N = 1005;
int n, x, idx = 0, r[N];
vector<int> edges[N];
queue<int> q;
void topo_sort()
{
for (int i = 1; i <= n; i++)
if (!r[i])
q.push(i);
while (!q.empty())
{
int p = q.front();
q.pop();
cout << p << ' ';
for (int e : edges[p])
if (!--r[e])
q.push(e);
}
}
int main()
{
ios::sync_with_stdio(false);
std::cin.tie(nullptr);
std::cout.tie(nullptr);
cin >> n;
for (int i = 1; i <= n; i++)
{
while (cin >> x && x)
{
r[x]++;
edges[i].push_back(x);
}
}
topo_sort();
return 0;
}
- DAG: P1807 最长路
#import "bits/stdc++.h"
#define fast \
std::ios::sync_with_stdio(false); \
std::cin.tie(nullptr);
#define int long long
#define N 50005
using namespace std;
typedef pair<int, int> PII;
int n, m, dis[N], deg[N];
struct Edge
{
int from, to, len;
Edge(int from, int to, int len) : from(from), to(to), len(len) {}
};
vector<Edge> edges[N];
void tupo_sort()
{
memset(dis, 0xcf, sizeof dis);
queue<int> q;
q.push(1);
dis[1] = 0, dis[n] = -1;
for (int i = 2; i <= n; i++)
if (!deg[i])
q.push(i);
while (!q.empty())
{
int u = q.front();
q.pop();
for (Edge e : edges[u])
{
int len = dis[u] + e.len;
if (dis[e.to] < len)
dis[e.to] = len;
if (!(--deg[e.to]))
q.push(e.to);
}
}
}
signed main()
{
fast;
cin >> n >> m;
if (m < (n - 1))
{
cout << -1;
exit(0);
}
if (n == 0 || m == 0)
{
cout << -1;
exit(0);
}
for (int i = 1, u, v, w; i <= m; i++)
{
cin >> u >> v >> w;
edges[u].push_back(Edge(u, v, w));
deg[v]++;
}
tupo_sort();
cout << dis[n];
return 0;
}
No.5 分层图
- P4568 [JLOI2011] 飞行路线
#import "bits/stdc++.h"
#define N 110005
#define M 2500005
#define int long long
#define fast \
std::ios::sync_with_stdio(false); \
cin.tie(nullptr);
using namespace std;
typedef pair<int, int> PII;
int n, m, k, s, t, u, v, w, cnt = 0, ans = __INT_MAX__;
int dis[N], pre[N];
bool vis[N];
struct Edge
{
int to, ne, len;
} edges[M];
void add(int u, int v, int len = 0)
{
edges[++cnt] = {v, pre[u], len};
pre[u] = cnt;
}
void input()
{
cin >> n >> m >> k >> s >> t;
for (int i = 1; i <= m; i++)
{
cin >> u >> v >> w;
add(u, v, w), add(v, u, w);
for (int j = 1; j <= k; j++)
{
add(u + (j - 1) * n, v + j * n), add(v + (j - 1) * n, u + j * n);
add(u + j * n, v + j * n, w), add(v + j * n, u + j * n, w);
}
}
}
void dijkstra()
{
priority_queue<PII, vector<PII>, greater<PII>> q;
memset(dis, 127, sizeof dis);
dis[s] = 0;
q.push(make_pair(0, s));
while (q.size())
{
int u = q.top().second;
q.pop();
if (u == t)
return;
if (vis[u])
continue;
vis[u] = true;
for (int i = pre[u]; i; i = edges[i].ne)
{
int to = edges[i].to, len = dis[u] + edges[i].len;
if (dis[to] > len)
{
dis[to] = len;
q.push(make_pair(dis[to], to));
}
}
}
}
signed main()
{
fast;
input();
for (int i = 1; i <= k; i++)
add(t + (i - 1) * n, t + i * n), add(t + i * n, t + (i - 1) * n);
dijkstra();
for (int i = 0; i <= k; i++)
ans = min(ans, dis[t + i * n]);
cout << ans;
return 0;
}
#import "bits/stdc++.h"
#define N 110005
#define M 2500005
#define int long long
#define fast \
std::ios::sync_with_stdio(false); \
cin.tie(nullptr);
using namespace std;
int n, m, k, s, t, u, v, w, cnt = 0, ans = __INT_MAX__;
int d[N][15], pre[N];
bool vis[N][15];
struct Edge
{
int p, cnt, dis;
bool operator <(const Edge& other) const { return dis > other.dis; }
} edges[M];
struct node
{
int to, ne, len;
} a[M];
void add(int u, int v, int len)
{
a[++cnt] = {v, pre[u], len};
pre[u] = cnt;
}
void input()
{
cin >> n >> m >> k >> s >> t;
s++, t++;
for (int i = 1; i <= m; i++)
{
cin >> u >> v >> w;
u++, v++;
add(u, v, w), add(v, u, w);
}
}
void dijkstra()
{
priority_queue<Edge> q;
memset(d, 0x3f, sizeof d);
d[s][0] = 0;
q.push({s, 0, 0});
while (q.size())
{
Edge u = q.top();
q.pop();
int p = u.p, cnt = u.cnt, dis = u.dis;
if (vis[p][cnt])
continue;
vis[p][cnt] = true;
for (int i = pre[p]; i; i = a[i].ne)
{
int to = a[i].to;
if (cnt < k && d[p][cnt] < d[to][cnt + 1])
{
d[to][cnt + 1] = d[p][cnt];
q.push({to, cnt + 1, d[to][cnt + 1]});
}
if (d[p][cnt] + a[i].len < d[to][cnt])
{
d[to][cnt] = d[p][cnt] + a[i].len;
q.push({to, cnt, d[to][cnt]});
}
}
}
}
signed main()
{
fast;
input();
dijkstra();
for (int i = 0; i <= k; i++)
ans = min(ans, d[t][i]);
cout << ans;
return 0;
}
- P10948 升降梯上
#include <bits/stdc++.h>
#define int long long
#define N 1005
#define M 25
#define Enoch2013 return 0;
using namespace std;
int n, m, s = 0, ans = 1e16, c[M], dis[N][M];
bool vis[N][M];
struct Edge
{
int to, ne, len;
Edge() {}
Edge(int to, int ne, int len) : to(to), ne(ne), len(len) {}
bool operator <(const Edge &other) const { return other.len < len; }
};
//int abbs(int a)
//{
// if (a < 0)
// return -a;
// return a;
//}
void dijkstra()
{
priority_queue<Edge> q;
memset(dis, 127, sizeof dis);
dis[1][s] = 0;
q.push(Edge(1, s, 0));
while (q.size())
{
Edge u = q.top();
q.pop();
int to = u.to, ne = u.ne;
if (vis[to][ne])
continue;
vis[to][ne] = true;
for (int i = 1; i <= m; i++)
{
int e = to + c[i];
if (e < 1 || e > n)
continue;
int len = llabs(i - ne) + llabs(c[i] * 2);
if (dis[to][ne] + len < dis[e][i])
{
dis[e][i] = dis[to][ne] + len;
if (!vis[e][i])
q.push(Edge(e, i, dis[e][i]));
}
}
}
}
signed main()
{
cin >> n >> m;
for (int i = 1; i <= m; i++)
{
cin >> c[i];
if (c[i] == 0)
s = i;
}
dijkstra();
for (int i = 1; i <= m; i++)
ans = min(ans, dis[n][i]);
if (ans >= 1e16)
cout << -1;
else
cout << ans;
Enoch2013;
}
- P9751 [CSP-J 2023] 旅游巴士
#include <bits/stdc++.h>
#define N 20005
#define INF 2147483647
using namespace std;
typedef pair<int, int> PII;
int n, m, k, u, v, ai[N], cnt = 0, pre[N], vis[N][105], dis[N][105];
struct Edge
{
int to, next, len;
} a[N];
void add(int from, int to, int len)
{
a[++cnt] = {to, pre[from], len};
pre[from] = cnt;
}
void dijkstra()
{
memset(vis, 0, sizeof vis);
memset(dis, 127, sizeof dis);
priority_queue<PII, vector<PII>, greater<PII> > q;
q.push({0, 1});
dis[1][0] = 0;
while (q.size())
{
PII u = q.top();
q.pop();
int dis_orz = u.first, p = u.second;
if (vis[p][dis_orz % k])
continue;
vis[p][dis_orz % k] = true;
if (p == n && !(dis_orz % k))
{
printf("%d", dis_orz);
exit(0);
}
for (int i = pre[p]; i; i = a[i].next)
{
int to = a[i].to, t = dis_orz;
if (t < a[i].len)
t = dis_orz + (a[i].len - dis_orz + k - 1) / k * k;
if (t + 1 < dis[to][(t + 1) % k])
{
dis[to][(t + 1) % k] = t + 1;
q.push({t + 1, to});
}
}
}
}
int main()
{
scanf("%d%d%d", &n, &m, &k);
for (int i = 1; i <= m; i++)
{
scanf("%d%d%d", &u, &v, &ai[i]);
add(u, v, ai[i]);
}
dijkstra();
printf("-1");
return 0;
}
No.6 最近公共祖先
#include <bits/stdc++.h>
#define N 500005
#define M 20
using namespace std;
int n, m, s, d[N], anc[N][M], dis[N];
vector<int> v[N];
void add(int x, int y)
{
v[x].push_back(y);
v[y].push_back(x);
}
void dfs(int u, int fa)
{
for (auto to : v[u])
{
if (to == fa)
continue;
d[to] = d[u] + 1;
anc[to][0] = u;
dfs(to, u);
}
}
void init()
{
for (int j = 1; j <= M - 2; j++)
for (int i = 1; i <= n; i++)
anc[i][j] = anc[anc[i][j - 1]][j - 1];
}
int LCA(int x, int y)
{
if (d[x] < d[y])
swap(x, y);
for (int i = M - 2; i >= 0; i--)
if (d[anc[x][i]] >= d[y])
x = anc[x][i];
if (x == y)
return x;
for (int i = M - 2; i >= 0; i--)
if (anc[x][i] != anc[y][i])
x = anc[x][i], y = anc[y][i];
return anc[x][0];
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n >> m >> s;
for (int i = 1, x, y; i < n; i++)
{
cin >> x >> y;
add(x, y);
}
d[s] = 1;
dfs(s, 0);
init();
while (m--)
{
int x, y;
cin >> x >> y;
cout << LCA(x, y) << endl;
}
return 0;
}
No.7 缩点
/*
强连通图:有向图,任意两个点都能互相到达。
强连通分量:有向图中的极大的强连通子图。
我们需要求出如下的信息:
1. 有几个强连通分量,它们的编号分别是几;
2. 每个强连通分量有几个点;
3. 每个点属于哪个强连通分量。
*/
#include <bits/stdc++.h>
#define N 100005
using namespace std;
int n, m, ans = 0, k = 0, cnt = 0, idx = 0, top = 0, u, v, val[N], dfn[N], low[N], dp[N], st[N], pre[N], bel[N];
bool ins[N];
struct Edge
{
int to, ne;
} a[N];
void add(int u, int v)
{
a[++k] = (Edge){v, pre[u]};
pre[u] = k;
}
void tarjan(int u)
{
dfn[u] = low[u] = ++cnt;
ins[st[++top] = u] = true;
for (int i = pre[u]; i; i = a[i].ne)
{
int to = a[i].to;
if (!dfn[to])
{
tarjan(to);
low[u] = min(low[u], low[to]);
}
else if (ins[to])
low[u] = min(low[u], dfn[to]);
}
if (dfn[u] == low[u])
{
int sum = 0;
int to;
idx++;
do
{
ins[to = st[top--]] = false;
bel[to] = idx;
sum += val[to];
for (int i = pre[to]; i; i = a[i].ne)
dp[idx] = max(dp[idx], dp[bel[a[i].to]]);
} while (to != u);
dp[idx] += sum;
ans = max(ans, dp[idx]);
}
return;
}
int main()
{
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i++)
scanf("%d", &val[i]);
for (int i = 1; i <= m; i++)
{
scanf("%d%d", &u, &v);
add(u, v);
}
for (int i = 1; i <= n; i++)
if (!dfn[i])
tarjan(i);
printf("%d", ans);
return 0;
}
No.8 割点(割顶)
#include <bits/stdc++.h>
#define N 100005
using namespace std;
int pre[N], tot = 1, dfn[N], low[N], st[N], top, cnt, deg_In[N], idx, n, m;
vector<int> vdcc[N];
struct node
{
int to, ne;
} a[N << 1];
void add(int b, int e)
{
a[++tot] = (node){e, pre[b]};
pre[b] = tot;
a[++tot] = (node){b, pre[e]};
pre[e] = tot;
}
void tarjan(int u, int lst)
{
dfn[u] = low[u] = ++cnt;
st[++top] = u;
for (int i = pre[u]; i; i = a[i].ne)
if (i != (lst ^ 1))
{
int to = a[i].to, vv;
if (!dfn[to])
{
tarjan(to, i);
low[u] = min(low[u], low[to]);
if (low[to] >= dfn[u])
{
idx++;
vdcc[idx].push_back(u);
++deg_In[u];
do
{
vv = st[top--];
vdcc[idx].push_back(vv);
++deg_In[vv];
} while (vv != to);
}
}
else
low[u] = min(low[u], dfn[to]);
}
}
int main()
{
cin >> n >> m;
for (int i = 1, b, e; i <= m; i++)
{
cin >> b >> e;
add(b, e);
}
for (int i = 1;i <= n; i++)
if (!dfn[i])
{
tarjan(i, -1);
top = 0;
}
int ans = 0;
for (int i = 1; i <= n; i++)
if (deg_In[i] > 1)
ans++;
cout << ans << endl;
for (int i = 1, lst = 0; i <= n; i++)
if (deg_In[i] > 1)
{
if (lst)
cout << ' ';
lst = true;
cout << i;
}
// putchar('\n');
/*
for (int i = 1; i <= idx; i++)
{
cout << i << ':';
for (auto j : vdcc[i])
cout << j << ' ';
cout << endl;
}
*/
return 0;
}
No.9 点双连通分量
#include <bits/stdc++.h>
#define N 2000005
using namespace std;
int pre[N], tot, dfn[N], low[N], st[N], top, cnt, deg_In[N], idx, n, m, root;
vector<int> vdcc[N];
struct node
{
int to, ne;
} a[N << 1];
void add(int b, int e)
{
a[tot] = (node){e, pre[b]};
pre[b] = tot++;
}
void tarjan(int u, int lst)
{
int son = 0;
dfn[u] = low[u] = ++cnt;
st[++top] = u;
for (int i = pre[u]; ~i; i = a[i].ne)
{
int to = a[i].to, vv;
if (!dfn[to])
{
son++;
tarjan(to, u);
low[u] = min(low[u], low[to]);
if (low[to] >= dfn[u])
{
idx++;
// ++deg_In[u];
do
{
vv = st[top--];
vdcc[idx].push_back(vv);
// ++deg_In[vv];
// add(idx + n, vv);
} while (vv != to);
vdcc[idx].push_back(u);
}
}
else if (i != (lst ^ 1))
low[u] = min(low[u], dfn[to]);
}
if (u == root && son == 0)
vdcc[++idx].push_back(u);
}
int main()
{
memset(pre, -1, sizeof pre);
cin >> n >> m;
for (int i = 1, b, e; i <= m; i++)
{
cin >> b >> e;
// if (b == e)
// continue;
add(b, e);
add(e, b);
}
for (root = 1; root <= n; root++)
if (!dfn[root])
{
top = 0;
tarjan(root, -1);
}
// int ans = 0;
// for (int i = 1; i <= n; i++)
// if (deg_In[i] > 1)
// ans++;
// cout << ans << endl;
// for (int i = 1, lst = 0; i <= n; i++)
// if (deg_In[i] > 1)
// {
// if (lst)
// cout << ' ';
// lst = true;
// cout << i;
// }
// putchar('\n');
cout << idx << endl;
for (int i = 1; i <= idx; i++)
{
// cout << i << endl;
cout << vdcc[i].size() << ' ';
for (auto j : vdcc[i])
cout << j << ' ';
cout << endl;
}
return 0;
}
No.10 边双连通分量
#include <bits/stdc++.h>
#define N 500005
using namespace std;
typedef pair<int, int> PII;
int n, m, u, v, cnt, dfn[N], low[N];
vector<PII> e[N];
vector<vector<int> > ans;
stack<int> st;
void tarjan(int x, int las)
{
dfn[x] = low[x] = ++cnt;
st.push(x);
for (auto 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();
}
ans.push_back(vec);
st.pop();
}
}
int main()
{
cin >> n >> m;
for (int i = 1; i <= m; i++)
{
cin >> u >> v;
e[u].push_back(make_pair(v, i << 1));
e[v].push_back(make_pair(u, i << 1 | 1));
}
for (int i = 1; i <= n; i++)
if (!dfn[i])
tarjan(i, 0);
cout << ans.size() << endl;
for (auto i : ans)
{
cout << i.size() << ' ';
for (auto j : i)
cout << j << ' ';
cout << endl;
}
return 0;
}
No.11 树上差分
#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 5, M = N << 1, L = 31;
// order[i]: 房间顺序,dep[i]: 每个点的深度,r[i]:第i个房间至少要放几个糖果
int n, k, order[N], pre[N], dep[N], r[N], f[N][L], lg[N];
struct Edge
{
int to, next;
} a[N];
void add(int u, int v)
{
a[++k] = {v, pre[u]};
pre[u] = k;
}
void dfs(int x, int fath)
{
dep[x] = dep[fath] + 1;
f[x][0] = fath;
for (int i = 1; i <= 30; i++)
f[x][i] = f[f[x][i - 1]][i - 1];
for (int i = pre[x]; i; i = a[i].next)
{
int to = a[i].to;
if (to == fath)
continue;
dfs(to, x);
}
}
int lca(int x, int y)
{
if (dep[x] < dep[y])
swap(x, y);
for (int i = 30; i >= 0; i--)
if (dep[f[x][i]] >= dep[y])
x = f[x][i];
if (x == y)
return x;
for (int i = 30; i >= 0; i--)
if (f[x][i] != f[y][i])
x = f[x][i], y = f[y][i];
return f[x][0];
}
void solve(int x, int fath)
{
for (int i = pre[x]; i; i = a[i].next)
{
int to = a[i].to;
if (to == fath)
continue;
solve(to, x);
r[x] += r[to];
}
}
signed main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n;
for (int i = 1; i <= n; i++)
cin >> order[i];
int x, y;
for (int i = 1; i < n; i++)
{
cin >> x >> y;
add(x, y), add(y, x);
}
dfs(1, 0);
for (int i = 1; i < n; i++)
{
int u = order[i], v = order[i + 1], lc = lca(u, v);
r[f[lc][0]]--, r[lc]--;
r[u]++, r[v]++;
}
solve(1, 0);
for (int i = 2; i <= n; i++)
r[order[i]]--;
for (int i = 1; i <= n; i++)
cout << r[i] << endl;
}