Explosive Materials
InterviewTime limit3sMemory limit256 MB
Split conflicting materials into two safe boxes and minimize the fuller box size.
- Level
Medium5 of 10
- Topics
- Graph, BFS, Dynamic programming
- Solved
- No attempts yet
Problem
Erik is sending samples of kinds of material to his laboratory for a purity analysis. The kinds are numbered 1 to . He packs the samples into boxes of equal capacity. A box has capacity if it holds up to kinds of material.
Some materials react and explode, so they cannot go in the same box. Erik found that if putting kinds in one box explodes, then putting any two of them in one box explodes as well. He wrote down every pair that explodes, and the list has pairs.
Because every explosion comes from a pair, Erik wants to know whether two boxes of equal capacity are enough. All kinds must be split between the two boxes, and no two materials in the same box may form a pair from the list. If that is possible, what is the smallest capacity of a box?
Input
The first line contains the number of test cases ().
The first line of each test case contains two integers and (, ), the number of kinds of material and the number of pairs on the list. Each of the next lines contains two integers and (, ), meaning that material and material explode when they are put in the same box. No pair is listed twice.
Output
For each test case, print one integer on its own line. Print -1 if the materials cannot be sent in two boxes. Otherwise print the smallest capacity of a box.