942021-01-11 22:32:04Babják PéterÜzletlánccpp11Runtime error 20/402ms2124 KiB
#include <bits/stdc++.h>
#define N 2001
using namespace std;
int A[N];
int B[N];
bool volt[N];
bool volt2[N];
vector<int>adj[N];
bool e=0;
void bfs(int z)
{
	vector<int>sor;
	sor.push_back(z);
	for(int i=0;i<sor.size();i++)
	{
		int v=sor[i];
		for(int u: adj[v])
		{
			if(volt[u]==0)
			{
				A[u]=A[v]+1;
				sor.push_back(u);
				volt[u]=1;
			}
		}
	}
}
void bfs2(int z)
{
	vector<int>sor;
	sor.push_back(z);
	for(int i=0;i<sor.size();i++)
	{
		int v=sor[i];
		for(int u: adj[v])
		{
			if(volt2[u]==0)
			{
				B[u]=B[v]+1;
				sor.push_back(u);
				volt2[u]=1;
			}
		}
	}
}
struct pen
{
	int val;
	int ind;	
};
struct cmp
{
	bool operator()(const pen &a,const pen &b)
	{
		return a.val<b.val;
	}	
};
int main()
{
	int n,m,a,b;
	ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
	cin>>n>>m>>a>>b;a--;b--;
	A[a]=0;
	B[b]=0;
	for(int i=0;i<m;i++)
	{
		int c,d;cin>>c>>d;c--;d--;
		adj[c].push_back(d);
		adj[d].push_back(c);
	}
	volt[a]=1;
	bfs(a);
	volt2[b]=1;
	bfs2(b);
	pen p[n];
	for(int i=0;i<n;i++)
	{
		pen g;
		g.ind=i;
		g.val=A[i]-B[i];
		p[i]=g;
	}
	sort(p,p+n,cmp());
	
	bool ch[n];
	int sum=0;
	for(int i=0;i<n/2;i++)
	{
		ch[p[i].ind]=0;
		sum+=A[p[i].ind];
	}
	for(int i=n/2;i<n;i++)
	{
		ch[p[i].ind]=1;
		sum+=B[p[i].ind];
	}
	cout<<sum<<endl;
	char t[2]={'A','B'};
	for(int i=0;i<n;i++)
	{
		cout<<t[ch[i]];
	}
	return 0;
}
SubtaskSumTestVerdictTimeMemory
base20/40
1Accepted0/02ms1812 KiB
2Accepted0/01ms1972 KiB
3Accepted2/21ms1980 KiB
4Accepted2/21ms1908 KiB
5Accepted3/32ms1916 KiB
6Accepted3/31ms1920 KiB
7Accepted2/21ms1988 KiB
8Accepted2/21ms1932 KiB
9Accepted3/31ms1940 KiB
10Accepted3/31ms2020 KiB
11Runtime error0/22ms2056 KiB
12Runtime error0/21ms2068 KiB
13Runtime error0/31ms2088 KiB
14Runtime error0/31ms2104 KiB
15Runtime error0/21ms2096 KiB
16Runtime error0/21ms2060 KiB
17Runtime error0/31ms2108 KiB
18Runtime error0/31ms2124 KiB