204912026-01-07 12:05:48ubormaciÜzletlánccpp17Elfogadva 40/4041ms3892 KiB
#include <iostream>
#include <algorithm> // for sort, mainly
#include <vector>
#include <map>
#include <set>
#include <cmath>
#include <array>
#include <string>
#include <cstdio>
#include <iterator>
#include <unordered_set>
#include <cstdint> // for int64_t, int32_t, etc
#include <queue>
#include <stack>
#include <deque>
#include <numeric> // gcd, lcm
#include <fstream>
#include <bitset> // for bitset
#include <iomanip>
#include <cassert> // for set with custom ordering
#include <type_traits> // for set with custom ordering
#include <utility> // for swap, forward, etc
using namespace std;

#pragma GCC optimize("O2")
// #pragma GCC optimize("O1","O2","O3","Ofast","unroll-loops")
// #pragma GCC target("sse","sse2","sse3","sse4.1","sse4.2","avx","avx2","fma")

template<typename A, typename B> ostream& operator<<(ostream &os, const pair<A, B> &p) { return os << '(' << p.first << ", " << p.second << ')'; }
template<typename T_container, typename T = typename enable_if<!is_same<T_container, string>::value, typename T_container::value_type>::type> ostream& operator<<(ostream &os, const T_container &v) { os << '{'; string sep; for (const T &x : v) os << sep << x, sep = ", "; return os << '}'; }
void dbg_out() { cout << endl; }
template<typename Head, typename... Tail> void dbg_out(Head H, Tail... T) { cout << ' ' << H; dbg_out(T...); }
#ifdef LOCAL
#define dbg(...) cout << "(" << #__VA_ARGS__ << "):", dbg_out(__VA_ARGS__)
#else
#define dbg(...)
#endif

/*

notes:

int64_t
stoi(string s) -> string to int
to_string() -> int (or else) to string

vector declaration:
vector<ll> v(n, 0)
vector<vector<ll>> v(n, vector<ll>(n, 0));

{if statement} ? {truth value} : {false value}

#ifdef LOCAL
    freopen("in.txt","r",stdin);
    freopen("out.txt","w",stdout);
#endif

std::lcm(ll a, ll b), std::gcd(int a, int b)

cout << fixed << setprecision(n);

set with custom ordering
set<ll, decltype(&cmp)> qu(cmp);

*/

typedef int32_t ll;

const ll inf = INT32_MAX;

void getd(ll start, vector<vector<ll>> &v, vector<ll> &d) {

	d[start] = 0;
	queue<ll> q;
	q.push(start);
	while(!q.empty()) {
		ll curr = q.front();
		q.pop();

		for(const auto & x : v[curr]) {
			if(d[x] == inf) {
				d[x] = d[curr] + 1;
				q.push(x);
			}
		}
	}

}

void solve() {

	ll n, m, a, b;
	cin >> n >> m >> a >> b;

	vector<vector<ll>> v(n+1);
	for(ll i = 0; i < m; i++) {
		ll c, d;
		cin >> c >> d;
		v[c].push_back(d);
		v[d].push_back(c);
	}

	// eloszor mindegyiket berakjuk arra, amelyik kozelebb van hozza
	// aztan a vegen valtoztatjuk hogy kijojjon az egyensuly

	vector<ll> da(n+1, inf);
	vector<ll> db(n+1, inf);
	getd(a, v, da);
	getd(b, v, db);

	//cerr << "\nda=" << da << "; db=" << db;

	ll toa = 0, tob = 0;

	vector<bool> res(n+1, false); // false=A, true=B

	ll ans = 0;

	vector<pair<ll,ll>> sw(n, {0,0});
	for(ll i = 0; i < n; i++) {

		ll curr = i + 1;

		ll bt = min(da[curr], db[curr]);
		ll ws = max(da[curr], db[curr]);

		if(bt == da[curr]) {
			ans += da[curr];
			toa++;
			res[curr] = false;
		}else if(bt == db[curr]){
			ans += db[curr];
			tob++;
			res[curr] = true;
		}

		//cerr << "\ncurr=" << curr << "; bt=" << bt << "; ws=" << ws;
		//cerr << "\ntoa=" << toa << "; tob=" << tob;

		sw[i] = {ws - bt, curr};
	}

	sort(sw.begin(), sw.end());
	for(const auto & [extra, which] : sw) {

		if(toa < tob) {

			// ha a mostani b-re ment
			// rakjuk at a-ra

			if(res[which] == true) {
				tob--;
				toa++;
				res[which] = false;
				ans += extra;
			}

		}else if(toa > tob) {

			// ha a mostani a-ra ment
			// rakjuk at b-re

			if(res[which] == false) {
				tob++;
				toa--;
				res[which] = true;
				ans += extra;
			}
		}
	}

	string st(n, 'A');

	cout << ans << "\n";
	for(ll i = 1; i <= n; i++) {
		if(res[i]) {
			st[i-1] = 'B';
		}
	}
	cout << st;

}

int main()
{
	std::ios_base::sync_with_stdio(false);
	cin.tie(nullptr);
	cout.tie(nullptr);

	solve();

	return 0;
}
RészfeladatÖsszpontTesztVerdiktIdőMemória
base40/40
1Elfogadva0/01ms512 KiB
2Elfogadva0/01ms508 KiB
3Elfogadva2/21ms316 KiB
4Elfogadva2/21ms316 KiB
5Elfogadva3/31ms316 KiB
6Elfogadva3/31ms316 KiB
7Elfogadva2/21ms316 KiB
8Elfogadva2/21ms316 KiB
9Elfogadva3/31ms316 KiB
10Elfogadva3/31ms316 KiB
11Elfogadva2/24ms568 KiB
12Elfogadva2/24ms820 KiB
13Elfogadva3/323ms1744 KiB
14Elfogadva3/33ms564 KiB
15Elfogadva2/232ms3476 KiB
16Elfogadva2/239ms3636 KiB
17Elfogadva3/324ms3272 KiB
18Elfogadva3/341ms3892 KiB