70362023-12-28 10:54:34BenedekMartonÜzletlánccpp11Accepted 40/4079ms11312 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/03ms2004 KiB
2Accepted0/03ms2124 KiB
3Accepted2/23ms2336 KiB
4Accepted2/23ms2520 KiB
5Accepted3/33ms2644 KiB
6Accepted3/33ms2836 KiB
7Accepted2/23ms3044 KiB
8Accepted2/23ms3248 KiB
9Accepted3/33ms3464 KiB
10Accepted3/33ms3672 KiB
11Accepted2/28ms4456 KiB
12Accepted2/210ms4552 KiB
13Accepted3/356ms6536 KiB
14Accepted3/37ms4588 KiB
15Accepted2/263ms10824 KiB
16Accepted2/271ms11060 KiB
17Accepted3/343ms10224 KiB
18Accepted3/379ms11312 KiB