QOJ.ac

QOJ

Time Limit: 2 s Memory Limit: 256 MB Total points: 100

#22472. 樹中的乳牛

Statistics
插圖

Bessie 與 Farmer John 到 Mootreepolis 旅行卻走散,Bessie 迷路。FJ 組織搜索隊,無論好壞,唯一定位方法是牛叫方向,聲音可在全城聽見。

交通如城市名,是一棵樹。有 $N$ 個地點($1 \le N \le 100,000$),編號 $0$..$N$-$1$,以 $N$-$1$ 條雙向步道連接,兩地路徑唯一。Bessie 在 $N$ 個地點之一。

搜索隊在 $N$ 個地點各一人。FJ 可致電任一人,回報牛叫方向,即哪條離開該地的步道朝向 Bessie,或在該地已找到她。

手機費昂貴,想最少電話找到她。請決定致電地點,尤其保證至多 $20$ 次後能回報位置。

注意:$50\%$ 輸入中 $N$ 至多 $1000$。

先從標準輸入讀地圖,再透過標準輸入輸出(控制台)與評測程式互動。初始每行列接觸某地的步道,列表索引從零起,後續互動需要。

首行 $N$,第二行起按地點 $0$ 起列鄰居。首數 $K$($1 \le K \le N$)為鄰居數,再 $K$ 整數 $N_{0}$、$N_{1}$、…、$N_{K-1}$,為鄰居零基編號。步道連兩地即互為鄰居。

依次向標準輸出寫地點。每次評測器回應通向 Bessie 的步道索引,即初始輸入對應行的下標。從標準輸入讀回應。

輸出 Bessie 所在地時自動終止,不須自行處理。QOJ 找到她後關閉回應流;讀回應遇輸入結束應正常終止。

評測器可自適應選回應,但全部回應須與至少一個可能位置一致。

找到她時得非零分數,具體按查詢數評分:包含當次,至多 $20$ 次得 $10$,至少 $30$ 次得 $0$;其他得 $30$ 減查詢次數。

假設下圖 Bessie 在節點 $5$,右邊是輸入描述:

                               Node Count Neighbors
                0   1            0      1 3
                |   |            1      1 4
             2--3---4--5(B)      2      1 3
                |   |            3      4 0 2 6 4
                6   7            4      4 3 7 1 5
                                 5      1 4
                                 6      1 3
                                 7      1 4

下表一種互動。query 為送標準輸出的值,response 為從標準輸入讀的值,location 為回應所指地點。details 是完整有序鄰居表,以 <> 表解釋:

Query   Response    Location     Neighbor list details 
  2        0           3          <3>
  6        0           3          <3>
  3        3           4          0 2 6 <4>
  4        3           5          3 7 1 <5>
  5        Done; program will be terminated

從 $2$、$6$ 看,最近且唯一鄰居是 $3$;從 $3$ 是 $4$,從 $4$ 是 $5$。她就在 $5$,輸出 $5$ 後評測系統終止程式。

互動程式

須刷新輸出,確保不留在程式緩衝區。

C、C++ 使用 stdio.h 時加入:

setbuf(stdout, 0);

放在程式近開頭處。

C++ iostream 輸入輸出,每次輸出一行這樣刷新:

cout << line << "\n" << flush;

Pascal 每次 writeln 後加:

flush(stdout);

Java 每次輸出語句後加:

System.out.flush();

回覆後逾時,可能未正確刷新輸出。

Java 使用者

以下(希望)為所需輸入輸出框架:

import java.io.*;
import java.util.*;
public class cowtree {
    public static void main(String[] args) throws IOException {
        BufferedReader in = new BufferedReader(new
                                          InputStreamReader(System.in));
        /* write ... */
        System.out.println(...);
        System.out.flush();
        /* read a reply */
        response = in.readLine();
        ...

Node: 節點

Count: 數量

Neighbors: 鄰居

Query: 查詢

Response: 回應

Location: 地點

Neighbor list details: 鄰居列表詳情

Done; program will be terminated: 完成,程式會終止

write ...: 寫入

read a reply: 讀取回應

輸入格式

  • 第 $1$ 行:整數 $N$。

  • 第 $2..N+1$ 行:第 $i+2$ 行以空格分隔整數描述地點 $i$ 鄰居:$K$、$N_{0}$、…、$N_{K-1}$。

輸出格式

  • 無輸出檔案行:寫標準輸出,不寫輸出檔案。

範例

輸入格式 1

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

輸出格式 1

出題者:MIT,2010

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.