0% found this document useful (0 votes)
4 views

Problem - E - Codeforces

The document describes a programming contest problem from Codeforces Round 798 (Div. 2) where participants must help Vlad connect a graph based on an array of non-negative integers using the minimum number of operations. Each operation allows incrementing or decrementing elements of the array to ensure that the graph remains connected. The input consists of multiple test cases, and the output requires the minimum operations and the modified array for each test case.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Problem - E - Codeforces

The document describes a programming contest problem from Codeforces Round 798 (Div. 2) where participants must help Vlad connect a graph based on an array of non-negative integers using the minimum number of operations. Each operation allows incrementing or decrementing elements of the array to ensure that the graph remains connected. The input consists of multiple test cases, and the output requires the minimum operations and the modified array for each test case.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Enter | Register

HOME TOP CATALOG CONTESTS GYM PROBLEMSET GROUPS RATING EDU API CALENDAR HELP RAYAN

PROBLEMS SUBMIT CODE MY SUBMISSIONS STATUS HACKS ROOM STANDINGS CUSTOM INVOCATION

Codeforces Round 798 (Div. 2)


E. ANDfinity Finished
time limit per test: 4 seconds
memory limit per test: 256 megabytes → Practice?
Bit Lightyear, to the ANDfinity and beyond! Want to solve the contest problems after the
official contest ends? Just register for
practice and you will be able to submit
After graduating from computer sciences, Vlad has been awarded an array a1 , a2 , … , an of n solutions.
non-negative integers. As it is natural, he wanted to construct a graph consisting of n vertices,
numbered 1, 2, … , n . He decided to add an edge between i and j if and only if ai &aj > 0 , Register for practice
where & denotes the bitwise AND operation.

Vlad also wants the graph to be connected, which might not be the case initially. In order to → Virtual participation 
satisfy that, he can do the following two types of operations on the array:
Virtual contest is a way to take part in past
contest, as close as possible to participation
1. Choose some element ai and increment it by 1. on time. It is supported only ICPC mode for
2. Choose some element ai and decrement it by 1 (possible only if ai > 0 ). virtual contests. If you've seen these
problems, a virtual contest is not for you -
solve these problems in the archive. If you
It can be proven that there exists a finite sequence of operations such that the graph will be just want to solve some problem from a
connected. So, can you please help Vlad find the minimum possible number of operations to do contest, a virtual contest is not for you -
solve this problem in the archive. Never use
that and also provide the way how to do that? someone else's code, read the tutorials or
communicate with other person during a
Input virtual contest.

There are several test cases in the input data. The first line contains a single integer t ( Start virtual contest
1 ≤ t ≤ 1000) — the number of test cases. This is followed by the test cases description.

The first line of each test case contains an integer n (2 ≤ n ≤ 2000 ).


→ Problem tags
30
The second line of each test case contains n integers a1 , a2 , … , an (0 ≤ ai < 2 ) — the
bitmasks brute force
elements of the array.
constructive algorithms dfs and similar
It is guaranteed that the sum of n over all test cases does not exceed 2000 .
dsu graphs *2500
No tag edit access
Output
For each test case, print a single integer m in the first line — the minimum number of operations.
In the second line print the array after a valid sequence of operations that have been done such → Contest materials
that the graph from the task becomes connected.
Announcement (en)
If there are multiple solutions, output any.
Tutorial (en)
Example
input Copy

4
5
1 2 3 4 5
2
0 2
2
3 12
4
3 0 0 0

output Copy

0
1 2 3 4 5
2
2 2
1
3 11
3
3 1 1 1

Note
In the first test case, the graph is already connected.

In the second test case, we can increment 0 twice and end up with the array [2, 2]. Since
2&2 = 2 > 0 , the graph is connected. It can be shown that one operation is not enough.
In the third test case, we can decrement 12 once and we end up with an array [3, 11].
3&11 = 3 > 0 hence the graph is connected. One operation has to be done since the graph is

not connected at the beginning.

Codeforces (c) Copyright 2010-2025 Mike Mirzayanov


The only programming contests Web 2.0 platform
Server time: Mar/25/2025 12:35:35UTC-6 (l2).
Desktop version, switch to mobile version.
Privacy Policy

Supported by

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy