Connection
Time limit2sMemory limit512 MB
Decide whether all N vertices can be connected by adding candidate edges while every component keeps its vertex weight sum at least its edge weight sum, and print the edge order.
- Level
Medium7 of 10
- Topics
- Union-find, Greedy, Sorting
- Solved
- No attempts yet
Problem
There is a graph with vertices numbered . Vertex () has a non-negative integer weight .
Initially, the graph has no edges. You may add undirected edges to the graph. There are candidate edges, numbered . Edge () connects vertices and and has a non-negative integer weight .
Your goal is to make all vertices connected. To do so, you may repeat the following operation any number of times: choose exactly one candidate edge that has not been added yet and add it to the graph. At every moment, the following condition must hold.
- For every connected component of the graph, (sum of vertex weights) (sum of edge weights).
Determine whether all vertices can be connected.
Input
Output
If it is impossible to connect all vertices, print -1 on a single line.
If it is possible, print one way to add edges as follows.
- On the first line, print the number of edges to add ().
- On each of the next lines, the -th line contains the number () of the -th edge to add.
Constraints
- is an integer,
- is an integer,