社区讨论
20分求调
P5318【深基18.例3】查找文献参与者 2已保存回复 1
讨论操作
快速查看讨论及其快照的属性,并进行相关操作。
- 当前回复
- 1 条
- 当前快照
- 1 份
- 快照标识符
- @lzt595ez
- 此快照首次捕获于
- 2024/08/14 08:59 2 年前
- 此快照最后确认于
- 2024/08/14 11:00 2 年前
代码如下
CPP#include<bits/stdc++.h>
using namespace std;
int n,m;
int p_s,p_e;
int head[110001];
int tot=0;
int allow[110001];
bool flag[110001];
int tl=0;
int hd=0;
int s[110001];
int e[110001];
struct input_change{
int s;
int e;
}i_c[110001];
struct edge{
int ver;
int next;
}E[110001];
void dfs(int p_n){
flag[p_n]=1;
cout<<p_n<<" ";
for(int i=head[p_n];i;i=E[i].next){
if(!flag[E[i].ver]){
dfs(E[i].ver);
}
}
}
void bfs(){
memset(flag,0,sizeof(flag));
hd++;
allow[hd]=1;
flag[0]=1;
cout<<"1 ";
while(hd!=tl-1){
tl++;
for(int i=head[allow[tl]];i;i=E[i].next){
if(flag[E[i].ver]==0){
flag[E[i].ver]=1;
hd++;
allow[hd]=E[i].ver;
cout<<E[i].ver<<" ";
}
}
}
}
bool cmp(input_change x,input_change y){
return x.e>y.e;
}
int main(){
cin>>n>>m;
for(int i=0;i<m;i++){
cin>>i_c[i].s>>i_c[i].e;
}
sort(i_c,i_c+m,cmp);
for(int i=1;i<=m;i++){
p_s=i_c[i].s;
p_e=i_c[i].e;
tot++;
E[tot].ver=p_e;
E[tot].next=head[p_s];
head[p_s]=tot;
}
dfs(1);
cout<<endl;
bfs();
}
回复
共 1 条回复,欢迎继续交流。
正在加载回复...