CF BUDDY
← Problems·

1738F · Connectivity Addicts

2400 · constructive algorithms, dsu, graphs

Problem: This is an interactive problem.

Given a simple undirected graph with nn vertices numbered from 11 to nn, your task is to color all the vertices such that for every color cc, the following conditions hold:

  1. The set of vertices with color cc is connected;
  2. scnc2s_c \leq n_c^2, where ncn_c is the number of vertices with color cc, and scs_c is the sum of degrees of vertices with color cc.

Initially, you are only given the number nn of vertices and the degree of each vertex.

In each query, you can choose a vertex uu. As a response, you will be given the kk-th edge incident to uu, if this is the kk-th query on vertex uu.

You are allowed to make at most nn queries.

An undirected graph is simple if it does not contain multiple edges or self-loops.

The degree of a vertex is the number of edges incident to it.

A set SS of vertices is connected if for every two different vertices u,vSu, v \in S, there is a path, which only passes through vertices in SS, that connects uu and vv. That is, there is a sequence of edges (u1,v1),(u2,v2),,(uk,vk)(u_1, v_1), (u_2, v_2), \dots, (u_k, v_k) with k1k \geq 1 such that

  1. u1=uu_1 = u, vk=vv_k = v, and vi=ui+1v_i = u_{i+1} for every 1i<k1 \leq i < k; and
  2. ukSu_k \in S and vkSv_k \in S for every 1ik1 \leq i \leq k.

Note: In the example, there is only one test case.

In the test case, there are n=5n = 5 vertices with vertices 1,2,3,41, 2, 3, 4 of degree 22 and vertex 55 of degree 00. It is obvious that vertex 55 is isolated, i.e., it does not connect to any other vertices.

A possible interaction is shown in the sample input and output, where 44 "?" queries are made on vertex 11 twice and vertex 33 twice. According to the responses to these queries, we know that each of vertex 11 and vertex 33 connects to two vertices 22 and 44.

A possible solution is shown in the sample output, where vertex 11 and vertex 22 are colored by 11, vertex 33 and vertex 44 are colored by 22, and vertex 55 is colored by 33. It can be seen that this solution satisfies the required conditions as follows.

  • For color c=1c = 1, vertex 11 and vertex 22 are connected. Moreover, n1=2n_1 = 2 and s1=d1+d2=2+2=4n12=22=4s_1 = d_1 + d_2 = 2 + 2 = 4 \leq n_1^2 = 2^2 = 4;
  • For color c=2c = 2, vertex 33 and vertex 44 are connected. Moreover, n2=2n_2 = 2 and s2=d3+d4=2+2=4n22=22=4s_2 = d_3 + d_4 = 2 + 2 = 4 \leq n_2^2 = 2^2 = 4;
  • For color c=3c = 3, there is only one vertex (vertex 55) colored by 33. Moreover, n3=1n_3 = 1 and s3=d5=0n32=12=1s_3 = d_5 = 0 \leq n_3^2 = 1^2 = 1.

Sample Cases

Case 1

Input

1
5
2 2 2 2 0

2

4

2

4

Output

? 1

? 1

? 3

? 3

! 1 1 2 2 3

Similar problems

00:00:00
Loading editor…
Welcome! I'm your coding tutor for this problem. Use the chips below to reveal stored hints or get AI feedback on your code. I'll guide you step by step — never giving away the solution.

Sign in to unlock AI tutor feedback