题目来源:
2021-2022 ACM-ICPC Latin American Regional Programming Contest
时间:
2022.4.28
题解:
并查集 + 思维 (连通块思想)
要保证A和B得到的田地的产量尽可能相等,所以肯定是A拿第n块田地,B拿第n-1块田地。然后剩下的田地,只要B能拿就拿(与第n-1块田相连的就能拿),剩下的给A。这样就能保证B的田地的产量尽可能大。
因为第n块田的产量比【1,n-1】的田地的产量总和还要大,第n-1块田地同理。
代码:
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define endl "\n"
#define inf 0x3f3f3f3f
#define debug(x) cout<<#x<<" = "<<x<<endl;
#define IOS ios::sync_with_stdio(0); cin.tie(0)
const int M = 10 + 3e5;
const int N = 10 + 3e5;
int fa[N];
int get(int x)
{
if(fa[x] == x) return x;
return fa[x] = get(fa[x]);
}
void merge(int u, int v)
{
int dx = get(u);
int dy = get(v);
if(dx != dy)
{
fa[dx] = dy;
}
}
void slove()
{
int n, m; cin>>n>>m;
for(int i = 1; i <= n; i++) fa[i] = i;
for(int i = 0; i < m; i++)
{
int u, v; cin>>u>>v;
if(max(u, v) == n) continue;
merge(u, v);
}
for(int i = 1; i <= n; i++)
{
if(get(i) == get(n-1)) cout<<'B';
else cout<<"A";
}
cout<<endl;
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("in.in", "r", stdin);
freopen("out.out", "w", stdout);
#endif
IOS;
slove();
return 0;
}