19382022-12-12 11:55:49TomaSajtÜzletlánccpp17Accepted 40/4041ms10392 KiB
#include <bits/stdc++.h>
#define speed ios::sync_with_stdio(0);cin.tie(0)
using namespace std;

int n;
void calcDist(const vector<vector<int>>& g, vector<int>& dist, int s) {
    queue<int> q;
    q.push(s);
    dist.assign(n + 1, -1);
    dist[s] = 0;
    while (!q.empty()) {
        int curr = q.front(); q.pop();
        for (int nei : g[curr]) {
            if (dist[nei] != -1) continue;
            dist[nei] = dist[curr] + 1;
            q.push(nei);
        }
    }
}
int main() {
    speed;
    int m, a, b;
    cin >> n >> m >> a >> b;
    vector<vector<int>> g(n + 1);
    vector<bool> vis(n + 1);
    while (m--) {
        int u, v; cin >> u >> v;
        g[u].push_back(v);
        g[v].push_back(u);
    }
    vector<int> dista, distb;
    calcDist(g, dista, a);
    calcDist(g, distb, b);
    vector<pair<int, int>> indCombo(n);
    for (int i = 1; i <= n; i++) {
        indCombo[i - 1] = { dista[i] - distb[i],i };
    }
    sort(indCombo.begin(), indCombo.end());
    vector<int> isA(n + 1);
    for (int j = 0; j < n; j++) {
        auto [d, i] = indCombo[j];
        isA[i] = j < n / 2;
    }
    int res = 0;
    for (int i = 1; i <= n; i++) res += isA[i] ? dista[i] : distb[i];
    cout << res << '\n';
    for (int i = 1; i <= n; i++) cout << "BA"[isA[i]];
}
SubtaskSumTestVerdictTimeMemory
base40/40
1Accepted0/03ms1824 KiB
2Accepted0/02ms2068 KiB
3Accepted2/22ms2096 KiB
4Accepted2/22ms2324 KiB
5Accepted3/32ms2556 KiB
6Accepted3/32ms2732 KiB
7Accepted2/22ms2932 KiB
8Accepted2/22ms3048 KiB
9Accepted3/32ms3128 KiB
10Accepted3/32ms3120 KiB
11Accepted2/24ms4028 KiB
12Accepted2/27ms4172 KiB
13Accepted3/323ms6168 KiB
14Accepted3/34ms4200 KiB
15Accepted2/234ms9956 KiB
16Accepted2/237ms10156 KiB
17Accepted3/325ms9432 KiB
18Accepted3/341ms10392 KiB