我们把“u点能够到达的最大点”转化为反向图中能到达u点的所有点里的最大值,可知缩点后满足无后效性。val[i]的初值设为连通分量i中的最大点。反向存图,tarjan缩点,拓扑序dp即可。
- #include <iostream>
- #include <cstdio>
- #include <queue>
- #define maxn 100100
- using namespace std;
- int n, m;
- void read(int &x) {
- x = 0;
- char ch = getchar();
- while (!isdigit(ch))
- ch = getchar();
- while (isdigit(ch))
- x = x * 10 + (ch ^ 48),
- ch = getchar();
- return;
- }
- struct E {
- int to, nxt;
- } edge[maxn], edge2[maxn];
- int head[maxn], top, head2[maxn], top2;
- inline void insert(int u, int v) {
- edge[++top] = (E) {v, head[u]};
- head[u] = top;
- }
- inline void insert2(int u, int v) {
- edge2[++top2] = (E) {v, head2[u]};
- head2[u] = top2;
- }
- int low[maxn], dfn[maxn], timer, c[maxn], cnt, sta[maxn], stp;
- bool ins[maxn];
- int val[maxn];
- void tarjan(int u) {
- low[u] = dfn[u] = ++ timer;
- sta[++stp] = u, ins[u] = true;
- for (int i = head[u]; i; i = edge[i].nxt) {
- int v = edge[i].to;
- if (!dfn[v]) {
- tarjan(v);
- low[u] = min(low[u], low[v]);
- } else if (ins[v])
- low[u] = min(low[u], dfn[v]);
- }
- if (dfn[u] == low[u]) {
- ++cnt;
- int x;
- do {
- x = sta[stp--];
- ins[x] = false;
- c[x] = cnt;
- val[cnt] = max(val[cnt], x);
- } while (x != u);
- }
- }
- int ind[maxn];
- void build() {
- for (int u = 1; u <= n; ++u)
- for (int i = head[u]; i; i = edge[i].nxt) {
- int v = edge[i].to;
- if (c[u] != c[v])
- insert2(c[u], c[v]), ++ind[c[v]];
- }
- }
- void dp() {
- queue<int> que;
- for (int i = 1; i <= cnt; ++i)
- if (!ind[i]) que.push(i);
- while (!que.empty()) {
- int u = que.front(); que.pop();
- for (int i = head2[u]; i; i = edge2[i].nxt) {
- int v = edge2[i].to;
- val[v] = max(val[v], val[u]);
- --ind[v];
- if (!ind[v])
- que.push(v);
- }
- }
- return;
- }
- int main() {
- read(n), read(m);
- int u, v;
- for (int i = 1; i <= m; ++i) {
- read(u), read(v);
- insert(v, u);
- }
- for (int i = 1; i <= n; ++i)
- if (!dfn[i])
- tarjan(i);
- build();
- dp();
- for (int i = 1; i <= n; ++i)
- printf("%d ", val[c[i]]);
- return 0;
- }
至此luogu上真哥留下的缩点习题全部完成。晚上更新对最小树形图(朱刘算法)的理解。