Building a Linear Molecule
Time limit5sMemory limit1024 MB
Count the pairs (L, R) where keeping only atoms L through R leaves a molecule whose bonds form a single simple path.
- Level
Medium7 of 10
- Topics
- Graph, Sliding window, Segment tree
- Solved
- No attempts yet
Input
Lev received a molecule as a material for a chemistry experiment. The molecule has atoms, numbered from 1 to with distinct labels, and distinct chemical bonds. Each bond joins two different atoms, so the molecule can be treated as an undirected graph. Unlike real chemistry, the molecule does not have to be connected, does not have to consist of two or more atoms, and an atom may not be linked to four or fewer other atoms.
Lev uses a state-of-the-art machine for the experiment. When Lev enters two integers , the machine removes every atom except the atoms numbered from to , inclusive. Removing atom means removing atom together with all bonds attached to it.
Lev must record the resulting molecule in a report, and the molecule in the report must be linear. That is, the atoms can be rearranged along a straight line so that adjacent atoms are bonded to each other and non-adjacent atoms have no bond between them.
A skilled chemist, Lev can make many such molecules. For extra credit, Lev also wants to report how many distinct linear molecules can be made. Lev needs the number of integer pairs that can be entered into the machine to produce a linear molecule. Write a program that counts these pairs.
Input
The first line contains two integers and , separated by a space. is the number of atoms in the molecule, and is the number of chemical bonds.
Each of the next lines contains two integers and , separated by a space. This means there is a chemical bond between atom and atom . The line numbers are for from 1 to .