14342022-09-30 16:02:58mraronÜzletlánccpp17Accepted 40/4039ms11348 KiB
#include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// #include <ext/pb_ds/priority_queue.hpp>
//#pragma GCC optimize("O3")
//#pragma GCC target("sse4")
//#pragma GCC target("avx2")
 
#define deb(x) cout << #x << " = " << x << "\n"
#define all(x) (x).begin(), (x).end()
#define len(x) (int) x.size()
#define lsb(x) (x) & -(x)
#define l(x) (x << 1) + 1
#define r(x) (x << 1) + 2
#define v(x) (int)(x - 'a')
 
#define xx first
#define yy second
#define mp make_pair
#define pb push_back
#define lb lower_bound
#define ub upper_bound
 
using namespace std;
// using namespace __gnu_pbds;
typedef long long ll;
typedef long double ld;
typedef pair <int, int> pii;
typedef pair <ld, ld> pld;
typedef pair <ll, ll> pll;
 
// template <class T1, class T2 = less <T1>> using pb_heap = __gnu_pbds::priority_queue <T1, T2, binary_heap_tag>;
// template <class T1, class T2 = null_type, class T3 = less <T1>> using pb_map = tree <T1, T2, T3, rb_tree_tag, tree_order_statistics_node_update>;
// template <class T1, class T2 = null_type, class T3 = hash <T1>> using pb_umap = gp_hash_table <T1, T2, T3>;
template <class T1, class T2, class T3 = hash <T1>> using umap = unordered_map <T1, T2, T3>;
template <class T> using uset = unordered_set <T>;
template <class T> using vec = vector <T>;
 
const ll infll = numeric_limits <ll>::max() >> 1;
const int inf = numeric_limits <int>::max() >> 1;
const int N = 40001;
int n, m, a, b;

int dsta[N];
int dstb[N];
vec <int> adj[N];

string answer;

void input() 
{
     cin >> n >> m >> a >> b;
     for (int i = 0; i < m; ++i) {
          int u; cin >> u;
          int v; cin >> v;
          adj[u].pb(v);
          adj[v].pb(u);
     }
}

void bfs(int source, int dst[N])
{
     fill(dst, dst + N, -1);
     queue <int> q;
     q.push(source);
     dst[source] = 0;
     while (!q.empty()) {
          int u = q.front(); q.pop();
          for (int v: adj[u]) {
               if (dst[v] == -1) {
                    dst[v] = dst[u] + 1;
                    q.push(v);
               }
          }
     }
}

void solve()
{
     bfs(a, dsta);
     bfs(b, dstb);
     int ans = 0;
     for (int i = 1; i <= n; ++i) {
          ans += dsta[i];
     }
     answer = string(n, 'A');
     priority_queue <pii> pq;
     for (int i = 1; i <= n; ++i) {
          pq.push({dsta[i] - dstb[i], i - 1});
     }
     for (int i = 0; i < n / 2; ++i) {
          pii p = pq.top(); pq.pop();
          answer[p.yy] = 'B';
          ans -= p.xx;
     }
     cout << ans << "\n";
     cout << answer << "\n";
}

int main() 
{
     ios_base::sync_with_stdio(false);
     cin.tie(0), cout.tie(0);
     input();
     solve();
}
SubtaskSumTestVerdictTimeMemory
base40/40
1Accepted0/04ms4364 KiB
2Accepted0/03ms4612 KiB
3Accepted2/23ms4752 KiB
4Accepted2/23ms5160 KiB
5Accepted3/33ms5340 KiB
6Accepted3/33ms5320 KiB
7Accepted2/23ms5448 KiB
8Accepted2/23ms5528 KiB
9Accepted3/33ms5660 KiB
10Accepted3/33ms5732 KiB
11Accepted2/26ms6396 KiB
12Accepted2/27ms6480 KiB
13Accepted3/323ms8600 KiB
14Accepted3/34ms6660 KiB
15Accepted2/232ms10448 KiB
16Accepted2/235ms10768 KiB
17Accepted3/323ms10076 KiB
18Accepted3/339ms11348 KiB