A14199 | Phoenix and Odometers
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
In Fire City, there are $n$ intersections and $m$ one-way roads. The $i$ -th road goes from intersection $a_i$ to $b_i$ and has length $l_i$ miles.
There are $q$ cars that may only drive along those roads. The $i$ -th car starts at intersection $v_i$ and has an odometer that begins at $s_i$ , increments for each mile driven, and resets to $0$ whenever it reaches $t_i$ . Phoenix has been tasked to drive cars along some roads (possibly none) and return them to their initial intersection with the odometer showing $0$ .
For each car, please find if this is possible.
A car may visit the same road or intersection an arbitrary number of times. The odometers don't stop counting the distance after resetting, so odometers may also be reset an arbitrary number of times.
There are $q$ cars that may only drive along those roads. The $i$ -th car starts at intersection $v_i$ and has an odometer that begins at $s_i$ , increments for each mile driven, and resets to $0$ whenever it reaches $t_i$ . Phoenix has been tasked to drive cars along some roads (possibly none) and return them to their initial intersection with the odometer showing $0$ .
For each car, please find if this is possible.
A car may visit the same road or intersection an arbitrary number of times. The odometers don't stop counting the distance after resetting, so odometers may also be reset an arbitrary number of times.
输入格式
The first line of the input contains two integers $n$ and $m$ ( $2 \le n \le 2 \cdot 10^5$ ; $1 \le m \le 2 \cdot 10^5$ ) — the number of intersections and the number of roads, respectively.
Each of the next $m$ lines contain three integers $a_i$ , $b_i$ , and $l_i$ ( $1 \le a_i, b_i \le n$ ; $a_i \neq b_i$ ; $1 \le l_i \le 10^9$ ) — the information about the $i$ -th road. The graph is not necessarily connected. It is guaranteed that between any two intersections, there is at most one road for each direction.
The next line contains an integer $q$ ( $1 \le q \le 2 \cdot 10^5$ ) — the number of cars.
Each of the next $q$ lines contains three integers $v_i$ , $s_i$ , and $t_i$ ( $1 \le v_i \le n$ ; $0 \le s_i < t_i \le 10^9$ ) — the initial intersection of the $i$ -th car, the initial number on the $i$ -th odometer, and the number at which the $i$ -th odometer resets, respectively.
Each of the next $m$ lines contain three integers $a_i$ , $b_i$ , and $l_i$ ( $1 \le a_i, b_i \le n$ ; $a_i \neq b_i$ ; $1 \le l_i \le 10^9$ ) — the information about the $i$ -th road. The graph is not necessarily connected. It is guaranteed that between any two intersections, there is at most one road for each direction.
The next line contains an integer $q$ ( $1 \le q \le 2 \cdot 10^5$ ) — the number of cars.
Each of the next $q$ lines contains three integers $v_i$ , $s_i$ , and $t_i$ ( $1 \le v_i \le n$ ; $0 \le s_i < t_i \le 10^9$ ) — the initial intersection of the $i$ -th car, the initial number on the $i$ -th odometer, and the number at which the $i$ -th odometer resets, respectively.
输出格式
Print $q$ answers. If the $i$ -th car's odometer may be reset to $0$ by driving through some roads (possibly none) and returning to its starting intersection $v_i$ , print YES. Otherwise, print NO.
输入输出样例
输入 #1
4 4 1 2 1 2 3 1 3 1 2 1 4 3 3 1 1 3 1 2 4 4 0 1
输出 #1
YES NO YES
输入 #2
4 5 1 2 1 2 3 1 3 1 2 1 4 1 4 3 2 2 1 2 4 4 3 5
输出 #2
YES YES
The illustration for the first example is below:
In the first query, Phoenix can drive through the following cities: $1$ $\rightarrow$ $2$ $\rightarrow$ $3$ $\rightarrow$ $1$ $\rightarrow$ $2$ $\rightarrow$ $3$ $\rightarrow$ $1$ . The odometer will have reset $3$ times, but it displays $0$ at the end.
In the second query, we can show that there is no way to reset the odometer to $0$ and return to intersection $1$ .
In the third query, the odometer already displays $0$ , so there is no need to drive through any roads.
Below is the illustration for the second example:

In the first query, Phoenix can drive through the following cities: $1$ $\rightarrow$ $2$ $\rightarrow$ $3$ $\rightarrow$ $1$ $\rightarrow$ $2$ $\rightarrow$ $3$ $\rightarrow$ $1$ . The odometer will have reset $3$ times, but it displays $0$ at the end.
In the second query, we can show that there is no way to reset the odometer to $0$ and return to intersection $1$ .
In the third query, the odometer already displays $0$ , so there is no need to drive through any roads.
Below is the illustration for the second example:

C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted