社区讨论

求助

P5318【深基18.例3】查找文献参与者 2已保存回复 4

讨论操作

快速查看讨论及其快照的属性,并进行相关操作。

当前回复
4 条
当前快照
1 份
快照标识符
@lo2t6i3g
此快照首次捕获于
2023/10/23 19:22
2 年前
此快照最后确认于
2023/10/23 19:22
2 年前
查看原帖
CPP
#include <iostream>
#include <cstring>
#include <vector>
#include <queue>
using namespace std;
int n,m;
vector<int> p[100005];
queue<int> q;
bool u[100005];
void solve(int x)
{
    cout << x << ' ';
    for (int i = 0,sz = p[x].size();i < sz;i++)
    {
        if (!u[p[x][i]]) {
            u[p[x][i]] = true;
            solve(p[x][i]);
        }
    }
}
void bfs()
{
    memset(u,false,sizeof(u));
    u[1] = true;
    q.push(1);
    while (!q.empty())
    {
        int x= q.front();
        q.pop();
        cout << x << ' ';
        for (int i = 0,sz = p[x].size();i < sz;i++)
            if (!u[p[x][i]])
            {
                u[p[x][i]] = true;
                q.push(p[x][i]);
            }
    }
}
int main()
{
    cin >> n >> m;
    for (int i = 1;i <= m;i++)
    {
        int x,y;
        cin >> x >>y;
        p[x].push_back(y);
    }
    u[1] = true;
    solve(1);
    cout << endl;
    bfs();
    return 0;
}

回复

4 条回复,欢迎继续交流。

正在加载回复...