跳到正文

个人记录

YL第四周新课+习题课总结

XieMuzhu约 22 分钟
原文

这是 2026年10月2日 存档的第 1 版,不是当前版本。查看当前版本

文章信息与版本

文章信息

发布
2026年10月2日
收录
2026年10月2日
更新
2026年11月11日
篇幅
约 22 分钟

版本

  1. v2当前
  2. v1

周三习题课

  • 比赛日期:2026/9/302026/9/30
  • 比赛时长:3h3h
  • 比赛得分:308pts308pts
  • 比赛排名:No.1No.1

比赛整体回顾

也是非常的激动好吧,第一次断网拿 No.1No.1,做题顺序为:B→C→A→DB \to C \to A \to D,自认为难度为(由低到高):B→A→C→DB \to A \to C \to D,AA 题花费了亿点点时间和耐心……

题目复盘

A - Family Tree B

题面戳这儿

分数

88\color{green} \text{88} ptspts,WA\color{red} \text{WA} 22 个点

题意

给定一个家谱树,每父亲和叶子节点的连线代表一对母女关系,对于 x,yx,y 有如下定义:

  • 若 x,yx,y 的母亲是同一个,则称他们俩是 SIBLINGS 的关系

  • 若 x,yx,y 为直系关系:

    • xx 是 yy 的祖先:

      • 母亲:称 x is the mother of y
      • 母亲的母亲(外祖母):称 x is the grand-mother of y
      • 母亲的母亲的母亲(外曾外祖母):称 x is the great-grand-mother of y
      • 母亲的母亲的母亲的母亲(外曾外曾外祖母):称 x is the great-great-grand-mother of y
      • …\dots …\dots,以此类推
    • yy 是 xx 的祖先:同理,将 x,yx,y 位置调换即可

  • 若 xx 和 yy 不是直系关系,但是是姨母关系:

    • xx 是 yy 的祖先:

      • xx 是 yy 的外祖母的孩子:称 x is the aunt of y
      • xx 是 yy 的外曾外祖母的孩子:称 x is the great-aunt of y
      • xx 是 yy 的外曾外曾外祖母的孩子:称 x is the great-great-aunt of y
      • …\dots …\dots,以此类推
    • yy 是 xx 的祖先:同理,将 x,yx,y 位置调换即可

  • 若 xx 和 yy 不是直系关系也不是姨母关系,但是有亲戚关系,就称 xx 和 yy 是 COUSINS 关系

  • 如果 xx 和 yy 没有亲戚关系,就称 xx 和 yy 是 NOT RELATED 关系

给定两个人 x,yx,y,问他们俩之间的关系是什么

考场思路

考虑到名字是一个字符串,字符串不太好处理,所以我们可以给这些奶牛的名字编一个号,xx 为 11,yy 为 22,其他的依次顺下去。

我们可以把这个家谱树就想象成我们的祖宗十八代,我们维护一个 num_ge(int num, int r) 函数(num_generation),代表 rr 的第 numnum 代母亲的编号是多少,方便后续的遍历计算。

直系关系其实很好求,这个关系他都是有规律的,从第二代开始,他的关系就是 i−2i - 2 个 great-++grand-mother,所以我们只需要从 22 开始遍历 xx 与 yy 的每一代祖母,然后判断他和 yy 与 xx 是否相等。

姨母关系其实也差不多,就是看一下 xx 与 yy 的每一代祖母,然后判断他和 yy 与 xx 的母亲是否相等,如果相等就可以输出 i−2i - 2 个 great-++aunt。

接着就是处理是否有亲戚关系,可以使用并查集做。

考场代码

