1. 程式人生 > >ssoj2092: 交通 (traffic)

ssoj2092: 交通 (traffic)

時間限制: 2 Sec 記憶體限制: 128 MB O2 提交: 93 解決: 30 [提交][狀態][部落格][加入收藏] 題目描述 The center of Gdynia is located on an island in the middle of the Kacza river. Every morning thousands of cars drive through this island from the residential districts on the western bank of the river (using bridge connections to junctions on the western side of the island) to the industrial areas on the eastern bank (using bridge connections from junctions on the eastern side of the island).

The island resembles a rectangle, whose sides are parallel to the cardinal directions. Hence, we view it as an A × B rectangle in a Cartesian coordinate system, whose opposite corners are in points (0, 0) and (A,B). On the island, there are n junctions numbered from 1 to n. The junction number i has coordinates (xi, yi). If a junction has coordinates of the form (0, y), it lies on the western side of the island. Similarly, junctions with the coordinates (A, y) lie on the eastern side. Junctions are connected by streets. Each street is a line segment connecting two junctions. Streets can be either unidirectional or bidirectional. No two streets may have a common point (except for, possibly, a common end in a junction). There are are no bridges or tunnels. You should not assume anything else about the shape of the road network. In particular, there can be streets going along the river bank or junctions with no incoming or outgoing streets. Because of the growing traffic density, the city mayor has hired you to check whether the current road network on the island is sufficient.

He asked you to write a program which determines how many junctions on the eastern side of the island are reachable from each junction on the western side.

格丁尼亞的中心位於Kacza河中的一座島嶼。每天清晨,成千上萬輛汽車通過島嶼從西岸的住宅區 (由橋連線島的西部)到東岸的工業區(由橋連線島的東部)。

該島類似於矩形,它的邊平行於主方向。故可將它看作是笛卡爾座標系中的一個A*B的矩形,它的對角分別為(0, 0)和(A, B)。 島上有n個交通節點,編號為1…n(junction, 此處可理解為廣義的路口),第i個節點座標為(xi, yi)。 如果一個節點的座標為(0, y),它就位於島的西岸。類似的,座標為(A, y)的節點位於島的東岸。 各個節點由街道連線,每條街道用線段連線兩個節點。街道有單向行駛或雙向行駛之分。除端點外任意兩條街道都沒有公共點。也沒有橋樑或者隧道。 你不能對道路網路形狀做任何其他假設。沿河岸的街道或節點可能沒有入口或者出口街道。

由於交通堵塞日趨嚴重,市長聘請你測試島上當前的道路網是否足夠。要求你寫一個程式確定從島的西岸的每個節點能夠到達東岸的多少個節點。

輸入 The first line of the standard input contains four integers n, m, A and B (1 <= n <= 300 000, 0 <= m <= 900 000, 1 <= A,B <= 10^9). They denote the number of junctions in the center of Gdynia, the number of streets and dimensions of the island, respectively. In each of the following n lines there are two integers xi, yi (0 <= xi <= A, 0 <= yi <= B) describing the coordinates of the junction number i. No two junctions can have the same coordinates. The next m lines describe the streets. Each street is represented in a single line by three integers ci, di, ki (1 <= ci, di <= n, ci ≠ di, ki ∈ {1, 2}). Their meaning is that junctions ci and di are connected with a street. If ki = 1, then this is a unidirectional street from ci to di. Otherwise, the street can be driven in both directions. Each unordered pair {ci, di} can appear in the input at most once. You can assume that there is at least one junction on the western side of the island from which it is possible to reach some junction on the eastern side of the island. Additionally, in test cases worth at least 30 points, n,m <= 6 000.

第1行包含4個整數n, m, A, B(1≤n≤300000, 0≤m≤900000,1≤A,B≤10^9),分別表示格丁尼亞市中心的節點數,街道數和島嶼的尺寸。

接下來的n行,每行包含兩個整數xi,yi (0≤xi≤A,0≤yi≤B),表示第i個節點的座標。任意兩個節點的座標都不相同。

再往下的m行表示街道,每條街道用3個整數ci, di, ki(1≤ci, di≤n, ci≠di, ki∈{1,2}),表示節點ci、di有街道連線。如果ki=1,表示從ci到di的街道是單向的,否則,這條街道可以雙向行駛。

每個無序對{ci, di}最多出現1次。

你可以假設西岸節點中至少有1個能夠到達東岸的一些節點。

至少有30分的測試資料,n, m≤6000。

輸出 Your program should write to the standard output one line for each junction on the western side of the island. This line should contain the number of junctions on the eastern side that are reachable from that junction. The output should be ordered according to decreasing y-coordinates of the junctions.

為每個西岸節點輸出1行,包括從這個節點出發能夠到達東岸的節點數目,

輸出根據這些節點的y座標降序排序。

樣例輸入 5 3 1 3 0 0 0 1 0 2 1 0 1 1 1 4 1 1 5 2 3 5 2 樣例輸出 2 0 2 來源 ceoi2011day2

題解: 暴力地想,列舉每一個起點bfs,效率O(N^2),然後怎麼都想不出優化的方法了……題目有一個奇怪的地方,明明是圖論,給座標系幹什麼?難道只是要確定起點、終點嗎?這時注意到題目又有一個容易忽略的性質:道路之間除端點外不相交。什麼意思呢?考慮先把不能到達的終點去掉,以防止干擾,那麼對於剩下的終點,感性地覺得每個起點到達的終點一定不能太隨意,否則會相交。那麼不能隨意到什麼程度呢?畫個圖會發現,若一個起點到達另一個起點能到達的終點的“縫隙”之間,那麼他們一定相交,因為它沒法藉助另一個起點的邊到達,則一定自己有邊穿過另一個節點的路徑。那麼問題轉化為求每個起點能到達中的的區間,考慮分為min,max兩部分考慮,對於min,因為是終點對起點貢獻答案,故考慮反向建圖,將終點縱座標按從小到大順序想左bfs一遍,只要有被走過就不再走,記錄下每個起點被哪個終點搜到即為min值,因為這是能到達的縱座標最小值,max同理。