9082022-01-27 12:38:58kidesoÜzletlánccpp14Accepted 40/4075ms16568 KiB
#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>
#include <fstream>

#define pii pair<int,int>
using namespace std;

ifstream fin("be.txt");

vector<vector<int> > x;
vector<vector<int> > t;
vector<pii> T;
int N, M, A, B;

void szel(int k, int s) {
	queue<int> y;
	vector<bool> lat(N + 1, false);

	lat[k] = true;
	t[k][s] = 0;
	y.push(k);

	while (!y.empty()) {
		int csp = y.front();
		y.pop();

		for(auto e:x[csp])
			if (!lat[e]) {
				lat[e] = true;
				y.push(e);
				t[e][s] = t[csp][s] + 1;
			}
	}
}

bool r(const pii& a, const pii& b) { return a.second > b.second; }

int main() {
	cin >> N >> M >> A >> B;
	
	x.resize(N + 1);
	t.resize(N + 1, vector<int>(2, 0));

	while (M) {
		int a, b;
		cin >> a >> b;

		x[a].push_back(b);
		x[b].push_back(a);

		--M;
	}

	szel(A, 0); szel(B, 1);

	vector <pii> T;

	for (int i = 1; i <= N; ++i)
		if (i != A && i != B) T.push_back({ i,t[i][0] - t[i][1] });

	sort(T.begin(), T.end(), r);

	string res;
	res.resize(N + 1, 'A');
	res[B] = 'B';

	for (int i = 0; i < N / 2 - 1; ++i) res[T[i].first] = 'B';

	int sum = 0;
	for (int i = 1; i <= N; ++i) sum += t[i][res[i] - 'A'];

	cout << sum << '\n' << res.substr(1) << '\n';
	return 0;
}
SubtaskSumTestVerdictTimeMemory
base40/40
1Accepted0/02ms1740 KiB
2Accepted0/01ms1800 KiB
3Accepted2/21ms1848 KiB
4Accepted2/22ms1848 KiB
5Accepted3/31ms1856 KiB
6Accepted3/31ms1860 KiB
7Accepted2/21ms1864 KiB
8Accepted2/21ms1876 KiB
9Accepted3/32ms1884 KiB
10Accepted3/31ms1880 KiB
11Accepted2/27ms3180 KiB
12Accepted2/210ms3432 KiB
13Accepted3/350ms6152 KiB
14Accepted3/38ms4080 KiB
15Accepted2/264ms13888 KiB
16Accepted2/271ms14964 KiB
17Accepted3/337ms14736 KiB
18Accepted3/375ms16568 KiB