C++ 算法竞赛、03 周赛篇 | AcWing 第4场周赛

AcWing 第4场周赛

竞赛 - AcWing

3694 A还是B

3694. A还是B - AcWing题库

image-20230910184340659

简单题

#include <algorithm>
#include <cstring>
#include <iostream>

using namespace std;

int n;
int a, b;

int main() {
    cin.tie(0);
    char c;
    cin >> n;
    for (int i = 0; i < n; i++) {
        cin >> c;
        if (c == 'A')
            a++;
        else
            b++;
    }
    if (a == b)
        puts("T");
    else if (a > b)
        puts("A");
    else
        puts("B");

    return 0;
}

3695 扩充序列

3695. 扩充序列 - AcWing题库

image-20230910190132424

考查递归。可以发现最终序列除中点,左右两段都是相等的,可以依据这个特性来递归

超级长的序列缩小 \(log_2n\) 次,每次将 k 坐标映射到缩小的各个序列上,k肯定是其中一个序列的中点

#include <algorithm>
#include <cstring>
#include <iostream>

using namespace std;

typedef long long LL;

int n;
LL k;
LL counts = 1;

int find() {
    LL mid = (counts + 1) / 2;
    if (mid == k) return n + 1;
    if (k > mid) k = k - mid;
    n--;
    counts = (counts - 1) / 2;
    return find();
}

int main() {
    cin >> n >> k;
    for (int i = 1; i <= n; i++) counts = counts * 2 + 1;
    int res = find();
    cout << res;
    return 0;
}

3696⭐构造有向无环图

3696. 构造有向无环图 - AcWing题库

image-20230910190734060

考查拓扑序列

拓扑序列

理论知识 有向图的拓扑序列 | 第三章 图论 - 小能日记 - 博客园 (cnblogs.com)

d 数组存储每个点的入度,q 队列用于拓扑排序,以下是邻接表存储图的拓扑排序法(思路就是逐一删除点,再将入度为0的点加入队列)

  • 若队尾 tt == n-1,意味着给定图的 \(n\) 个点均已入队,\(q[0]\)\(q[n-1]\) 的排列就是一个拓扑序列
  • 若队尾 tt != n-1,意味着只有 \(tt+1\) 个点入队,此时一定有环(没有入度为0的点),不是DAG(有向无环图 | 拓扑图)
int h[N], e[N], ne[N], idx;
int q[N];
bool topsort() {
    int hh = 0, tt = -1;
    for (int i = 1; i <= n; i++)
        if (!d[i]) q[++tt] = i;
    while (hh <= tt) {
        int t = q[hh++];
        for (int i = h[t]; ~i; i = ne[i]) {
            int j = e[i];
            if (--d[j] == 0) q[++tt] = j;
        }
    }
    return tt == n - 1;
}

题解

  • 先考虑有向边
    • 有环:一定无解
    • 无环:直接按照拓扑序列,从前往后添加边(不会影响该拓扑序列结果)
  • 难点
    • 稀疏图用邻接表存
    • 测试点很多,数组很大,用memset可能超时;可以限制初始化字节数量减少时间,如 (n + 1) * 4 (4是int字节数)
    • 每个测试点初始化不要漏掉 h(邻接表)、d(入度)、k(无向边个数)、idx(节点唯一编号)
    • 根据拓扑序列映射各个点的前后位置,即 pos 数组,从而判断无向边 a,b 的先后次序

#include <algorithm>
#include <cstring>
#include <iostream>

using namespace std;

int const N = 2e5 + 10;
int h[N], e[N], ne[N], idx;
int d[N];    // 存入度
int pos[N];  // 存储每个点在拓扑排序的下标
int t;
int n, m, k;  // k+1 存储无向边个数

// 存无向边
struct Edge {
    int a, b;
} edges[N];

void add(int a, int b) {
    e[idx] = b;
    ne[idx] = h[a];
    h[a] = idx++;
}

int q[N];
bool topsort() {
    int hh = 0, tt = -1;
    for (int i = 1; i <= n; i++)
        if (!d[i]) q[++tt] = i;
    while (hh <= tt) {
        int t = q[hh++];
        for (int i = h[t]; ~i; i = ne[i]) {
            int j = e[i];
            if (--d[j] == 0) q[++tt] = j;
        }
    }
    return tt == n - 1;
}

int main() {
    cin.tie(0);
    cin >> t;
    while (t--) {
        cin >> n >> m;
        memset(h, -1, (n + 1) * 4);
        memset(d, 0, (n + 1) * 4);
        k = idx = 0;
        while (m--) {
            int t, a, b;
            cin >> t >> a >> b;
            if (!t)
                edges[k++] = {a, b};
            else {
                add(a, b);
                d[b]++;
            }
        }
        if (!topsort())
            puts("NO");
        else {
            puts("YES");
            for (int i = 1; i <= n; i++)
                for (int j = h[i]; ~j; j = ne[j])
                    cout << i << " " << e[j] << endl;

            for (int i = 0; i < n; i++) pos[q[i]] = i;
            for (int i = 0; i < k; i++) {
                int a = edges[i].a, b = edges[i].b;
                if (pos[a] > pos[b]) swap(a, b);
                cout << a << " " << b << endl;
            }
        }
    }
    return 0;
}

拓扑序列资料补充

还不会拓扑排序?看这一篇就够了_Iareges的博客-CSDN博客

posted @ 2023-09-10 20:50  小能日记  阅读(12)  评论(0编辑  收藏  举报