C++
#include <bits/stdc++.h>
using namespace std;
const int N = 1e2 + 5;
int n, cnt = 2, fa[N];
string b, s, x, y;
map <string, int> id;
int num_ge(int num, int r){
    for(int i = 1; i <= num; i++)
        r = fa[r];
    return r;
}
int find(int u){
    if(fa[u] == u) return fa[u];
    return find(fa[u]);
}
int main(){
    cin >> n >> b >> s;
    id[b] = 1, id[s] = 2;
    for(int i = 1; i <= n; i++){
        cin >> x >> y;
        if(!id[x]) id[x] = ++cnt;
        if(!id[y]) id[y] = ++cnt;
        fa[id[y]] = id[x];
    }
    if(fa[1] == fa[2] && fa[1]){
        cout << "SIBLINGS";
        return 0;
    }
    if(fa[1] == 2){
        cout << s << " is the mother of " << b;
        return 0;
    }
    if(fa[2] == 1){
        cout << b << " is the mother of " << s;
        return 0;
    }
    for(int i = 2; i <= n; i++){
        if(num_ge(i, 1) == 2){
            cout << s << " is the ";
            for(int j = 1; j <= i - 2; j++)
                cout << "great-";
            cout << "grand-mother of " << b;
            return 0;
        }
        if(num_ge(i, 2) == 1){
            cout << b << " is the ";
            for(int j = 1; j <= i - 2; j++)
                cout << "great-";
            cout << "grand-mother of " << s;
            return 0;
        }
        if(num_ge(i, 1) == fa[2] && fa[2]){
            cout << s << " is the ";
            for(int j = 1; j <= i - 2; j++)
                cout << "great-";
            cout << "aunt of " << b;
            return 0;
        }
        if(num_ge(i, 2) == fa[1] && fa[1]){
            cout << b << " is the ";
            for(int j = 1; j <= i - 2; j++)
                cout << "great-";
            cout << "aunt of " << s;
            return 0;
        }
    }
    if(find(1) == find(2) && find(1)) cout << "COUSINS";
    else cout << "NOT RELATED";
    return 0;
}

错因

在处理 COUSINS 和 NOT RELATED 关系时,我用了一个并查集看看他们有没有公共祖先,但布兹道哪里出问题了,就 WA\color{red} \text{WA} 了 22 个点。

补题思路

借鉴了一下同桌大佬的思路,前面部分都是对的,但是在处理 COUSINS 和 NOT RELATED 关系时,我们可以考虑维护一个 f 数组,如果 f[i] = true,就代表 ii 是 xx 的祖先,再在 yy 的祖先中遍历一遍,如果找到了一个祖先 jj 使得 f[j] = true,那么就说明 xx 和 yy 有公共祖先 jj,就可以输出 COUSINS,否则输出 NO RELATED。

标准思路

  • Step①Step①:将奶牛名字编号
  • Step②Step②:按顺序求出 x,yx,y 的祖先列表
  • Step③Step③:找到最近公共祖先,根据最近公共祖先,判断每一种情况

补题代码

C++
#include <bits/stdc++.h>
using namespace std;
const int N = 1e2 + 5;
int n, cnt = 2, fa[N];
map <string, int> id;
string b, s, x, y;
bool f[N];
int num_ge(int num, int r){
    for(int i = 1; i <= num; i++)
        r = fa[r];
    return r;
}
int main(){
    cin >> n >> b >> s;
    id[b] = 1, id[s] = 2;
    for(int i = 1; i <= n; i++){
        cin >> x >> y;
        if(!id[x]) id[x] = ++cnt;
        if(!id[y]) id[y] = ++cnt;
        fa[id[y]] = id[x];
    }
    if(fa[1] == fa[2] && fa[1]){
        cout << "SIBLINGS";
        return 0;
    }
    if(fa[1] == 2){
        cout << s << " is the mother of " << b;
        return 0;
    }
    if(fa[2] == 1){
        cout << b << " is the mother of " << s;
        return 0;
    }
    for(int i = 2; i <= n; i++){
        if(num_ge(i, 1) == 2){
            cout << s << " is the ";
            for(int j = 1; j <= i - 2; j++)
                cout << "great-";
            cout << "grand-mother of " << b;
            return 0;
        }
        if(num_ge(i, 2) == 1){
            cout << b << " is the ";
            for(int j = 1; j <= i - 2; j++)
                cout << "great-";
            cout << "grand-mother of " << s;
            return 0;
        }
        if(num_ge(i, 1) == fa[2] && fa[2]){
            cout << s << " is the ";
            for(int j = 1; j <= i - 2; j++)
                cout << "great-";
            cout << "aunt of " << b;
            return 0;
        }
        if(num_ge(i, 2) == fa[1] && fa[1]){
            cout << b << " is the ";
            for(int j = 1; j <= i - 2; j++)
                cout << "great-";
            cout << "aunt of " << s;
            return 0;
        }
    }
    int a = 1, b = 2;
    while(a){
        f[a] = true;
        a = fa[a];
    }
    while(b){
        if(f[b]){
            cout << "COUSINS";
            return 0;
        }
        b = fa[b];
    }
    cout << "NOT RELATED";
    return 0;
}

