882021-01-10 19:32:13varikakasandorÜzletlánccpp11Accepted 40/4079ms13088 KiB
#include <bits/stdc++.h>
using namespace std;

int n,m;
int a,b;
int da[40001];
int db[40001];
bool wasa[40001];
bool wasb[40001];
vector<int> g[40001];
char sol[40001];

void bfs(int start, int d[], bool was[])
{
    queue<pair<int, int> > bfs_queue;
    bfs_queue.push({start,0});
    while(!bfs_queue.empty())
    {
        pair<int, int> act=bfs_queue.front();
        bfs_queue.pop();
        if(was[act.first]) continue;
        d[act.first]=act.second;
        was[act.first]=true;
        for(auto s:g[act.first]) bfs_queue.push({s,act.second+1});
    }
}

bool comp(int x, int y)
{
    return ((da[x]-da[y])<(db[x]-db[y]));
}

int main () {
    cin>>n>>m>>a>>b;
    for(int i=1;i<=m;i++)
    {
        int x,y;
        cin>>x>>y;
        g[x].push_back(y);
        g[y].push_back(x);
    }
    bfs(a,da,wasa);
    bfs(b,db,wasb);
    vector<int> v;
    for(int i=1;i<=n;i++) v.push_back(i);
    sort(v.begin(),v.end(),comp);
    string res="";
    int cost=0;
    for(int i=0;2*i<n;i++)
    {
         sol[v[i]]='A';
         cost+=da[v[i]];
    }
    for(int i=n/2;i<n;i++)
    {
        sol[v[i]]='B';
        cost+=db[v[i]];
    }
    for(int i=1;i<=n;i++) res+=sol[i];
    cout<<cost<<endl;
    cout<<res<<flush;
    return 0;
}
SubtaskSumTestVerdictTimeMemory
base40/40
1Accepted0/02ms3588 KiB
2Accepted0/02ms3756 KiB
3Accepted2/22ms3684 KiB
4Accepted2/22ms3684 KiB
5Accepted3/32ms3736 KiB
6Accepted3/32ms3692 KiB
7Accepted2/22ms3704 KiB
8Accepted2/22ms3708 KiB
9Accepted3/32ms3716 KiB
10Accepted3/32ms3712 KiB
11Accepted2/27ms4400 KiB
12Accepted2/29ms4636 KiB
13Accepted3/359ms9256 KiB
14Accepted3/36ms5148 KiB
15Accepted2/257ms10056 KiB
16Accepted2/268ms11596 KiB
17Accepted3/341ms10688 KiB
18Accepted3/379ms13088 KiB