MLE on #42
查看原帖
MLE on #42
792595
wbw_121124楼主2025/1/5 20:51

Memory limit exceeded on test 42

谁能帮我解决一下?

#include<bits/stdc++.h>
typedef int int32;
#define int long long
using namespace std;
const int N = 1e5 + 5;
int n, m, dfn[N], low[N], num, root, cnt;
bool cut[N];
vector<pair<int, int>>nbr[N];
map<int, bool>dcc[N];
stack<int>q;
set<int>s;
void tarjan(int x)
{
	dfn[x] = low[x] = ++num;
	q.push(x);
	if (x == root && !nbr[x].size())
	{
		dcc[++cnt][x] = true;
		return;
	}
	int tot = 0;
	for (auto& [nxt, w] : nbr[x])
		if (!dfn[nxt])
		{
			tarjan(nxt);
			low[x] = min(low[x], low[nxt]);
			if (low[nxt] >= dfn[x])
			{
				tot++;
				if (x != root || tot >= 2)
					cut[x] = true;
				int tmp = 0;
				cnt++;
				while (!q.empty() && tmp != nxt)
				{
					tmp = q.top();
					q.pop();
					dcc[cnt][tmp] = true;
				}
				dcc[cnt][x] = true;
			}
		}
		else
			low[x] = min(low[x], dfn[nxt]);
	return;
}
signed main()
{
	ios::sync_with_stdio(0);
	cin.tie(0), cout.tie(0);
	cin >> n >> m;
	for (int i = 1; i <= m; i++)
	{
		int x, y, z = i;
		cin >> x >> y;
		nbr[x].push_back({ y,z });
		nbr[y].push_back({ x,z });
	}
	for (int i = 1; i <= n; i++)
		if (!dfn[i])
			root = i, tarjan(i);
	for (int i = 1; i <= cnt; i++)
	{
		int tot = 0, tmp = 0;
		for (auto& [x, y] : dcc[i])
			if (y)
			{
				for (auto& [nxt, w] : nbr[x])
					if (dcc[i][nxt])
						tot++;
				tmp++;
			}
		tot /= 2;
		if (tot == tmp)
			for (auto& [x, y] : dcc[i])
				if (y)
					for (auto& [nxt, w] : nbr[x])
						if (dcc[i][nxt])
						s.insert(w);
	}
	cout << s.size() << '\n';
	for (auto& x : s)
		cout << x << ' ';
	return 0;
}
2025/1/5 20:51
加载中...