0分 求佬调 全爆了
查看原帖
0分 求佬调 全爆了
728445
redwolf楼主2023/7/31 16:28

本来四个MLE一个WA 我修改后每次寻找根节点在bfs和dfs就变成四个wa 一个MLE了

#include <iostream>
#include <cstring>
#include <algorithm>
#include <stack>
#include <unordered_map>
#include <map>
#include <cmath>
#include <queue>
#include <vector>
using namespace std;
typedef long long ll;
const int mod = 998244353;
const int N = 200050,inf = 0x3f3f3f3f;
int a[N];
template <typename T>
inline void read(T &f)
{
	f=1;
	T x=0;
	char ch=getchar();
	while(ch<'0'||ch>'9')
	{
		if(ch=='-')
			f=-1;
		ch=getchar();
	}
	while(ch>='0'&&ch<='9')
	{
		x=x*10+ch-'0';
		ch=getchar();
	}
	f*=x;
}
template <typename T>
inline void print(T x)
{
	if(x<0)
	{
		putchar('-');
		x=-x;
	}
	if(x>9)
		print(x/10);
	putchar(x%10+'0');
}

int n,m;
bool dst[N],bst[N];

vector<int>q;
queue<int>qb;
vector<int>qq;
vector<int>hh[N];
bool has_father[N];

void dfs(int u)
{
		if(!dst[u])
		q.push_back(u);
		dst[u] = true;
		for(int i = 0;i < hh[u].size(); ++ i)
		{
			dfs(hh[u][i]);
		}
	
}

void bfs(int u)
{
	qb.push(u);
	qq.push_back(u);
	while(qb.size())
	{
		int t = qb.front();
		qb.pop();
		for(auto x : hh[t])
		{
			if(!bst[x])
			{
				qq.push_back(x);
				qb.push(x);
				bst[x] = true;
			}
		}
	}
}
void solved()
{
	cin >> n >> m;

	for(int i = 0; i < m; ++ i)
	{
		int a, b;
		cin >> a >> b;
		hh[a].push_back(b);
		has_father[b] = true;
		
	}
	int root = 1;
	while(has_father[root]) root += 1;
	
	dfs(root);
	for(auto x: q)
	{
		cout << x <<" ";
	}
	cout << endl;
	bfs(root);
	for(auto x: qq)
	{
		cout << x <<" ";
	}
}

int main()
{
	cin.tie(0);
	cout.tie(0);
	ios::sync_with_stdio(false);
	//	int t;
	//	cin >> t;
	//	while(t --)
	solved();
	
}


2023/7/31 16:28
加载中...