您的位置:首页 > 新闻 > 会展 > 电子商务网站建设的步骤一般为_企业门户网站运营推广_网站建设找哪家好_火星时代教育培训机构学费多少

电子商务网站建设的步骤一般为_企业门户网站运营推广_网站建设找哪家好_火星时代教育培训机构学费多少

2025/4/10 22:55:35 来源:https://blog.csdn.net/2301_81772249/article/details/146485925  浏览:    关键词:电子商务网站建设的步骤一般为_企业门户网站运营推广_网站建设找哪家好_火星时代教育培训机构学费多少
电子商务网站建设的步骤一般为_企业门户网站运营推广_网站建设找哪家好_火星时代教育培训机构学费多少

这道题乍一看好像没什么不对的,但是!但是!结点最大可以到10的5次方!!!我们递归的时间复杂度是很高的,我们正常遍历是肯定通过不了的,不信的话我们试一下

#include <iostream>
#include <vector>
#include <cstring>
using namespace std;
const int N = 1e5+10;
int n,m;
vector<int> edges[N];
bool st[N];
int ret;
void dfs(int u)
{for(auto&e : edges[u]){if(!st[e]){ret = max(ret,e);st[e] = true;dfs(e);}}
}
int main()
{cin >> n >> m;for(int i = 1;i<=m;i++){int x,y;cin >> x >> y;edges[x].push_back(y);}for(int i =1;i<=n;i++){memset(st,false,sizeof(st));st[i] = true;ret = i;dfs(i);cout << ret << " ";}return 0;
}

正着来,我们的时间复杂度很高

如果正着来,我们需要从1开始遍历图一遍,遍历到5再回去找6,从2再来一遍,遍历到1,回去找6,这时候我们的时间复杂度就是N平方,

我们不如把图反过来,反着找,把每次能到达最大点的编号标记上,然后下次再遍历的时候如果某个点已经被标记了就剪掉,时间复杂度就是O(N)

#include <iostream>
#include <vector>
using namespace std;
const int N = 1e5+10;
vector<int> edges[N];
int ret[N];
int n,m;
void dfs(int x,int n)
{ret[x] = n;for(auto &e : edges[x]){if(ret[e]) continue;ret[e] = n;dfs(e,n);}
}
int main()
{cin >> n >> m;for(int i = 1;i<=m;i++){int x,y; cin >> x >> y;edges[y].push_back(x);}//上面表示存反图for(int i = n;i>=1;i--){if(ret[i]) continue;dfs(i,i);} for(int i =1;i<=n;i++){cout << ret[i] << " ";}return 0;
}

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com