#P9008. [入门赛 #9] 大碗宽面 (Hard Version)
[入门赛 #9] 大碗宽面 (Hard Version)
Background
This problem has exactly the same statement as the Easy Version; only the constraints on and the memory limit are different.
Fusu and her friends are holding a party at the Impart Hotel.
Problem Description
Including Fusu, there are people at this party. However, not every pair of people know each other, and even if two people know each other, their relationship may not be good.
Specifically, any two people may have one of the following three relationships:
- Enemies.
- Friends.
- Strangers.
One important activity at the party is shaking hands. For any two people , whether they shake hands follows these rules:
- If and are friends, then they must shake hands exactly once.
- If and are enemies, then they must not shake hands.
- If and are strangers, and there exists a person such that is a friend of one of and and also an enemy of the other one, then and will not shake hands; otherwise, and must shake hands exactly once.
For the third rule, a simpler wording is: for a pair of strangers, if one person’s friend is the other person’s enemy, then they do not shake hands; otherwise they shake hands.
It is known that there are pairs of friends and pairs of enemies. Apart from these pairs, every other pair of people are strangers.
Please compute how many handshakes happen in total at this party.
Input Format
The first line contains three integers, representing the number of people , the number of friend relationships , and the number of enemy relationships , respectively.
The next lines each contain two integers , indicating that and are friends.
The next lines each contain two integers , indicating that and are enemies.
Output Format
Output one line with one integer, representing the total number of handshakes at this party.
4 2 2
1 2
2 3
1 4
1 3
3
Hint
Explanation for Sample 1
There are , i.e. pairs of people in total.
- are friends, so they shake hands.
- are enemies, so they do not shake hands.
- are enemies, so they do not shake hands.
- are friends, so they shake hands.
- are strangers, but is a friend of and also an enemy of , so they do not shake hands.
- are strangers, and there does not exist anyone who is an enemy of one of and and also a friend of the other, so they shake hands.
Therefore, there are handshakes in total.
Constraints
Let , i.e. is the total number of friend and enemy relationships.
- For of the testdata, it is guaranteed that , , , . No pair of enemies or friends will appear twice, and no pair of people will be both enemies and friends at the same time.
By Yifusu Yi
Translated by ChatGPT 5