
There are 'N' kingdoms, numbered from 0 to N-1, connected by 'E' bidirectional roads. A war has broken out, and 'P' of these kingdoms have become hostile invaders.
To protect themselves, the remaining defending kingdoms destroy all roads connected to any of the 'P' invading kingdoms. This action may isolate groups of defending kingdoms from each other.
Your task is to determine the resulting groups of connected, defending kingdoms after the war. A group is a set of kingdoms where every kingdom in the group can reach every other kingdom in the same group through a path of roads that only passes through other defending kingdoms.
The first line of input contains an integer 'N', the number of kingdoms.
The second line contains an integer 'E', the number of roads.
The next 'E' lines each contain two space-separated integers, u and v, representing a road between kingdom u and v.
The next line contains an integer 'P', the number of invading kingdoms.
The last line contains 'P' space-separated integers, representing the numbers of the invading kingdoms.
Print the groups of surviving kingdoms, one group per line.
The kingdoms within each group must be sorted in ascending order.
The groups themselves must be printed in ascending order, based on the number of the first kingdom in each sorted group.
Each kingdom number in the output must be followed by a single space.
The graph is 0-indexed.
7
7
0 1
0 2
1 3
2 4
4 5
4 6
5 6
1
4
0 1 2 3
5 6
Kingdom 4 is the invader. All roads connected to it (`2-4`, `4-5`, `4-6`) are destroyed.
- After removal, kingdoms {0, 1, 2, 3} form one connected group.
- Kingdoms {5, 6} form another connected group.
- The groups are sorted internally and then sorted by their first element for printing.
6
5
0 1
0 2
1 2
3 4
4 5
2
1 4
0 2
3
5
Kingdoms 1 and 4 are invaders. Roads `0-1`, `1-2`, `3-4`, `4-5` are destroyed.
- Kingdom 0 is connected only to kingdom 2. They form a group {0, 2}.
- Kingdom 3 becomes isolated, forming the group {3}.
- Kingdom 5 becomes isolated, forming the group {5}.
- The groups are {0, 2}, {3}, {5}. They are already sorted correctly for output.
The expected time complexity is O(N + E), as it requires a single traversal of the graph.
2 <= N <= 10000
1 <= E <= 20000
1 <= P < N
0 <= kingdom numbers < N
Time limit: 1 sec