66922023-12-16 19:25:57111Üzletlánccpp17Accepted 40/4052ms17020 KiB
#include <bits/stdc++.h>
using namespace std;

#define int long long
#define double long double

#define pii pair<int, int>

signed main() {
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
#ifdef CB
	freopen("be2.txt", "r", stdin);
//	freopen("out.txt", "w", stdout);
#endif
	int N, M, A, B;
	cin >> N >> M >> A >> B;
	vector<vector<int>> g(N + 1);
	for (int i = 0; i < M; i++) {
		int a, b;
		cin >> a >> b;
		g[a].push_back(b);
		g[b].push_back(a);
	}
	vector<int> a(N + 1, -1);
	vector<int> b(N + 1, -1);
	deque<int> q;
	a[A] = 0;
	q.push_back(A);
	while (!q.empty()) {
		int i = q.front();
		q.pop_front();
		for (int j : g[i]) {
			if (a[j] == -1) {
				a[j] = a[i] + 1;
				q.push_back(j);
			}
		}
	}
	b[B] = 0;
	q.push_back(B);
	while (!q.empty()) {
		int i = q.front();
		q.pop_front();
		for (int j : g[i]) {
			if (b[j] == -1) {
				b[j] = b[i] + 1;
				q.push_back(j);
			}
		}
	}
	int ans = 0;
	multimap<int, int> ma, mb;
	for (int i = 1; i <= N; i++) {
		if (a[i] >= b[i]) {
			ans += b[i];
			ma.insert({a[i] - b[i], i});
		}
		else {
			ans += a[i];
			mb.insert({b[i] - a[i], i});
		}
	}
	while (ma.size() > mb.size()) {
		auto p = *ma.begin();
		ma.erase(ma.begin());
		mb.insert(p);
		ans += p.first;
		a[p.second]++;
		swap(a[p.second], b[p.second]);
	}
	while (mb.size() > ma.size()) {
		auto p = *mb.begin();
		mb.erase(mb.begin());
		ma.insert(p);
		ans += p.first;
		b[p.second]++;
		swap(a[p.second], b[p.second]);
	}
	cout << ans << '\n';
	for (int i = 1; i <= N; i++) {
		if (a[i] >= b[i]) {
			cout << 'B';
		}
		else {
			cout << 'A';
		}
	}
	cout << '\n';
	return 0;
}
SubtaskSumTestVerdictTimeMemory
base40/40
1Accepted0/03ms1828 KiB
2Accepted0/03ms2032 KiB
3Accepted2/23ms2236 KiB
4Accepted2/23ms2592 KiB
5Accepted3/33ms2716 KiB
6Accepted3/33ms2944 KiB
7Accepted2/23ms3148 KiB
8Accepted2/23ms3520 KiB
9Accepted3/33ms3720 KiB
10Accepted3/33ms3800 KiB
11Accepted2/27ms5396 KiB
12Accepted2/28ms5404 KiB
13Accepted3/327ms8760 KiB
14Accepted3/36ms5412 KiB
15Accepted2/245ms16128 KiB
16Accepted2/248ms16628 KiB
17Accepted3/335ms15168 KiB
18Accepted3/352ms17020 KiB