收获

花了 1h1h 干出了 88\color{green} \text{88} ptspts,收获颇满。首先呢是这种家谱树类的问题其实就是可以把它转化成现实生活中我们所说的“族谱”,然后就很好做了。关于我是怎么想到的呢……(我把奶牛看成了人,然后就误打误撞的做出来了 88\color{green} \text{88} ptspts,也是悲喜交加好吧)

题目回顾

这道题有亿点点废手总结也很废手,不过还是想想想把它磕出来了,还是比较满意的,就是现在还没有搞清楚为啥并查集会错。

B - The Great Revegetation B

题面戳这儿

分数

100\color{green} \text{100} ptspts,Accepted\color{green} \text{Accepted}

题意

给定 mm 对 x,yx,y 位置,要求求出一个字典序最小的由 1,2,3,41,2,3,4 构成的序列,使得每对 x,yx,y 位置在序列中代表的数都不同

考场思路

这题目能是个黄题,也是斐长的惊讶好吧,就是暴力,搞一个三重循环,应为他只要找字典序最小的满足要求的,所以对于序列上的每一个位置,直接枚举 11~44,如果满足要求就直接 break 掉(类似于 dfsdfs 里的最优性剪枝),然后对于每一个位置枚举到的数,再从 11 循环到 mm,检查一下每一组是否不同即可。

考场代码

C++
#include <bits/stdc++.h>
using namespace std;
const int N = 2e2 + 5;
int n, m, g[N], a[N], b[N];
int main(){
    cin >> n >> m;
    for(int i = 1; i <= m; i++)
        cin >> a[i] >> b[i];
    for(int i = 1; i <= n; i++)
        for(int j = 1; j <= 4; j++){
            bool ok = true;
            g[i] = j;
            for(int k = 1; k <= m; k++){
                if(a[k] > i || b[k] > i) continue;
                if(g[a[k]] == g[b[k]]){
                    ok = false;
                    break;
                }
            }
            if(ok){
                cout << j;
                break;
            }
            else g[i] = 0;
        }
    return 0;
}

标准思路

基本一致其实是一模一样

收获

就是一个 brute force,没有什么收获,主要就是要仔细,不然就会像很惨的伍*泽一样错失 2020 分。

题目回顾

再说一次:这道题能是黄题,也是斐长的惊讶,自认为这道题是 入门\color{red} \text{入门}

C - Swapity Swapity Swap S

题面戳这儿

分数

50\color{gold} \text{50} ptspts,TLE\color{black} \text{TLE} 55 个点

题意

给定一个序列 1,2,3,…,n1,2,3,\dots,n,对这个序列进行 kk 组操作:每一组又分为 mm 次,每一次可以将对应的 [li,ri][l_i,r_i] 进行翻转,问 kk 组操作完之后的序列长什么样

考场思路

暴力也是对龙杞瑞的999ms卡过去了心生佩服(注意到全班只有一个人没用暴力做)

考场代码

C++
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
int n, m, k, a[N], l[N], r[N];
int main(){
    ios::sync_with_stdio(0);
    cin.tie(0), cout.tie(0);
    cin >> n >> m >> k;
    for(int i = 1; i <= n; i++)
        a[i] = i;
    for(int i = 1; i <= m; i++)
        cin >> l[i] >> r[i];
    for(int i = 1; i <= k; i++)
        for(int j = 1; j <= m; j++)
            reverse(a + l[j], a + r[j] + 1);
    for(int i = 1; i <= n; i++)
        cout << a[i] << '\n';
    return 0;
}

