CF177C1.Party

普及/提高-

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

To celebrate the second ABBYY Cup tournament, the Smart Beaver decided to throw a party. The Beaver has a lot of acquaintances, some of them are friends with each other, and some of them dislike each other. To make party successful, the Smart Beaver wants to invite only those of his friends who are connected by friendship relations, and not to invite those who dislike each other. Both friendship and dislike are mutual feelings.

More formally, for each invited person the following conditions should be fulfilled:

  • all his friends should also be invited to the party;
  • the party shouldn't have any people he dislikes;
  • all people who are invited to the party should be connected with him by friendship either directly or through a chain of common friends of arbitrary length. We'll say that people _a_1 and a__p are connected through a chain of common friends if there exists a sequence of people _a_2, _a_3, ..., a__p - 1 such that all pairs of people a__i and a__i + 1 (1 ≤ i < p) are friends.

Help the Beaver find the maximum number of acquaintances he can invite.

为庆祝第二届 ABBYY 杯编程大赛,聪明的海狸决定举办一场派对。海狸有很多熟人,其中一些人彼此是朋友,另一些人则互相厌恶。为了让派对圆满成功,聪明的海狸希望只邀请那些通过友谊关系相互连通的人,且不邀请任何彼此厌恶的人。友谊与厌恶均为相互的关系。

更形式化地说,对于每一位被邀请的人,需满足以下条件:

  • 他所有的朋友也都必须被邀请参加派对;
  • 派对中不能出现他所厌恶的任何人;
  • 所有被邀请参加派对的人,都必须通过友谊关系与他直接相连,或通过任意长度的朋友链间接相连。我们称两个人 a1a_1 和 apa_p 通过朋友链相连,当且仅当存在一个人员序列 a2, a3, …, ap−1a_2,\,a_3,\,\dots,\,a_{p-1},使得对所有 ii(1≤i<p1 \le i < p),aia_i 与 ai+1a_{i+1} 彼此是朋友。

请帮助海狸找出他最多能邀请多少位熟人。

输入格式

The first line of input contains an integer n — the number of the Beaver's acquaintances.

The second line contains an integer k — the number of pairs of friends. Next k lines contain space-separated pairs of integers u__i, v__i — indices of people who form the i-th pair of friends.

The next line contains an integer m — the number of pairs of people who dislike each other. Next m lines describe pairs of people who dislike each other in the same format as the pairs of friends were described.

Each pair of people is mentioned in the input at most once . In particular, two persons cannot be friends and dislike each other at the same time.

The input limitations for getting 30 points are:

  • 2 ≤ n ≤ 14

The input limitations for getting 100 points are:

  • 2 ≤ n ≤ 2000

输入的第一行包含一个整数 nn —— 河狸的熟人数。

第二行包含一个整数 kk —— 朋友对的数量。接下来的 kk 行每行包含两个用空格分隔的整数 ui, viu_i,\,v_i —— 表示第 ii 对朋友的两人编号。

下一行包含一个整数 mm —— 相互厌恶的人对的数量。接下来的 mm 行以与朋友对相同的格式描述相互厌恶的人对。

每一对人至多在输入中出现一次 。特别地,两个人不可能既是朋友又相互厌恶。

获得 30 分的输入限制为:

  • 2 ≤ n ≤ 142 \le n \le 14

获得 100 分的输入限制为:

  • 2 ≤ n ≤ 20002 \le n \le 2000

输出格式

Output a single number — the maximum number of people that can be invited to the party. If a group of people that meets all the requirements is impossible to select, output 0.

输出一个整数——最多可以邀请参加聚会的人数。如果不存在满足所有要求的人群组合,则输出 0。

输入输出样例

  • 输入#1

    9
    8
    1 2
    1 3
    2 3
    4 5
    6 7
    7 8
    8 9
    9 6
    2
    1 6
    7 9

    输出#1

    3

说明/提示

Let's have a look at the example.

Two groups of people can be invited: {1, 2, 3} and {4, 5}, thus the answer will be the size of the largest of these groups. Group {6, 7, 8, 9} doesn't fit, since it includes people 7 and 9 who dislike each other. Group {1, 2, 3, 4, 5} also doesn't fit, because not all of its members are connected by a chain of common friends (for example, people 2 and 5 aren't connected).

我们来看一个示例。

可以邀请两组人:{1, 2, 3} 和 {4, 5},因此答案为这两组中较大者(即元素个数最多者)的大小。组 {6, 7, 8, 9} 不可行,因为它包含了彼此厌恶的人 7 和 9。组 {1, 2, 3, 4, 5} 同样不可行,因为其所有成员并非都通过共同朋友链相互连通(例如,人 2 和人 5 并不连通)。

输入解题思路,AI测评打分。不知道怎么写?

首页