78752024-01-11 15:33:41mmatedÜzletlánccpp17Elfogadva 40/4067ms19068 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,h;

void solve()
{
    int n,m,a,b;
    cin>>n>>m>>a>>b;
    g.resize(n+1);
    h.resize(n+1);

    int x,y;
    f(i,0,m)
    {
        cin>>x>>y;
        g[x].push_back(y);
        g[y].push_back(x);
        h[x].push_back(y);
        h[y].push_back(x);
    }

    queue<int> q1;
    q1.push(a);
    vector<int> d1(n+1,inf);
    vector<bool> v1(n+1,false);
    d1[a]=0;
    v1[a]=true;
    while(!q1.empty())
    {
        int w=q1.front();
        q1.pop();
        for(auto u : g[w])
        {
            if(v1[u]==false)
            {
                v1[u]=true;
                d1[u]=d1[w]+1;
                q1.push(u);
            }
        }
    }
    queue<int> q2;
    q2.push(b);
    vector<int> d2(n+1,inf);
    vector<bool> v2(n+1,false);
    d2[b]=0;
    v2[b]=true;
    while(!q2.empty())
    {
        int w=q2.front();
        q2.pop();
        for(auto u : h[w])
        {
            if(v2[u]==false)
            {
                v2[u]=true;
                d2[u]=d2[w]+1;
                q2.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);
    //f(i,1,n+1)    cout<<asd[i][0]<<"\n";
    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;
}
RészfeladatÖsszpontTesztVerdiktIdőMemória
base40/40
1Elfogadva0/03ms1892 KiB
2Elfogadva0/03ms2100 KiB
3Elfogadva2/23ms2196 KiB
4Elfogadva2/23ms2272 KiB
5Elfogadva3/33ms2268 KiB
6Elfogadva3/33ms2396 KiB
7Elfogadva2/23ms2484 KiB
8Elfogadva2/23ms2500 KiB
9Elfogadva3/33ms2836 KiB
10Elfogadva3/33ms2916 KiB
11Elfogadva2/28ms4776 KiB
12Elfogadva2/29ms5008 KiB
13Elfogadva3/330ms8676 KiB
14Elfogadva3/37ms5260 KiB
15Elfogadva2/257ms18092 KiB
16Elfogadva2/261ms18408 KiB
17Elfogadva3/341ms17664 KiB
18Elfogadva3/367ms19068 KiB