错因

这还要讲嘛,肯定过不去呀……(绷住)

标准思路

不难发现,在操作过程中不同位置之间是互不影响的,所以可以逐个位置进行分析。

对于每个位置,我们可以计算出一组操作后它的位置,考虑到这些操作是可逆的,所以经过若干次操作之后,肯定是会回到一开始的位置,这些位置便构成了一个环。

所以我们只需要找到每一个环,然后利用同余就可以快速求出 kk 组操作后的位置,最后输出答案即可。

补题代码

C++
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
int n, m, k, p, l[N], r[N], a[N], ans[N];
void find(int goal, int now, int cnt){
    if(goal == now && cnt){
        p = cnt;
        return;
    }
    find(goal, a[now], cnt + 1);
}
int store(int now, int cnt){
    if(!cnt) return now;
    return store(a[now], cnt - 1);
}
void work(int goal, int now1, int now2, int cnt){
    if(now1 == goal && cnt) return;
    if(now1 != goal) ans[now1] = a[ans[now2]];
    work(goal, a[now1], now1, cnt + 1);
}
int main(){
    cin >> n >> m >> k;
    for(int i = 1; i <= m; i++)
        cin >> l[i] >> r[i];
    for(int i = 1; i <= n; i++)
        a[i] = i;
    for(int i = 1; i <= m; i++)
        while(l[i] < r[i]){
            swap(a[l[i]], a[r[i]]);
            l[i]++, r[i]--;
        }
    for(int i = 1; i <= n; i++){
        if(!ans[i]){
            find(i, i, 0);
            ans[i] = store(i, k % p);
            work(i, i, i, 0);
        }
        cout << ans[i] << '\n';
    }
    return 0;
}

收获

这道题是个绿题,也有点惊讶,感觉可以降黄,想到不难想,就是代码稍稍有那么一点点难实现

题目回顾

D - Triangles S

题面戳这儿

分数

70\color{orange} \text{70} ptspts,TLE\color{black} \text{TLE} 33 个点

题意

给定平面上 nn 个整数点,求所有边平行于 x,yx,y 轴的三角形的总面积的 22 倍 mod  109+7\mod 10^9 + 7

考场思路

首先因为有负数,所以考虑坐标偏移,给每个数都加上 10410^4,不然就会像我的同桌一样遗憾的 RE\color{purple} \text{RE},然后就可以使用底 ×× 高,用一个 vectorvector 存储与位置 ii 的 xx 轴相等的位置,再用一个 vectorvector 存储与位置 ii 的 yy 轴相等的位置,然后枚举每个点,那么底就是存 yy 轴的 vectorvector 里面的所有点的 xx 轴与当前位置的 xx 轴的坐标差,高就是存 xx 轴的 vectorvector 里面的所有点的 yy 轴与当前位置的 yy 轴的坐标差,然后再愉快的使用 a⋅b2\dfrac{a \cdot b}{2},注意到最后要整体 ×2×2,所以就可以把那个 ÷2÷2 给抵消掉,就变成了底乘高模 109+710^9+7。

考场代码

C++
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 1e5 + 5;
int n, mod = 1e9 + 7, ans, x[N], y[N];
vector <int> f[N], g[N];
signed main(){
    cin >> n;
    for(int i = 1; i <= n; i++){
        cin >> x[i] >> y[i];
        x[i] += 10000, y[i] += 10000;
        f[x[i]].push_back(i);
        g[y[i]].push_back(i);
    }
    for(int i = 1; i <= n; i++){
        int sumx = 0, sumy = 0;
        for(auto j: g[y[i]])
            sumx += abs(x[i] - x[j]);
        for(auto j: f[x[i]])
            sumy += abs(y[i] - y[j]);
        ans = (ans + sumx * sumy) % mod;
    }
    cout << ans;
    return 0;
}

错因

如果 vectorvector 里的数存的过多(稠密图),那么时间复杂度就会被降到 O(n2)O(n^2),就会炸。

标准思路

补题代码

收获

题目回顾