Explanation of “mcoi-03” village state


The second question is explained!

Maybe it’s the last question before retirement

(it seems that only two articles have been written in all)

No, topic:

Question surface

Meaning of the title:

There are t data

There is a tree (to ensure that all nodes are connected) and there are n nodes. Each node has its corresponding weight and serial number. Now you need to perform m operations. The operation is as follows:

Find the node with the largest weight (if there is a node with the same weight and the largest one, select the node with the smaller sequence number), and the node directly connected to the node has a weight of + 1 (it does not increase the weight itself)

Finally, the node with the largest weight is output.

Data range:
For 100% data, 1 ≤ n ≤ 2 × 10 ^ 6,
1≤A i≤2^31-1,

Part of the score will not be written.

Anyway, it’s very big. Are you afraid to see that M is less than or equal to the 18th power of 10

The main solutions are as follows:

First of all, look at the topic, someone may not know why it is a tree after reading the problem surfaceOnly you willThe title gives n pointsN-1Two way path connection,
There are n villages and N-1 roads in country C. These roads can be used in both directions. Make sure little s can go from one village to any other village. The N villages are numbered 1 to n.

Make sure little s can go from one village to any other village

As we all know, if you want to form a ring, you need at least the same number of paths as the number of nodes, and the meaning of the topic is to ensure that each node must be connected, there must be n-1 paths
Is a tree.

It’s very wordy

After reading the topic and looking at the data range, we found that the range of M is so large that even the O (m) algorithm can not pass,Don’ts

So I fell into thinking. First, I wrote the law of data push:

Three points
2 6 3
All connected
M operations
Num [2]: 6 Max
3 6 4
Num [2]: 6 Max
4 6 5
Num [2]: 6 Max
5 6 6
There it is! Equal point! However, the number of num [2] is small, and the answer is still num [2];
Num [2]: 6 Max
6 6 7
Num [3] is greater than num [2];
Num [3]: 7 Max
6 7 7
The number of num [2] is small, select num [2].
Num [2]: 7 Max
7 7 8
Num [3] is the largest

We can draw the following rules:

(the weight here refers to the initial weight)

X1 is the node with the largest weight and Y1 is the node with the largest relative weight

  • Just record X1 and Y1, and the other nodes, please, they’re terrible! It can be seen from the data that num [1] connected with 6 is less than num [2], and has no competitiveness in the choice of answer, let alone the node that is not directly connected with the node with the largest weight. (students who don’t think it’s right can write a few sets of data by themselves)

  • When m is less than the difference between num [2] and num [3], the answer is always num [2], (Num [2]: hum, don’t you want to surpass me? )When m is less than x1-y1, select x1;

  • Then, when m > = their difference: look at the data, from the third operation to the sixth operation, the answer is circular. It is easy to get that it is related to the parity of M (if it is equal to the smallest serial number), first subtract x1-y1 from m, take Y1 for odd number, and X1 for even number

All this is based on the premise that the graph is a tree.

Code implementation is OK

And then it was gone

Is that right?

And special judge!

In code implementation, when n = 1, special consideration should be given to the case

I was killed four spots by this pit

85 points gone


Don’t you like it?

Oh, what comments can I make

Recommended Today

Jhipster first installation tutorial

First of all, introduce myself, I am a third rate programmer, oh no, coder! Now I want to learn jhipster. Now I’m thinking about whether to analyze my own growth experience. I hope I can help you. I hope you can support me. Thank you! First of all, we need to eat first-hand resources and […]