70382023-12-28 10:58:10BenedekMartonÜzletlánccpp11Accepted 40/4082ms11140 KiB
#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>

using namespace std;

struct s
{
    int er;
    int in;
};

bool f(s a, s b)
{
    return a.er<b.er;
}

int main()
{
    int n, m, a, b, x, y;
    cin >> n >> m >> a >> b;
    vector<int> mat[n+1];
    for(int i=1; i<n+1; i++)
    {
        mat[i].push_back(0);
    }
    for(int i=0; i<m; i++)
    {
        cin >> x >> y;
        mat[x].push_back(y);
        mat[y].push_back(x);
        mat[x][0]++;
        mat[y][0]++;
    }
    int hova[n+1];
    queue<int> q;
    q.push(a);
    for(int i=1; i<n+1; i++)
    {
        hova[i]=-1;
    }
    hova[a]=0;
    while(!q.empty())
    {
        x=q.front();
        q.pop();
        for(int i=1; i<=mat[x][0]; i++)
        {
            if(hova[mat[x][i]]==-1)
            {
                q.push(mat[x][i]);
                hova[mat[x][i]]=hova[x]+1;
            }
        }
    }

    int hovb[n+1];
    q.push(b);
    for(int i=1; i<n+1; i++)
    {
        hovb[i]=-1;
    }
    hovb[b]=0;
    while(!q.empty())
    {
        x=q.front();
        q.pop();
        for(int i=1; i<=mat[x][0]; i++)
        {
            if(hovb[mat[x][i]]==-1)
            {
                q.push(mat[x][i]);
                hovb[mat[x][i]]=hovb[x]+1;
            }
        }
    }
    s hovk[n+1];
    for(int i=1; i<n+1; i++)
    {
        hovk[i].er=hova[i]-hovb[i];
        hovk[i].in=i;
    }
    sort(hovk+1, hovk+n+1, f);
    char kar[n];
    int mink=0;
    for(int i=1; i<=n/2; i++)
    {
        mink=mink+hova[hovk[i].in];
        kar[hovk[i].in-1]='A';
    }
    for(int i=n/2+1; i<=n; i++)
    {
        mink=mink+hovb[hovk[i].in];
        kar[hovk[i].in-1]='B';
    }
    cout << mink << endl;
    for(int i=0; i<n; i++)
        cout << kar[i];
    return 0;
}
SubtaskSumTestVerdictTimeMemory
base40/40
1Accepted0/03ms1812 KiB
2Accepted0/03ms2004 KiB
3Accepted2/23ms2392 KiB
4Accepted2/23ms2512 KiB
5Accepted3/33ms2716 KiB
6Accepted3/33ms2876 KiB
7Accepted2/23ms3084 KiB
8Accepted2/23ms3176 KiB
9Accepted3/33ms3176 KiB
10Accepted3/33ms3404 KiB
11Accepted2/28ms4316 KiB
12Accepted2/210ms4636 KiB
13Accepted3/356ms6652 KiB
14Accepted3/37ms4656 KiB
15Accepted2/263ms10752 KiB
16Accepted2/271ms10876 KiB
17Accepted3/345ms10040 KiB
18Accepted3/382ms11140 KiB