RabbitWalking
Time limit8sMemory limit512 MB
Given a simple undirected graph, add the maximum number of edges so no closed walk of odd length exists, or print -1 if the graph is already bipartite.
- Level
Medium7 of 10
- Topics
- Graph, Union-find, BFS, Greedy
- Solved
- No attempts yet
Problem
The city where the rabbit lives has intersections and roads. The intersections are numbered starting from 1. The -th road connects intersection and intersection in both directions.
The rabbit likes walking and odd numbers. The rabbit wants to take a walk along a route that starts at some intersection, follows an odd number of roads, and returns to the starting vertex.
The cat, who is the mayor of this city, wants to add many roads connecting distinct pairs of intersections to make travel within the city more efficient. For any pair of intersections, at most one road can be built. The cat is also mischievous, so the cat wants the city to contain no route that follows an odd number of roads and returns to the starting vertex.
Find the maximum number of roads that can be added. If the rabbit's requirement is already satisfied from the start, output -1.
Input
The input is given in the following format:
...
Output
Print one integer on a single line: the maximum number of roads that can be added. If the rabbit's requirement is already satisfied from the start, print -1.
Constraints
- is between 1 and 100,000, inclusive.
- is between 0 and 100,000, inclusive.
- and are distinct.
- No two roads connect the same pair of intersections.