74232024-01-08 19:53:08horvathabelÜzletlánccpp17Accepted 40/4082ms13604 KiB
#include <bits/stdc++.h>
using namespace std;
vector<int>g[40001];
vector<int> tava(40001,0);
vector<int> tavb(40001,0);
void bfsa(int x, int n){
    queue<int>q;
    vector<bool> seen;
    seen.resize(n+1,0);
    q.push(x);
    seen[x]=true;
    tava[x]=0;
    while(!q.empty()){
        int v=q.front();
        q.pop();
        for (int edge:g[v]){
            if (!seen[edge]){
                tava[edge]=tava[v]+1;
                seen[edge]=true;
                q.push(edge);
            }
        }
    }
}
void bfsb(int x, int n){
    tavb[x]=0;
    queue<int>q;
    vector<bool> seen;
    seen.resize(n+1,0);
    q.push(x);
    seen[x]=true;

    while(!q.empty()){
        int v=q.front();
        q.pop();
        for (int edge:g[v]){
            if (!seen[edge]){
                tavb[edge]=tavb[v]+1;
                seen[edge]=true;
                q.push(edge);
            }
        }
    }
}
int main()
{
    int n,m,a,b;
    cin>>n>>m>>a>>b;
    for (int i=0; i<m;i++){
        int x,y;
        cin>>x>>y;
        g[x].push_back(y);
        g[y].push_back(x);
    }
    bfsa(a,n);
    bfsb(b,n);
    vector<pair<int,int>> diff;
    int ans=0;
    vector<string> answer;
    answer.resize(n+1,"A");
    diff.resize(n+1,{1000000000,0});
    for (int i=1; i<=n;i++){
        diff[i]={tavb[i]-tava[i],i};
        ans+=tava[i];
    }
    sort(diff.begin(),diff.end());
    for (int i=0; i<n/2;i++){
        ans+=diff[i].first;
        answer[diff[i].second]="B";
    }
    cout<<ans<<endl;
    for (int i=1; i<=n;i++) cout<<answer[i];


}
SubtaskSumTestVerdictTimeMemory
base40/40
1Accepted0/04ms4260 KiB
2Accepted0/04ms4512 KiB
3Accepted2/24ms4752 KiB
4Accepted2/24ms4916 KiB
5Accepted3/34ms5404 KiB
6Accepted3/34ms5196 KiB
7Accepted2/24ms5196 KiB
8Accepted2/24ms5436 KiB
9Accepted3/34ms5820 KiB
10Accepted3/34ms6040 KiB
11Accepted2/28ms6664 KiB
12Accepted2/212ms7244 KiB
13Accepted3/357ms9092 KiB
14Accepted3/38ms7284 KiB
15Accepted2/265ms12808 KiB
16Accepted2/275ms12996 KiB
17Accepted3/348ms12608 KiB
18Accepted3/382ms13604 KiB