65572023-12-09 15:12:14horvathabelÜzletlánccpp17Wrong answer 13/4078ms13096 KiB
#include <bits/stdc++.h>
using namespace std;
vector<int> g[40001];
queue<pair<int,int>> qa;
queue<pair<int,int>> qb;
vector<int> tav;
vector<bool> seen;
void bfsa(int a){
    queue<int> q;
    tav[a]=0;
    q.push(a);
    seen[a]=true;
    while (!q.empty()){
        int v=q.front();
        q.pop();
        qa.push({v,tav[v]});
        for (int edge:g[v]){
            if (!seen[edge]){
                q.push(edge);
                seen[edge]=true;
                tav[edge]=tav[v]+1;

            }
        }
    }
}
void bfsb(int a){
    queue<int> q;
    tav[a]=0;
    q.push(a);
    seen[a]=true;
    while (!q.empty()){
        int v=q.front();
        q.pop();
        qb.push({v,tav[v]});
        for (int edge:g[v]){
            if (!seen[edge]){
                q.push(edge);
                seen[edge]=true;
                tav[edge]=tav[v]+1;

            }
        }
    }
}
int main()
{
    int n,m,a,b;
    cin>>n>>m>>a>>b;
    for (int i=0; i<m;i++){
        int a,b;
        cin>>a>>b;
        g[a].push_back(b);
        g[b].push_back(a);
    }
    tav.resize(n+1,0);
    seen.resize(n+1,0);
    bfsa(a);
    tav.clear();
    seen.clear();
    tav.resize(n+1,0);
    seen.resize(n+1,0);
    bfsb(b);
    seen.clear();
    seen.resize(n+1,0);
    int cnt=0;
    int ans=0;
    vector<string> ct;
    ct.resize(n+1);
    while (cnt!=n/2){
        while (seen[qa.front().first]){
            qa.pop();
        }
        while (seen[qb.front().first]){
            qb.pop();
        }
        if (qb.front().first==qa.front().first){
            if (qb.front().second<qa.front().second){
                qa.pop();
                while (seen[qa.front().first]){
                    qa.pop();
                }
            }
            else{
                qb.pop();
                while (seen[qb.front().first]){
                    qb.pop();
                }
            }
        }
        ans+=qa.front().second;
        seen[qa.front().first]=true;
        ct[qa.front().first]="A";
        qa.pop();
        ans+=qb.front().second;
        seen[qb.front().first]=true;
        ct[qb.front().first]="B";
        qb.pop();
        cnt++;
    }
    cout<<ans<<endl;
    for (int i=1; i<=n;i++) cout<<ct[i];
}
SubtaskSumTestVerdictTimeMemory
base13/40
1Accepted0/04ms3924 KiB
2Wrong answer0/04ms4232 KiB
3Accepted2/23ms4332 KiB
4Wrong answer0/23ms4236 KiB
5Accepted3/34ms4412 KiB
6Wrong answer0/33ms4632 KiB
7Accepted2/24ms4836 KiB
8Wrong answer0/23ms5172 KiB
9Accepted3/34ms5024 KiB
10Accepted3/34ms5072 KiB
11Wrong answer0/28ms5656 KiB
12Wrong answer0/210ms6220 KiB
13Wrong answer0/354ms8164 KiB
14Wrong answer0/38ms6048 KiB
15Wrong answer0/261ms12452 KiB
16Wrong answer0/270ms12844 KiB
17Wrong answer0/343ms11972 KiB
18Wrong answer0/378ms13096 KiB