跳到正文

个人记录

717图总结

Iron_sheets约 6 分钟
原文
文章信息与版本

文章信息

发布
2025年7月17日
收录
2025年12月12日
更新
2026年11月22日
篇幅
约 6 分钟

版本

  1. v1当前

B3643 图的存储

输入点后直接存到邻接矩阵中,因为是无向图,还要反向存一个。再用vector二位数组存下邻接表的方式,在排序输出即可。

C++
#include <bits/stdc++.h>
using namespace std;
vector <int> op[1000 + 9];
int a[1000 + 9][1000 + 9];
int main()
{
    int n, m;
    cin >> n >> m;
    for (int i = 1; i <= m; i ++)
    {
        int x, y;
        cin >> x >> y;
        a[x][y] = 1;//邻接矩阵存下
        a[y][x] = 1;//邻接矩阵反存下
        if (x != y || find(op[x].begin(), op[x].end(), y) == op[x].end())//找到了新的一条边
        {
            op[x].push_back(y);//x连了y
            op[y].push_back(x);//y连了x
        }
    }
    for (int i = 1; i <= n; i ++)
    {
        for (int j = 1; j <= n; j ++)
        {
            cout << a[i][j] << " ";//输出邻接矩阵
        }
        cout << "\n";
    }
    for (int i = 1; i <= n; i ++)
    {
        cout << op[i].size() << " ";//输出连边的条数
        sort(op[i].begin(), op[i].end());//排序
        for (int j = 0; j < op[i].size(); j ++)
        {
            cout << op[i][j] << " ";
        }
        cout << "\n";
    }
    return 0;
}

P5318 【深基18.例3】查找文献

深搜:每次到一个点时输出该点,再找当前文献的还没看的引用文献看

广搜:可以走就走,分多条路线,输出。再重复操作即可。

C++
#include <bits/stdc++.h>
using namespace std;
int n, m;
vector <int> op[100000 + 9];
bool vis[100000 + 9];
void dfs(int cur)
{
    cout << cur << " ";//输出当前节点
    for (int i = 0; i < op[cur].size(); i ++)//当前文献的引用文献
    {
        if (vis[op[cur][i]] == 0)//没看过
        {
            vis[op[cur][i]] = 1;//看过了
            dfs(op[cur][i]);//搜下去
        }
    }
}
void bfs()
{
    queue <int> q;
    memset(vis, 0, sizeof vis);
    vis[1] = 1;//标记起点
    q.push(1);//压入起点
    while (q.size())
    {
        int c = q.front();//取出队首
        q.pop();//弹出队首
        cout << c << ' ';
        for (int i = 0; i < op[c].size(); i ++)//当前文献的引用文献
        {
            int v = op[c][i];
            if (vis[v] == 1)//已经看过了
            {
                continue;
            }
            vis[v] = 1;//标记为看过了
            q.push(v);//压入新起点
        }
    }
}
int main()
{
    cin >> n >> m;
    for (int i = 1; i <= m; i ++)
    {
        int x, y;
        cin >> x >> y;
        if (x != y || find(op[x].begin(), op[x].end(), y) == op[x].end())
        {
            op[x].push_back(y);
        }
    }
    for (int i = 1; i <= n; i ++)
    {
    	sort(op[i].begin(), op[i].end());//排序
	}
    vis[1] = 1;//深搜标记起点
    dfs(1);//深搜
    cout << "\n";
    bfs();//广搜
    return 0;
}

P5908 猫猫和企鹅

用深搜搜索所有点,如发现距离 dd 以内就让 cntcnt +1,最后输出 cntcnt 即可。

C++
#include <bits/stdc++.h>
using namespace std;
int n, d, cnt = 0;
vector <int> op[100000 + 9];
bool vis[100000 + 9];
void dfs(int cur, int step)
{
    if (step != 0 && step <= d)//距离小于d,并且不是1号节点
    {
    	cnt ++;//答案+1
	}
    for (int i = 0; i < op[cur].size(); i ++)//搜所有与当前点相邻的点
    {
    	int v = op[cur][i];
        if (vis[v] == 1)//当前点已经走过了
        {
            continue;
        }
        vis[v] = 1;//标记为走过
        dfs(v, step + 1);//继续搜
    }
}
int main()
{
    cin >> n >> d;
    for (int i = 1; i < n; i ++)
    {
    	int x, y;
    	cin >> x >> y;
        op[x].push_back(y);
        op[y].push_back(x);
    }
    vis[1] = 1;
    dfs(1, 0);//深搜
    cout << cnt;
    return 0;
}
原文