QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#446531#4809. Maximum RangeIBoryRE 1ms5880kbC++202.0kb2024-06-17 12:31:212024-06-17 12:31:21

Judging History

你现在查看的是最新测评结果

  • [2024-06-17 12:31:21]
  • 评测
  • 测评结果:RE
  • 用时:1ms
  • 内存:5880kb
  • [2024-06-17 12:31:21]
  • 提交

answer

#include <bits/stdc++.h>
#define pii pair<int, int>
using namespace std;

const int MAX = 100007;
vector<pii> G[MAX], BCC[MAX];
int V[MAX], E[MAX], B[MAX], pv[MAX], dn, bn;
bool no[MAX];
vector<int> S;
pii P[MAX];

int DFS(int cur, int prev) {
	int ret = V[cur] = ++dn;
	for (auto [next, en] : G[cur]) {
		if (next == prev) continue;
		if (V[cur] > V[next]) S.push_back(en);
		if (V[next]) {
			ret = min(ret, V[next]);
			continue;
		}

		int t = DFS(next, cur);
		ret = min(ret, t);
		if (t < V[cur]) continue;

		bn++;
		do {
			int e = S.back(); S.pop_back();
			B[e] = bn;
			BCC[bn].emplace_back(E[e], e);
		} while (BCC[bn].back().second != en);
		sort(BCC[bn].begin(), BCC[bn].end());
	}
	return ret;
}

vector<int> BFS(int S, int EA, int EB) {
	memset(V, 0, sizeof(V));
	queue<int> Q;
	Q.push(S); V[S] = 1;
	while (!Q.empty()) {
		int cur = Q.front(); Q.pop();
		if (cur == EA || cur == EB) break;
		for (auto [next, _] : G[cur]) {
			if (V[next] || no[next]) continue;
			V[next] = 1;
			pv[next] = cur;
			Q.push(next);
		}
	}
	int cnt = 0;
	
	vector<int> ret;
	int c = (V[EA] ? EA : EB);
	while (c != S) {
		ret.push_back(c);
		c = pv[c];

		cnt++;
		assert(cnt < 1000000);
	}
	ret.push_back(S);
	reverse(ret.begin(), ret.end());
	return ret;
}

int main() {
	ios::sync_with_stdio(0); cin.tie(0);
	int N, M;
	cin >> N >> M;
	for (int i = 1; i <= M; ++i) {
		int a, b;
		cin >> a >> b >> E[i];
		G[a].emplace_back(b, i);
		G[b].emplace_back(a, i);
		P[i] = {a, b};
	}

	DFS(1, 0);

	int go = -1, id = 0;
	for (int i = 1; i <= bn; ++i) {
		int n = BCC[i].back().first - BCC[i][0].first;
		if (go < n) {
			go = n;
			id = i;
		}
	}

	auto [a, b] = P[BCC[id][0].second];
	auto [c, d] = P[BCC[id].back().second];

	no[b] = 1;
	vector<int> P1 = BFS(a, c, d);
	no[b] = 0;
	for (int n : P1) no[n] = 1;
	vector<int> P2 = BFS(no[c] ? d : c, b, 0);
	for (int n : P2) P1.push_back(n);

	cout << go << '\n';
	cout << P1.size() << '\n';
	for (int n : P1) cout << n << ' ';
	return 0;
}

详细

Test #1:

score: 100
Accepted
time: 1ms
memory: 5880kb

input:

5 7
1 2 1
1 3 -2
2 3 1
3 4 3
4 5 1
1 5 -1
2 5 2

output:

5
4
1 5 4 3 

result:

ok ok

Test #2:

score: -100
Runtime Error

input:

99997 100000
12238 99016 352755196
99016 25485 -412473602
25485 2440 991507552
2440 31171 -181894654
36970 2440 -800167579
2440 41865 -148191946
96629 31171 847888506
36970 95740 395546542
27992 2440 647886610
99016 29557 369124914
80795 27992 -673871966
36970 3509 573208857
57672 29557 874406776
41...

output:


result: