2026.1.30_401_总结

· · 个人记录

总分

\color{red}0pts$+$\color{green}100pts$+$\color{green}100pts$+$\color{green}100pts$+$\color{red}20pts$=$ \color{yellow} 320pts

T727369 图的存储

\color{red} 0pts

死因

没有排序

思路

把图用邻接表和邻接矩阵存下来,按照题目的要求输出

代码

#include<bits/stdc++.h>
#define Guo using
#define Weiqian namespace
#define Code std
#define ll long long
#define N (int)(1e3 + 10)
#define FIO(IN, OUT); freopen(IN, "r", stdin);freopen(OUT, "w", stdout);
Guo Weiqian Code;
vector<int> g[N];
int n, m, x, y;
bool g1[N][N];
int main(){
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    cin >> n >> m;
    for(int i = 1; i <= m; i++){
        cin >> x >> y;
        g[x].push_back(y);
        g[y].push_back(x);
        g1[x][y] = 1;
        g1[y][x] = 1;
    }
    for(int i = 1; i <= n; i++){
        for(int j = 1; j <= n; j++){
            cout << g1[i][j] << " ";
        }
        cout << "\n";
    }
    for(int i = 1; i <= n; i++){
        cout << g[i].size() << " ";
        stable_sort(g[i].begin(), g[i].end());
        for(auto j : g[i]){
            cout << j << " ";
        }
        cout << "\n";
    }
    return 0;
}

T727370 图的遍历(简单版)

\color{green}100pts

思路

每个点都dfs一边,打擂台维护能到达的点的最大编号

代码

#include<bits/stdc++.h>
#define Guo using
#define Weiqian namespace
#define Code std
#define ll long long
#define N (int)(1e3 + 10)
#define FIO(IN, OUT); freopen(IN, "r", stdin);freopen(OUT, "w", stdout);
Guo Weiqian Code;
vector<int> g[N];
int n, m, x, y, mx, vis[N], id;

void dfs(int u){
    mx = max(mx, u);
    for(auto v : g[u]){
        if(vis[v] != id){
            vis[v] = id;
            dfs(v);
        }
    } 
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    cin >> n >> m;
    for(int i = 1; i <= m; i++){
        cin >> x >> y;
        g[x].push_back(y);
    }
    for(int i = 1; i <= n; i++){
        mx = 0;
        id = i;
        vis[i] = i;
        dfs(i);
        cout << mx << " ";
    }
    return 0;
}

T727371 猫猫和企鹅

\color{green}100pts

思路

dfs时传一个变量dep,用来记录当前走几步了,如果步数小于等于d,答案++

代码

#include<bits/stdc++.h>
#define Guo using
#define Weiqian namespace
#define Code std
#define ll long long
#define N (int)(1e5 + 10)
#define FIO(IN, OUT); freopen(IN, "r", stdin);freopen(OUT, "w", stdout);
Guo Weiqian Code;
vector<int> g[N];
int n, d, x, y, ans;
bool vis[N];

void dfs(int u, int dep){
    ans++;
    for(auto v : g[u]){
        if(!vis[v] && dep + 1 <= d){
            vis[v] = 1; 
            dfs(v, dep + 1);
        }
    } 
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    cin >> n >> d;
    for(int i = 1; i < n; i++){
        cin >> x >> y;
        g[x].push_back(y);
        g[y].push_back(x);
    }
    vis[1] = 1;
    dfs(1, 0);
    cout << ans - 1;
    return 0;
}

T727372 [GESP202503 七级] 图上移动

\color{green}100pts

思路

记录答案的数组二维,ans[i][j]表示第编号为i的点第j部能走到的点的数量dfs时每搜索到一个点就把ans[当前要求答案的点][当前的步数]++

代码

#include<bits/stdc++.h>
#define Guo using
#define Weiqian namespace
#define Code std
#define ll long long
#define N (int)(5e2 + 10)
#define K (int)(2e1 + 10)
#define FIO(IN, OUT); freopen(IN, "r", stdin);freopen(OUT, "w", stdout);
Guo Weiqian Code;
vector<int> g[N];
int n, m, k, x, y, id, cnt[N][K];
int vis[N][K];

void dfs(int u, int dep){
    cnt[u][dep]++;
    for(auto v : g[u]){
        if(vis[v][dep + 1] != id && dep + 1 <= k){
            vis[v][dep + 1] = id;
            dfs(v, dep + 1);
        }
    } 
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    cin >> n >> m >> k;
    for(int i = 1; i <= m; i++){
        cin >> x >> y;
        g[x].push_back(y);
        g[y].push_back(x);
    }
    for(int i = 1; i <= n; i++){
        id = i;
        vis[i][0] = i;
        dfs(i, 0);
    } 
    for(int i = 1; i <= n; i++){
        for(int j = 1; j <= k; j++){
            cout << cnt[i][j] << " ";
        }
        cout << "\n";
    }
    return 0;
}

P10110 [GESP202312 七级] 商品交易

\color{red}20pts

死因

没想到正解,dp写挂(其实dp不了)

思路

稍微观察后就会发现,从a到b的最小花费就是v[b] - v[a] + a到b的最小步数,所以我们用dfs求出a到b的最小距离就能得出答案

代码

#include<bits/stdc++.h>
#define Guo using
#define Weiqian namespace
#define Code std
#define ll long long
#define N (int)(1e5 + 10)
#define FIO(IN, OUT); freopen(IN, "r", stdin);freopen(OUT, "w", stdout);
Guo Weiqian Code;
vector<int> g[N];
int n, m, x, y, a, b, aa[N], d[N];
bool vis[N];

struct Node{
    int y, step;
};

void bfs(){
    queue<Node> q;
    q.push({a, 0});
    while(q.size()){
        Node u = q.front();
        d[u.y] = min(d[u.y], u.step);
        q.pop();
        for(auto v : g[u.y]){
            if(!vis[v]){
                vis[v] = 1;
                q.push({v, u.step + 1});
            }
        }
    }
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    cin >> n >> m >> a >> b;
    for(int i = 0; i < n; i++){
        cin >> aa[i]; 
        d[i] = 1e9;
    }
    for(int i = 1; i <= m; i++){
        cin >> x >> y;
        g[x].push_back(y);
    }
    bfs();
    if(!vis[b]){
        cout << "No solution";
        return 0;
    }
    cout << aa[b] - aa[a] + d[b];
    return 0;
}