77452024-01-10 22:44:21mmatedÜzletlánccpp17Wrong answer 33/4068ms18876 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(d1[i]-d2[i]);
        asd[i].push_back(i);
    }
    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][0]<0 && acount>0)    
        {
            cnt+=d1[asd[i][1]];
            acount--;
            ans[asd[i][1]]='A';
        }
        else if(asd[i][0]>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
base33/40
1Accepted0/03ms1832 KiB
2Accepted0/03ms2168 KiB
3Accepted2/23ms2356 KiB
4Accepted2/23ms2488 KiB
5Accepted3/33ms2696 KiB
6Accepted3/33ms2776 KiB
7Accepted2/23ms2784 KiB
8Accepted2/23ms3052 KiB
9Accepted3/33ms3168 KiB
10Accepted3/33ms3244 KiB
11Wrong answer0/28ms5188 KiB
12Wrong answer0/28ms5460 KiB
13Accepted3/330ms8744 KiB
14Accepted3/37ms5336 KiB
15Accepted2/257ms17904 KiB
16Accepted2/259ms18228 KiB
17Wrong answer0/341ms17480 KiB
18Accepted3/368ms18876 KiB