Codeforces Round #565 (Div. 3) Editorial
source link: http://codeforces.com/blog/entry/67598
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Codeforces Round #565 (Div. 3) Editorial
3 years ago, # | Problem E was little tricky, learned a lot. Thanks a lot for great contest!! |
can u explain problem E? I haven't understood the tutorial. thanks in advance!! |
-
Maintain a adjacency list, and then arrange the vertices by number of other vertices it is connected to(v[current_vertex].size) in a multiset or priority_queue (descending order). Pop to the priority_queue and mark the vertex as visited and push it in ans_vector. Iterate all the vertices to which it is connected as also mark them true but dont put these vertices in ans. (Because you need to maintain different colour). Do this until all vertices are visited.
Ex: For a tree with 3 vertices and 2 edge: 1-2 and 2-3. Vertex 2 will have max size(2), push it in ans_vector and also mark 1,2 and 3 as true.
Now you have an answer vector. If the size if less than equal n/2 print it otherwise print those which not not in ans_vector . This is because the set of vertices which are in ans_vector and which aren't both satisfies the condition of given problem (expect for size one)
My submission: https://codeforces.com/contest/1176/submission/55381513 Though i was not able to do it in contest.
-
If someone is trying this approach, try this case. You will understand why pure greedy approach won't work.
1
9 8
1 3
2 3
3 4
4 5
4 6
9 7
4 9
9 8Thats why we have to use either the found set or compliment of it.
-
Thanks a lot for this test case. I was thinking greedy will work
-
What do you exactly mean by greedy here, I just ran a dfs on the graph starting with coloring 1 with 0, its uncolored neighbors with 1 and so on, got ac link , then printing each all colored 0 or1 based on their size
Am i missing something,
-
-
please explain the condition when the ans.size() > n/2 if i understood it correctly you want the answer to be all the other remaining vertices. Why is it so?
-
This is because the vertices not present in ans vector will also satisfy the given problem statement.
Consider coloring the vector in answer vector by color 1 and rest by color 2. Now reverse the colors. You can better understand by drawing some examples.
-
-
For the future readers. Here it's not necessary to select nodes with highest priority based on their cardinality since we don't have to optimize the number of selected nodes. Here is my solution. my java solution
-
Alternately, you can try the approach given in tutorial.
Use BFS. Since BFS transverses the graph layer-wise, you can 'color' the alternate layers with color 0 and 1.
Ex:For a tree with 3 vertices and 2 edge: 1-2 and 2-3. Color vertices 1 and 3 with color 0 and 2 with color 1. You would get two answers, the vertices those on even layers and the other on odd. Print the one with minimum size.
This is a more cool approach :)
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK