78762024-01-11 15:40:16mmatedÜzletlánccpp17Accepted 40/4057ms13392 KiB
#include <bits/stdc++.h>
using namespace std;
#define f(i,k,n) for(int i=k; i<n; i++)
#define ll long long
const int inf = INT_MAX;

vector<vector<int>> g;
void solve()
{
    int n,m,a,b;
    cin>>n>>m>>a>>b;
    vector<vector<int>> g(n+1);
    int x,y;
    f(i,0,m)
    {
        cin>>x>>y;
        g[x].push_back(y);
        g[y].push_back(x);
    }
    queue<int> q;
    q.push(a);
    vector<int> d1(n+1,inf);
    vector<bool> v(n+1,false);
    d1[a]=0;
    v[a]=true;
    while(!q.empty())
    {
        int w=q.front();
        q.pop();
        for(auto u : g[w])
        {
            if(v[u]==false)
            {
                v[u]=true;
                d1[u]=d1[w]+1;
                q.push(u);
            }
        }
    }
    q.push(b);
    vector<int> d2(n+1,inf);
    for(auto x:v)   x=false;
    d2[b]=0;
    v[b]=true;
    while(!q.empty())
    {
        int w=q.front();
        q.pop();
        for(auto u : g[w])
        {
            if(v[u]==false)
            {
                v[u]=true;
                d2[u]=d2[w]+1;
                q.push(u);
            }
        }
    }
    vector<vector<int>> asd(n+1);
    f(i,1,n+1)
    {
        asd[i].push_back(abs(d1[i]-d2[i]));
        asd[i].push_back(i);
        if(d1[i]>d2[i]) asd[i].push_back(1);
        else    asd[i].push_back(-1);
    }
    sort(asd.begin(),asd.end());
    int acount=n/2, bcount=n/2, cnt=0;
    vector<char> ans(n+1);
    for(int i=n; i>0; i--)
    {
        if(asd[i][2]<=0 && acount>0)    
        {
            cnt+=d1[asd[i][1]];
            acount--;
            ans[asd[i][1]]='A';
        }
        else if(asd[i][2]>=0 && bcount>0)
        {
            cnt+=d2[asd[i][1]];
            bcount--;
            ans[asd[i][1]]='B';

        } 
        else if(bcount==0)
        {
            cnt+=d1[asd[i][1]];
            acount--;
            ans[asd[i][1]]='A';

        }
        else
        {
            cnt+=d2[asd[i][1]];
            bcount--;
            ans[asd[i][1]]='B';

        }
    }
    cout<<cnt<<"\n";
    f(i,1,n+1)  cout<<ans[i];
    cout<<"\n";
}

int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    int t=1;
    //cin>>t;
    while(t--)  solve();
    return 0;
}
SubtaskSumTestVerdictTimeMemory
base40/40
1Accepted0/03ms1980 KiB
2Accepted0/03ms2136 KiB
3Accepted2/23ms2376 KiB
4Accepted2/23ms2448 KiB
5Accepted3/33ms2396 KiB
6Accepted3/33ms2596 KiB
7Accepted2/23ms2804 KiB
8Accepted2/23ms2808 KiB
9Accepted3/33ms2956 KiB
10Accepted3/33ms3004 KiB
11Accepted2/27ms4324 KiB
12Accepted2/28ms4348 KiB
13Accepted3/326ms6224 KiB
14Accepted3/36ms4660 KiB
15Accepted2/245ms13144 KiB
16Accepted2/248ms13356 KiB
17Accepted3/335ms12756 KiB
18Accepted3/357ms13392 KiB