#DSU0000032. Liên thông bằng đường bộ và đường sắt (Connectivity)
Bài tập thuộc lộ trình
Mục lục đề ⌄
Liên thông bằng đường bộ và đường sắt (Connectivity)
Connectivity
Source: AtCoder
Version: Phuoc Hung OJ Extended
Problem Statement
There are cities numbered from to . The transportation system has roads and railway links. Every link is bidirectional, but the two transportation networks are considered separately.
Two cities are road-connected if one can travel between them using only roads. They are rail-connected if one can travel between them using only rail links. A city is always considered connected to itself, even if it has no incident links.
For each city , count the cities that are both road-connected and rail-connected to . The two journeys are considered independently and need not follow the same route.
Input
The first line contains ; then road pairs , followed by rail pairs . Pairs are distinct within each network and endpoints are in ascending order.
Output
Print counts for the cities in order.
Subtasks
- Subtask 1 (20 points): , ; , ; edges do not repeat within either network.
- Subtask 2 (30 points): , ; , ; edges do not repeat within either network.
- Subtask 3 (50 points): , ; , ; edges do not repeat within either network.
Examples
Input
4 3 1
1 2
2 3
3 4
2 3
Output
1 2 2 1
Explanation
All four cities are connected by roads. By rail, only cities and are connected, while cities and are separate. The numbers of cities reachable by both modes are therefore 1 2 2 1.