Tree Craftsman
Time limit1sMemory limit1024 MB
Given a simple graph, count the ways to add edges so it becomes a tree, or report -1 if that count exceeds K.
- Level
Medium6 of 10
- Topics
- Graph, Union-find, Math
- Solved
- No attempts yet
Problem
Sejong is a famous tree craftsman in Hanyang. Bring him any graph and he turns it into a fine tree!
You are given a simple graph with vertices and edges. A simple graph is one where every edge connects two distinct vertices, and no two vertices and are connected by more than one edge.
Sejong adds zero or more new edges between the vertices so that the graph becomes a tree.
Before he starts, Sejong wants to estimate how many ways there are to turn the graph into a tree. Help him check whether that number is greater than , and if it is not, find the exact count. Two ways are different only if the sets of added edges differ. All edges are undirected.
Input
The first line contains three integers , , and separated by spaces. (, , )
Each of the next lines contains two vertices and separated by a space, the endpoints of the -th edge. (, )
Output
If the number of ways to turn the graph into a tree is greater than , print -1. Otherwise, print the exact number of ways.