社区讨论
help无法下载数据且看不出错误的蒟蒻
P5318【深基18.例3】查找文献参与者 1已保存回复 0
讨论操作
快速查看讨论及其快照的属性,并进行相关操作。
- 当前回复
- 0 条
- 当前快照
- 1 份
- 快照标识符
- @mli1247z
- 此快照首次捕获于
- 2026/02/11 20:50 上周
- 此快照最后确认于
- 2026/02/11 21:35 上周
样例可以过,但测试点全WA,求助路过大佬
CPP#include <bits/stdc++.h>
using namespace std;
void dfs(multimap<int,int> &G, vector<bool> &vis, int x, int n)//1->2->5->6->3->7->8->4
{
cout<<x<<" "; vis[x]=true;
multimap<int,int>::iterator it=G.find(x);//1-》2
for(; it!=G.end() && it->first==x; it++)
{
int y=it->second;
if(vis[y]==false)
dfs(G, vis, y, n);
}
return;
}
void bfs(multimap<int,int> &G, vector<bool> &vis)
{
queue<int> Q;
Q.push(1); vis[1]=true;
while(!Q.empty())
{
int x=Q.front(); Q.pop(); cout<<x<<" ";
multimap<int,int>::iterator it=G.find(x);
for(; it!=G.end() && it->first==x; it++)
{
int y=it->second;
if(vis[y]==false)
Q.push(y),vis[y]=true;
}
}
cout<<endl;
}
int main()
{
//multimap有向边
multimap<int,int> G;
int vn, en; cin>>vn>>en;
for(int i=0; i<en; i++)
{
int sv, ev; cin>>sv>>ev;
G.insert(pair<int, int>(sv, ev));
}
//multimap+vis动态数组-》dfs输出
vector<bool> vis(vn+1, false);//1 2 3 ……vn
dfs(G, vis, 1, vn);
cout<<endl;
//multimap+queue-》bfs输出
for(int i=1; i<=vn; i++)
vis[i]=false;
bfs(G, vis);
return 0;
}
//multimap
//insert find erase
//priority_queue
//push front pop
回复
共 0 条回复,欢迎继续交流。
正在加载回复...