74232024-01-08 19:53:08horvathabelÜzletlánccpp17Elfogadva 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];


}
RészfeladatÖsszpontTesztVerdiktIdőMemória
base40/40
1Elfogadva0/04ms4260 KiB
2Elfogadva0/04ms4512 KiB
3Elfogadva2/24ms4752 KiB
4Elfogadva2/24ms4916 KiB
5Elfogadva3/34ms5404 KiB
6Elfogadva3/34ms5196 KiB
7Elfogadva2/24ms5196 KiB
8Elfogadva2/24ms5436 KiB
9Elfogadva3/34ms5820 KiB
10Elfogadva3/34ms6040 KiB
11Elfogadva2/28ms6664 KiB
12Elfogadva2/212ms7244 KiB
13Elfogadva3/357ms9092 KiB
14Elfogadva3/38ms7284 KiB
15Elfogadva2/265ms12808 KiB
16Elfogadva2/275ms12996 KiB
17Elfogadva3/348ms12608 KiB
18Elfogadva3/382ms13604 KiB