2026-08-16 20:20:00 ~ 2026-10-31 20:25:00
来源:P12862 [NERC 2020 Online] Miser
难度:黄
题意里面给定了约束关系,比如人员1在第1天,第3天,第5天都出现了
第 1 天的标牌一定比第 3 天的小, 第 3 天的标牌一定比第 5 天的小。
按照拓扑的层数进行分层,同一层的可以使用一张标牌,那么需要的标牌的最少数量取决于层数。
所以本题的问题转换为求解最长路。
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 7;
int n;
int pre[maxn];
int in[maxn];
int dp[maxn];
int ans;
vector<int> G[maxn];
void topo()
{
queue<int> q;
q.push(0);
while (q.size())
{
int u = q.front();
q.pop();
for (int i = 0; i < G[u].size(); i++)
{
int v = G[u][i];
in[v]--;
dp[v] = max(dp[v], dp[u] + 1);
if (!in[v])
{
q.push(v);
}
}
}
}
int main()
{
cin >> n;
for (int i = 1; i <= n; ++i)
{
int k;
cin >> k;
while (k--)
{
int x;
cin >> x;
G[pre[x]].push_back(i);
in[i]++;
pre[x] = i;
}
}
topo();
for (int i = 1; i <= n; ++i)
{
ans = max(ans, dp[i]);
}
cout << ans << '\n';
return 0;
}
来源:P1347 [ECNA 2001] 排序
难度:绿
情况2:有环说明一定矛盾
情况3:如果当前出现
拓扑序不唯一
采用的是最长路的写法,如果最长路等于n,说明此时就是唯一的拓扑序。
输出个数不够
都有可能是无法确定, 但是具体的需要等到所有输入结束才能定夺。
如果上述两种情况都不满足,说明得到了正确的拓扑序。
#include <bits/stdc++.h>
using namespace std;
const int maxn = 130;
int in[maxn], IN[maxn], dp[maxn], n, m, tot;
char ans[maxn];
bool vis[maxn], G[maxn][maxn];
int topo()
{
int cnt = 0;
queue<int> q;
for (int i = 'A'; i <= 'Z'; i++)
{
IN[i] = in[i];
}
for (int i = 'A'; i <= 'Z'; i++)
{
if (vis[i] == true && IN[i] == 0)
{
q.push(i);
dp[i] = 1;
}
}
while (!q.empty())
{
int cur = q.front();
q.pop();
ans[++cnt] = cur;
for (int i = 'A'; i <= 'Z'; i++)
{
if (G[cur][i] == false)
{
continue;
}
dp[i] = max(dp[i], dp[cur] + 1);
IN[i]--;
if (IN[i] == 0)
{
q.push(i);
}
}
}
if (cnt < tot) // 矛盾 有环
{
return 2;
}
if (tot < n || dp[ans[cnt]] != n)
// 目前出队人数不够 或者 可以求解的关系是唯一的拓扑
{
return 3;
}
return 1; // 正常返回
}
void print()
{
for (int i = 1; i <= n; i++)
{
cout << ans[i];
}
cout << ".";
}
int main()
{
cin >> n >> m;
for (int i = 1; i <= m; i++)
{
char a, b;
cin >> a >> b >> b;
if (vis[a] == false)
{
vis[a] = true, tot++;
}
if (vis[b] == false)
{
vis[b] = true, tot++;
}
if (G[a][b] == true) // 重复了
{
continue;
}
in[b]++;
G[a][b] = true;
int flag = topo();
if (flag == 1)
{
cout << "Sorted sequence determined after " << i << " relations: ";
print();
return 0;
}
else if (flag == 2)
{
cout << "Inconsistency found after " << i << " relations.";
return 0;
}
}
cout << "Sorted sequence cannot be determined.";
return 0;
}
来源:P14273 [ROI 2014 Day1] 鲁滨逊与鳄鱼
首先按照题意建边,比如样例三,根据箭头的指向建立反边,因为此时这个方向是否能出去,取决于对应这个方向遇到的所有箭头是否提前出去。
直接考虑暴力建边,然后拓扑,可以得到
在上述的基础上,首先考虑优化建边。
优化规则:向前扫,遇到鳄鱼 Y 就加边;但一旦 Y 与 X 同向,加完这条边立刻停。
正确性:Y 在 X 正前方且同向,Y 自己的依赖已经包含了「Y 更前面所有鳄鱼」。所以 X→Y 通过传递性(X 等 Y,Y 等更前)就隐含了 X 等更前面的全部鳄鱼,不丢任何约束。
那么这样优化之后的边的数量是
因为固定一个方向(如「向东」),该行中每个有鳄鱼的格子,至多被一只向东的鳄鱼连入一次。
这种方式我们叫做 后缀优化建图。
优化建边之后,空间仍然会超限,所以在建图的时候,点的编号采用 short 类型, 范围是 -32768到32767 ,请注意此时并非所有数据都能开,比如这个拓扑计数的 ans 可能会超过这个范围。
#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e3 + 7;
int n, m;
int in[maxn][maxn];
char a[maxn][maxn];
struct Point
{
int x, y;
};
vector<Point> G[maxn][maxn];
void add_edge(int x, int y, char c)
{
if (c == 'S')
{
for (int i = x + 1; i <= n; i++)
{
if (a[i][y] != '.')
{
G[i][y].push_back({x, y});
in[x][y]++;
}
if (a[i][y] == a[x][y])
{
break;
}
}
}
if (c == 'W')
{
for (int i = y - 1; i >= 1; i--)
{
if (a[x][i] != '.')
{
G[x][i].push_back({x, y});
in[x][y]++;
}
if (a[x][i] == a[x][y])
{
break;
}
}
}
if (c == 'E')
{
for (int i = y + 1; i <= m; i++)
{
if (a[x][i] != '.')
{
G[x][i].push_back({x, y});
in[x][y]++;
}
if (a[x][i] == a[x][y])
{
break;
}
}
}
if (c == 'N')
{
for (int i = x - 1; i >= 1; i--)
{
if (a[i][y] != '.')
{
G[i][y].push_back({x, y});
in[x][y]++;
}
if (a[i][y] == a[x][y])
{
break;
}
}
}
}
int ans = 0;
void topo()
{
queue<Point> Q;
for (int i = 1; i <= n; ++i)
{
for (int j = 1; j <= m; ++j)
{
add_edge(i, j, a[i][j]);
if (!in[i][j] && a[i][j] != '.')
{
Q.push({i, j});
}
}
}
while (!Q.empty())
{
int x = Q.front().x;
int y = Q.front().y;
Q.pop();
ans++;
for (int i = 0; i < G[x][y].size(); i++)
{
int dx = G[x][y][i].x;
int dy = G[x][y][i].y;
in[dx][dy]--;
if (!in[dx][dy])
{
Q.push({dx, dy});
}
}
}
}
signed main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n >> m;
for (int i = 1; i <= n; ++i)
{
for (int j = 1; j <= m; ++j)
{
cin >> a[i][j];
}
}
topo();
cout << ans;
return 0;
}
#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e3 + 7;
int n, m;
int in[maxn][maxn];
char a[maxn][maxn];
struct Point
{
short x, y;
};
vector<Point> G[maxn][maxn];
void add_edge(short x, short y, char c)
{
if (c == 'S')
{
for (short i = x + 1; i <= n; i++)
{
if (a[i][y] != '.')
{
G[i][y].push_back({x, y});
in[x][y]++;
}
if (a[i][y] == a[x][y])
{
break;
}
}
}
if (c == 'W')
{
for (short i = y - 1; i >= 1; i--)
{
if (a[x][i] != '.')
{
G[x][i].push_back({x, y});
in[x][y]++;
}
if (a[x][i] == a[x][y])
{
break;
}
}
}
if (c == 'E')
{
for (short i = y + 1; i <= m; i++)
{
if (a[x][i] != '.')
{
G[x][i].push_back({x, y});
in[x][y]++;
}
if (a[x][i] == a[x][y])
{
break;
}
}
}
if (c == 'N')
{
for (short i = x - 1; i >= 1; i--)
{
if (a[i][y] != '.')
{
G[i][y].push_back({x, y});
in[x][y]++;
}
if (a[i][y] == a[x][y])
{
break;
}
}
}
}
int ans = 0;
void topo()
{
queue<Point> Q;
for (short i = 1; i <= n; ++i)
{
for (short j = 1; j <= m; ++j)
{
add_edge(i, j, a[i][j]);
if (!in[i][j] && a[i][j] != '.')
{
Q.push({i, j});
}
}
}
while (!Q.empty())
{
short x = Q.front().x;
short y = Q.front().y;
Q.pop();
ans++;
for (int i = 0; i < G[x][y].size(); i++)
{
short dx = G[x][y][i].x;
short dy = G[x][y][i].y;
in[dx][dy]--;
if (!in[dx][dy])
{
Q.push({dx, dy});
}
}
}
}
signed main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n >> m;
for (int i = 1; i <= n; ++i)
{
for (int j = 1; j <= m; ++j)
{
cin >> a[i][j];
}
}
topo();
cout << ans;
return 0;
}
来源:P7077 [CSP-S 2020] 函数调用
难度:蓝↑
相似题:
(简化版本):P10374 [AHOI2024 初中组 / 科大国创杯初中组 2024] 操作
(中等进阶版本):B4161 [BCSP-X 2024 12 月小学高年级组] 操作序列
函数之间的相互调用关系存在依赖性,采用拓扑关系将整个函数的调用顺序进行梳理。
下述我们将会用到样例 2 进行解释。
文字表达有限,听我讲解!!!
#include <bits/stdc++.h>
const int maxn = 1e5 + 7;
const int mod = 998244353;
#define int long long
using namespace std;
int n, m, q;
int a[maxn];
int in[maxn];
int fun[maxn];
int p[maxn];
int v[maxn];
int tp[maxn];
int num;
int mul[maxn];
int cnt[maxn];
vector<int> G[maxn];
queue<int> Q;
void topo()
{
for (int i = 1; i <= m; i++)
{
if (!in[i])
{
Q.push(i);
}
}
while (!Q.empty())
{
int u = Q.front();
Q.pop();
tp[++num] = u;
for (int i : G[u])
{
in[i]--;
if (!in[i])
{
Q.push(i);
}
}
}
}
void getmul() // 从后往前 自下往上
{
for (int i = m; i >= 1; i--)
{
int u = tp[i];
for (int j : G[u])
{
mul[u] = mul[u] * mul[j] % mod;
}
}
}
void getsum()
{
for (int i = 1; i <= m; i++)
{
int u = tp[i], cur = cnt[u];
for (int j : G[u])
{
cnt[j] = (cnt[j] + cur % mod) % mod;
cur = cur * mul[j] % mod;
}
}
}
signed main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n;
for (int i = 1; i <= n; i++)
{
cin >> a[i];
}
cin >> m;
for (int i = 1; i <= m; i++)
{
int op;
cin >> op;
if (op == 1)
{
cin >> p[i] >> v[i];
mul[i] = 1;
}
else if (op == 2)
{
cin >> v[i];
mul[i] = v[i];
}
else
{
int c, v;
cin >> c;
mul[i] = 1;
while (c--)
{
cin >> v;
G[i].emplace_back(v);
in[v]++;
}
}
}
for (int i = 1; i <= m; i++)
{
reverse(G[i].begin(), G[i].end());
}
topo();
getmul();
cin >> q;
int cur = 1;
for (int i = 1; i <= q; i++)
{
cin >> fun[i];
}
for (int i = q; i >= 1; i--)
{
int u = fun[i];
cnt[u] = (cnt[u] + cur) % mod;
cur = cur * mul[u] % mod;
}
getsum();
for (int i = 1; i <= n; i++)
{
a[i] = a[i] * cur % mod;
}
for (int i = 1; i <= m; i++)
{
if (p[i])
{
a[p[i]] = (a[p[i]] + v[i] * cnt[i] % mod) % mod;
}
}
for (int i = 1; i <= n; i++)
{
cout << a[i] << " ";
}
return 0;
}