A10590 | Roads Orientation Problem
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
Berland consists of $n$ cities and $m$ bidirectional roads connecting pairs of cities. There is no road connecting a city to itself, and between any pair of cities there is no more than one road. It is possible to reach any city from any other moving along roads.
Currently Mr. President is in the city $s$ and his destination is the city $t$ . He plans to move along roads from $s$ to $t$ ( $s≠t$ ).
That's why Ministry of Fools and Roads has difficult days. The minister is afraid that Mr. President could get into a traffic jam or get lost. Who knows what else can happen!
To be sure that everything goes as planned, the minister decided to temporarily make all roads one-way. So each road will be oriented in one of two possible directions. The following conditions must be satisfied:
- There should be no cycles along roads after orientation.
- The city $s$ should be the only such city that all its roads are oriented out (i.e. there are no ingoing roads to the city $s$ and the city $s$ is the only such city).
- The city $t$ should be the only such city that all its roads are oriented in (i.e. there are no outgoing roads from the city $t$ and the city $t$ is the only such city).
Help the minister solve his problem. Write a program to find any such orientation of all roads or report that no solution exists.
Currently Mr. President is in the city $s$ and his destination is the city $t$ . He plans to move along roads from $s$ to $t$ ( $s≠t$ ).
That's why Ministry of Fools and Roads has difficult days. The minister is afraid that Mr. President could get into a traffic jam or get lost. Who knows what else can happen!
To be sure that everything goes as planned, the minister decided to temporarily make all roads one-way. So each road will be oriented in one of two possible directions. The following conditions must be satisfied:
- There should be no cycles along roads after orientation.
- The city $s$ should be the only such city that all its roads are oriented out (i.e. there are no ingoing roads to the city $s$ and the city $s$ is the only such city).
- The city $t$ should be the only such city that all its roads are oriented in (i.e. there are no outgoing roads from the city $t$ and the city $t$ is the only such city).
Help the minister solve his problem. Write a program to find any such orientation of all roads or report that no solution exists.
输入格式
Each test in this problem contains one or more test cases to solve. The first line of the input contains positive number $T$ — the number of cases to solve.
Each case starts with a line containing four integers $n$ , $m$ , $s$ and $t$ ( $2<=n<=4·10^{5}$ , $1<=m<=10^{6}$ , $1<=s,t<=n$ , $s≠t$ ) — the number of cities, the number of roads and indices of departure and destination cities. The cities are numbered from $1$ to $n$ .
The following $m$ lines contain roads, one road per line. Each road is given as two integer numbers $x_{j}$ , $y_{j}$ ( $1<=x_{j},y_{j}<=n$ , $x_{j}≠y_{j}$ ), which means that the $j$ -th road connects cities $x_{j}$ and $y_{j}$ . There is at most one road between any pair of cities. It is possible to reach any city from any other moving along roads.
The sum of values $n$ over all cases in a test doesn't exceed $4·10^{5}$ . The sum of values $m$ over all cases in a test doesn't exceed $10^{6}$ .
Each case starts with a line containing four integers $n$ , $m$ , $s$ and $t$ ( $2<=n<=4·10^{5}$ , $1<=m<=10^{6}$ , $1<=s,t<=n$ , $s≠t$ ) — the number of cities, the number of roads and indices of departure and destination cities. The cities are numbered from $1$ to $n$ .
The following $m$ lines contain roads, one road per line. Each road is given as two integer numbers $x_{j}$ , $y_{j}$ ( $1<=x_{j},y_{j}<=n$ , $x_{j}≠y_{j}$ ), which means that the $j$ -th road connects cities $x_{j}$ and $y_{j}$ . There is at most one road between any pair of cities. It is possible to reach any city from any other moving along roads.
The sum of values $n$ over all cases in a test doesn't exceed $4·10^{5}$ . The sum of values $m$ over all cases in a test doesn't exceed $10^{6}$ .
输出格式
For each case print "Yes" if the answer exists. In the following $m$ lines print roads in the required directions. You can print roads in arbitrary order. If there are multiple answers, print any of them.
Print the only line "No" if there is no answer for a case.
Print the only line "No" if there is no answer for a case.
输入输出样例
输入 #1
2 4 4 1 2 1 2 2 3 3 4 4 1 3 2 1 3 3 1 2 3
输出 #1
Yes 1 2 3 2 4 3 1 4 No
暂无题解
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted