66922023-12-16 19:25:57111Üzletlánccpp17Elfogadva 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;
}
RészfeladatÖsszpontTesztVerdiktIdőMemória
base40/40
1Elfogadva0/03ms1828 KiB
2Elfogadva0/03ms2032 KiB
3Elfogadva2/23ms2236 KiB
4Elfogadva2/23ms2592 KiB
5Elfogadva3/33ms2716 KiB
6Elfogadva3/33ms2944 KiB
7Elfogadva2/23ms3148 KiB
8Elfogadva2/23ms3520 KiB
9Elfogadva3/33ms3720 KiB
10Elfogadva3/33ms3800 KiB
11Elfogadva2/27ms5396 KiB
12Elfogadva2/28ms5404 KiB
13Elfogadva3/327ms8760 KiB
14Elfogadva3/36ms5412 KiB
15Elfogadva2/245ms16128 KiB
16Elfogadva2/248ms16628 KiB
17Elfogadva3/335ms15168 KiB
18Elfogadva3/352ms17020 KiB