8.16-拓扑专题测试-401

2026-08-16 20:20:00 ~ 2026-10-31 20:25:00

解析

T1

来源:P12862 [NERC 2020 Online] Miser

难度:黄

思路:

实现代码参考:

#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;
}

T2

来源:P1347 [ECNA 2001] 排序

难度:绿

思路:

实现代码参考:

#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;
}

T3

来源:P14273 [ROI 2014 Day1] 鲁滨逊与鳄鱼

思路:

暴力思路:

优化思路:

实现代码参考:

暴力代码

#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;
}

T4

来源:P7077 [CSP-S 2020] 函数调用

难度:蓝↑

相似题:

(简化版本):P10374 [AHOI2024 初中组 / 科大国创杯初中组 2024] 操作

(中等进阶版本):B4161 [BCSP-X 2024 12 月小学高年级组] 操作序列

思路:

文字表达有限,听我讲解!!!

实现代码参考:

#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;
}