click below for solution

Monday, 31 January 2022

Perfect Matching solution|Educational Codeforces round 122 (rated for div 2)

F. Perfect Matching
time limit per test12 seconds
memory limit per test512 megabytes
inputstandard input
outputstandard output
You are given a tree consisting of n vertices (numbered from 1 to n) and n−1 edges (numbered from 1 to n−1). Initially, all vertices except vertex 1 are inactive.

You have to process queries of three types:

1 v — activate the vertex v. It is guaranteed that the vertex v is inactive before this query, and one of its neighbors is active. After activating the vertex, you have to choose a subset of edges of the tree such that each active vertex is incident to exactly one chosen edge, and each inactive vertex is not incident to any of the chosen edges — in other words, this subset should represent a perfect matching on the active part of the tree. If any such subset of edges exists, print the sum of indices of edges in it; otherwise, print 0.
2 — queries of this type will be asked only right after a query of type 1, and there will be at most 10 such queries. If your answer to the previous query was 0, simply print 0; otherwise, print the subset of edges for the previous query as follows: first, print the number of edges in the subset, then print the indices of the chosen edges in ascending order. The sum of indices should be equal to your answer to the previous query.
3 — terminate the program.
Note that you should solve the problem in online mode. It means that you can't read the whole input at once. You can read each query only after writing the answer for the last query. Use functions fflush in C++ and BufferedWriter.flush in Java languages after each writing in your program.

Input
The first line contains one integer n (2≤n≤2⋅105) — the number of vertices of the tree.

Then n−1 lines follow. The i-th line contains two integers ui and vi (1≤ui,vi≤n; ui≠vi) — the endpoints of the i-th edge. These edges form a tree.

Then the queries follow in the format described in the statement, one line per query. There will be at least 2 and at most n+10 queries. The last query (and only the last one) will be of type 3. Note that you can read the i-th query only if you have already given the answer for the query i−1 (except for i=1).

If your answer for one of the queries is incorrect and the judging program recognizes it, instead of the next query, you may receive the integer 0 on a separate line. After receiving it, your program should terminate gracefully, and you will receive "Wrong Answer" verdict. If your program doesn't terminate, your solution may receive some other verdict, like "Time Limit Exceeded", "Idleness Limit Exceeded", etc. Note that the fact that your solution doesn't receive the integer 0, it does not mean that all your answers are correct, some of them will be checked only after your program is terminated.

Output
For each query of type 1 or 2, print the answer on a separate line as described in the statement. Don't forget to flush the output.

solution: 

solution link 1

https://shareus.in/?i=GZatxNl0g


if 1st link is not work then click on another link

Solution link 2

https://shrinke.me/0joa


Solution link 3

https://afly.pro/4dPM5oi


Solution link 4

https://afly.pro/X6gV2u4


How to get link tutorial video link

https://youtu.be/0TSsPHhgwb4

Spanning Tree Queries solution|

                                                                      E. Spanning Tree Queries

time limit per test

4 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

You are given a connected weighted undirected graph, consisting of n vertices and m edges.

You are asked k queries about it. Each query consists of a single integer x. For each query, you select a spanning tree in the graph. Let the weights of its edges be w1,w2,…,wn−1. The cost of a spanning tree is ∑i=1n−1|wi−x| (the sum of absolute differences between the weights and x). The answer to a query is the lowest cost of a spanning tree.

The queries are given in a compressed format. The first p (1≤p≤k) queries q1,q2,…,qp are provided explicitly. For queries from p+1 to kqj=(qj−1a+b)modc.

Print the xor of answers to all queries.

Input

The first line contains two integers n and m (2≤n≤50n−1≤m≤300) — the number of vertices and the number of edges in the graph.

Each of the next m lines contains a description of an undirected edge: three integers vu and w (1≤v,u≤nv≠u0≤w≤108) — the vertices the edge connects and its weight. Note that there might be multiple edges between a pair of vertices. The edges form a connected graph.

The next line contains five integers p,k,a,b and c (1≤p≤105p≤k≤1070≤a,b≤1081≤c≤108) — the number of queries provided explicitly, the total number of queries and parameters to generate the queries.

The next line contains p integers q1,q2,…,qp (0≤qj<c) — the first p queries.

Output

Print a single integer — the xor of answers to all queries.

 

Solution:

solution link 1

if 1st link is not work then click on another link
Solution link 2

Solution link 3

Solution link 4

How to get link tutorial video link

Make Them Equal solution|educational Codeforces round 122 (rated for div 2)

D. Make Them Equal
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

You have an array of integers a of size n. Initially, all elements of the array are equal to 1. You can perform the following operation: choose two integers i (1in) and x (x>0), and then increase the value of ai by aix (i.e. make ai=ai+aix).

After performing all operations, you will receive ci coins for all such i that ai=bi.

Your task is to determine the maximum number of coins that you can receive by performing no more than k operations.

Input

The first line contains a single integer t (1t100) — the number of test cases.

The first line of each test case contains two integers n and k (1n103;0k106) — the size of the array and the maximum number of operations, respectively.

The second line contains n integers b1,b2,,bn (1bi103).

The third line contains n integers c1,c2,,cn (1ci106).

The sum of n over all test cases does not exceed 103.

Output

For each test case, print one integer — the maximum number of coins that you can get by performing no more than k operations.

Solution: 

solution link 1

https://shareus.in/?i=GZatxNl0g


if 1st link is not work then click on another link

Solution link 2

https://shrinke.me/0joa


Solution link 3

https://afly.pro/4dPM5oi


Solution link 4

https://afly.pro/X6gV2u4


How to get link tutorial video link

https://youtu.be/0TSsPHhgwb4