天梯赛L2题解(021-024)

L2-021 点赞狂魔

#include <bits/stdc++.h>
using namespace std;
#define int long long

void ylh_() {
    int n;
    cin >> n;
    vector<string> name(n + 1);
    vector<int> num(n + 1), idx(n + 1), k(n + 1);
    for (int i = 1; i <= n; ++i) {
        idx[i] = i;
        cin >> name[i] >> k[i];
        set<int> st;
        for (int j = 1; j <= k[i]; ++j) {
            int x;
            cin >> x;
            st.insert(x);
        }
        num[i] = st.size();
    }
    sort(idx.begin() + 1, idx.end(), [&](int x, int y) {
        if (num[x] == num[y])
            return num[x] * k[y] > num[y] * k[x];
        return num[x] > num[y];
    });
    if (n == 1) {
        cout << name[idx[1]] << " - -";
    } else if (n == 2) {
        cout << name[idx[1]] << ' ' << name[idx[2]] << " -";
    } else {
        cout << name[idx[1]] << ' ' << name[idx[2]] << ' ' << name[idx[3]];
    }
}

int32_t main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    int T = 1;
    // cin >> T;
    while (T--) {
        ylh_();
    }
}

L2-022 重排链表

#include <bits/stdc++.h>
using namespace std;
#define int long long
const int N = 1e5 + 5;

void ylh_() {
    int st, n;
    cin >> st >> n;
    vector<int> data(N, 0), nxt(N, -1);
    
    for (int i = 1; i <= n; ++i) {
        int addr, val, next;
        cin >> addr >> val >> next;
        data[addr] = val;
        nxt[addr] = next;
    }
    
    vector<int> list;
    int cur = st;
    while (cur != -1) {
        list.push_back(cur);
        cur = nxt[cur];
    }
    int l = 0, r = list.size() - 1;
    vector<int> res;
    while (l <= r) {
        if (l == r) {
            res.push_back(list[l]);
            break;
        }
        res.push_back(list[r]);
        res.push_back(list[l]);
        l++;
        r--;
    }
    for (int i = 0; i < res.size(); ++i) {
        cout << setw(5) << setfill('0') << res[i] << " " << data[res[i]] << " ";
        if (i == res.size() - 1) cout << "-1\n";
        else cout << setw(5) << setfill('0') << res[i + 1] << "\n";
    }
}

int32_t main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    int T = 1;
    // cin >> T;
    while (T--) {
        ylh_();
    }
    return 0;
}

L2-023 图着色问题

数据规模小随便弄下就能过,但是要注意颜色数是等于K而不是小于等于

#include <bits/stdc++.h>
using namespace std;
#define int long long
using PII = pair<int, int>;

void ylh_() {
    int n, m, k;
    cin >> n >> m >> k;
    vector<vector<int>> g(n + 1);
    for (int i = 1; i <= m; ++i) {
        int x, y;
        cin >> x >> y;
        g[x].push_back(y);
        g[y].push_back(x);
    }
    int q;
    cin >> q;
    vector<int> a(n + 1);
    set<int> st;
    while (q--) {
        for (int i = 1; i <= n; ++i) {
            cin >> a[i];
            st.insert(a[i]);
        }
        bool f = 1;
        for (int i = 1; i <= n; ++i) {
            for (auto v : g[i]) {
                if (a[i] == a[v]) {
                    f = 0;
                }
            }
        }
        if (f && st.size() == k) {
            cout << "Yes\n";
        } else {
            cout << "No\n";
        }
        st.clear();
    }
}

int32_t main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    int T = 1;
    // cin >> T;
    while (T--) {
        ylh_();
    }
}

L2-024 部落

刷到这里天梯赛L2好多并查集板子。。。还是要会这个

#include <bits/stdc++.h>
using namespace std;
#define int long long

void ylh_() {
    int n;
    cin >> n;
    vector<int> a(n + 1);
    const int N = 1e4;
    vector<int> p(N + 1);
    iota(p.begin(), p.end(), 0);
    auto findx = [&](auto&& findx, int x) -> int {
        return x == p[x] ? x : p[x] = findx(findx, p[x]);
    };
    auto find = [&](int x) -> int {
        return findx(findx, x);
    };
    int cnt = 0;
    auto merge = [&](int u, int v) -> void {
        u = find(u), v = find(v);
        if (u == v)
            return;
        ++cnt;
        p[u] = v;
    };
    set<int> st;
    for (int i = 1; i <= n; ++i) {
        int k, p1;
        cin >> k >> p1;
        int pi;
        st.insert(p1);
        for (int i = 2; i <= k; ++i) {
            cin >> pi;
            merge(pi, p1);
            st.insert(pi);
        }
    }

    int q;
    cin >> q;
    cout << st.size() << ' ' << st.size() - cnt << '\n';
    while (q--) {
        int x, y;
        cin >> x >> y;
        cout << ((find(x) == find(y)) ? 'Y' : 'N') << '\n';
    }
}

int32_t main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    int T = 1;
    // cin >> T;
    while (T--) {
        ylh_();
    }
}
posted @ 2026-03-24 21:31  特瑞斯特  阅读(4)  评论(0)    收藏  举报