当前位置:网站首页>[code source] daily question farmland Division
[code source] daily question farmland Division
2022-07-25 09:38:00 【self_ disc】
2022.05.12
Topic link : Farmland division - subject - Daimayuan Online Judge
Title Description
John is a farmer , His farm has n Block field , Number from 1 To n, this n Block field pass m A two-way road connects ( The data guarantees that n All fields are connected ), Let's assume that No i A field will produce 2^i kg Revenue , Now John wants to leave all the farm work to his two children , The division method must meet the following rules :
1. Every field needs to be given to just one child .
2. The farmland allocated to the two children must be connected . That is to say, for any child in any field allocated to him , Can not pass through another child's field , Reach any of your fields .
3. The benefits divided between the two children must be as equal as possible , If it cannot be equal , Older children get the bigger share .
For the first i Block field , If you're going to give it to older children , Please export A, Otherwise output B.
Topic input
On the first line, enter two integers representing n,m Next m That's ok , Every two integers u,v, It means that the two farmland are directly connected by a two-way road , Data guarantees that there are no double edges and self rings
Topic output
Output a string , For the answer
The sample input 1
3 2
1 3
3 2Sample output 1
ABAThe sample input 2
6 6
3 5
2 6
1 3
3 6
5 1
4 6Sample output 2
BABABAData range
2≤n≤3e5,1≤m≤3e5
analysis :
The title requires that n Each field is divided into two parts , And each part needs to be connected .
Analyze the income of each field , Right. i The income of this field is 2^i, front (1~i-1) The sum of blocks is at most 2^i-1, That is to say, even if you take 1~i-1 I didn't get a piece i The benefits of each piece are great , That is, the larger the number , The bigger the proceeds. . So for n Block plot , To divide evenly , The first n The piece must be for A Of , In the case that Unicom is satisfied, it will be distributed to B The more, the better , And the larger the number, the better , therefore n-1 Must be assigned to A.
therefore , Just connect with n-1 The connected fields are allocated to B Give the rest to A( Union checking set ).
See the code for details. :
#include <bits/stdc++.h>
using namespace std;
int n, m;
int far[300009];
inline int find(int x) // lookup
{
return (far[x] == x) ? x : far[x] = find(far[x]);
}
inline void unionn(int x, int y) // Merge
{
x = find(x);
y = find(y);
if (x != y)
{
if (x < y) // In order to make n-1 The father of Unicom's number is n-1, So merge the smaller ones into the larger ones
swap(x, y);
far[y] = far[x]; // take y My father became x Father
}
}
int main()
{
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i++)
far[i] = i; // And search set initialization
while (m--)
{
int x, y;
scanf("%d%d", &x, &y);
if (x == n || y == n) // B Our fields cannot pass A In the field to reach Unicom
// Topics ensure connectivity , Not with B Unicom will definitely work with A Unicom
continue;
else
unionn(x, y); // Merge
}
for (int i = 1; i <= n; i++)
if (find(i) == n - 1) // And B Unicom is assigned to B
cout << "B";
else
cout << "A";
}边栏推荐
猜你喜欢
![[GPLT] 2022 大众情人(floyd)](/img/30/c96306ca0a93f22598cec80edabd6b.png)
[GPLT] 2022 大众情人(floyd)

OC -- Foundation -- string + date and time

Assignment 7.21 Joseph Ring problem and decimal conversion

深入解读C语言随机数函数和如何实现随机数
![自定义 view 实现兑奖券背景[初级]](/img/97/53e28673dcd52b31ac7eb7b00d42b3.png)
自定义 view 实现兑奖券背景[初级]

How to write Android switching interface with kotlin

TCP network application development process

Prim 最小生成树(图解)

main函数的一些操作

~5 ccf 2021-12-2 序列查询新解
随机推荐
为什么要使用JSON.stringify()和JSON.parse()
laravel 调用第三方 发送邮件 (php)
[code source] score split of one question per day
Data query language (DQL)
Go foundation 1
Object initialization
How to deploy the jar package to the server? Note: whether the startup command has nohup or not has a lot to do with it
Prim 最小生成树(图解)
OC--Foundation--集合
Some operations of main function
App的生命周期和AppleDelegate,SceneDelegate
Understand the execution process of try, catch and finally (including return) (the most detailed analysis of the whole network)
~1 ccf 2022-06-2 寻宝!大冒险!
How to configure SSH after changing the computer
单例模式(Singleton)
[GPLT] 2022 大众情人(floyd)
@3-2 CCF 2020-12-2 期末预测之最佳阈值
【代码源】每日一题 国家铁路
[code source] a prime number of fun every day (BFS)
Android 如何使用adb命令查看应用本地数据库