A14779 | Perfect Matching
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
You are given a tree consisting of $n$ vertices (numbered from $1$ to $n$ ) and $n-1$ edges (numbered from $1$ to $n-1$ ). Initially, all vertices except vertex $1$ are inactive.
You have to process queries of three types:
- $1$ $v$ — activate the vertex $v$ . It is guaranteed that the vertex $v$ is inactive before this query, and one of its neighbors is active. After activating the vertex, you have to choose a subset of edges of the tree such that each active vertex is incident to exactly one chosen edge, and each inactive vertex is not incident to any of the chosen edges — in other words, this subset should represent a perfect matching on the active part of the tree. If any such subset of edges exists, print the sum of indices of edges in it; otherwise, print $0$ .
- $2$ — queries of this type will be asked only right after a query of type $1$ , and there will be at most $10$ such queries. If your answer to the previous query was $0$ , simply print $0$ ; otherwise, print the subset of edges for the previous query as follows: first, print the number of edges in the subset, then print the indices of the chosen edges in ascending order. The sum of indices should be equal to your answer to the previous query.
- $3$ — terminate the program.
Note that you should solve the problem in online mode. It means that you can't read the whole input at once. You can read each query only after writing the answer for the last query. Use functions fflush in C++ and BufferedWriter.flush in Java languages after each writing in your program.
You have to process queries of three types:
- $1$ $v$ — activate the vertex $v$ . It is guaranteed that the vertex $v$ is inactive before this query, and one of its neighbors is active. After activating the vertex, you have to choose a subset of edges of the tree such that each active vertex is incident to exactly one chosen edge, and each inactive vertex is not incident to any of the chosen edges — in other words, this subset should represent a perfect matching on the active part of the tree. If any such subset of edges exists, print the sum of indices of edges in it; otherwise, print $0$ .
- $2$ — queries of this type will be asked only right after a query of type $1$ , and there will be at most $10$ such queries. If your answer to the previous query was $0$ , simply print $0$ ; otherwise, print the subset of edges for the previous query as follows: first, print the number of edges in the subset, then print the indices of the chosen edges in ascending order. The sum of indices should be equal to your answer to the previous query.
- $3$ — terminate the program.
Note that you should solve the problem in online mode. It means that you can't read the whole input at once. You can read each query only after writing the answer for the last query. Use functions fflush in C++ and BufferedWriter.flush in Java languages after each writing in your program.
输入格式
The first line contains one integer $n$ ( $2 \le n \le 2 \cdot 10^5$ ) — the number of vertices of the tree.
Then $n-1$ lines follow. The $i$ -th line contains two integers $u_i$ and $v_i$ ( $1 \le u_i, v_i \le n$ ; $u_i \ne v_i$ ) — the endpoints of the $i$ -th edge. These edges form a tree.
Then the queries follow in the format described in the statement, one line per query. There will be at least $2$ and at most $n+10$ queries. The last query (and only the last one) will be of type $3$ . Note that you can read the $i$ -th query only if you have already given the answer for the query $i-1$ (except for $i = 1$ ).
If your answer for one of the queries is incorrect and the judging program recognizes it, instead of the next query, you may receive the integer $0$ on a separate line. After receiving it, your program should terminate gracefully, and you will receive "Wrong Answer" verdict. If your program doesn't terminate, your solution may receive some other verdict, like "Time Limit Exceeded", "Idleness Limit Exceeded", etc. Note that the fact that your solution doesn't receive the integer $0$ , it does not mean that all your answers are correct, some of them will be checked only after your program is terminated.
Then $n-1$ lines follow. The $i$ -th line contains two integers $u_i$ and $v_i$ ( $1 \le u_i, v_i \le n$ ; $u_i \ne v_i$ ) — the endpoints of the $i$ -th edge. These edges form a tree.
Then the queries follow in the format described in the statement, one line per query. There will be at least $2$ and at most $n+10$ queries. The last query (and only the last one) will be of type $3$ . Note that you can read the $i$ -th query only if you have already given the answer for the query $i-1$ (except for $i = 1$ ).
If your answer for one of the queries is incorrect and the judging program recognizes it, instead of the next query, you may receive the integer $0$ on a separate line. After receiving it, your program should terminate gracefully, and you will receive "Wrong Answer" verdict. If your program doesn't terminate, your solution may receive some other verdict, like "Time Limit Exceeded", "Idleness Limit Exceeded", etc. Note that the fact that your solution doesn't receive the integer $0$ , it does not mean that all your answers are correct, some of them will be checked only after your program is terminated.
输出格式
For each query of type $1$ or $2$ , print the answer on a separate line as described in the statement. Don't forget to flush the output.
输入输出样例
输入 #1
6 1 4 6 1 3 2 1 2 5 1 1 4 2 1 2 2 1 3 2 1 5 1 6 2 3
输出 #1
1 1 1 0 0 4 2 1 3 0 0 0
暂无题解
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted