题目描述
There are n cars on a coordinate axis OX. Each car is located at an integer point initially and no two cars are located at the same point. Also, each car is oriented either left or right, and they can move at any constant positive speed in that direction at any moment.
More formally, we can describe the i-th car with a letter and an integer: its orientation orii and its location xi. If orii=L, then xi is decreasing at a constant rate with respect to time. Similarly, if orii=R, then xi is increasing at a constant rate with respect to time.
We call two cars irrelevant if they never end up in the same point regardless of their speed. In other words, they won’t share the same coordinate at any moment.
We call two cars destined if they always end up in the same point regardless of their speed. In other words, they must share the same coordinate at some moment.
Unfortunately, we lost all information about our cars, but we do remember m relationships. There are two types of relationships:
1 i j —i-th car and j-th car are irrelevant.
2 i j —i-th car and j-th car are destined.
Restore the orientations and the locations of the cars satisfying the relationships, or report that it is impossible. If there are multiple solutions, you can output any.
Note that if two cars share the same coordinate, they will intersect, but at the same moment they will continue their movement in their directions.
Input
The first line contains two integers, n and m (2≤n≤2⋅105;1≤m≤min(2⋅105,n(n−1)2) — the number of cars and the number of restrictions respectively.
Each of the next m lines contains three integers, type, i, and j (1≤type≤2;1≤i,j≤n;i≠j).
If type = 1, i-th car and j-th car are irrelevant. Otherwise, i-th car and j-th car are destined.
It is guaranteed that for each pair of cars, there are at most 1 relationship between.
Output
In the first line, print either “YES” or “NO” (in any case), whether it is possible to restore the orientations and the locations of the cars satisfying the relationships.
If the answer is “YES”, print n lines each containing a symbol and an integer: orii and xi (orii∈{L,R};−109≤xi≤109) — representing the information of the i-th car.
If the orientation is left, then orii = L. Otherwise orii = R.
xi is the where the i-th car is located. Note that all xi should be distinct.
We can prove that if there exists a solution, then there must be a solution satisfying the constraints on xi.
样例1
输入
4 4
1 1 2
1 2 3
2 3 4
2 4 1
输出
YES
R 0
L -3
R 5
L 6
样例2
输入
3 3
1 1 2
1 2 3
1 1 3
输出
NO
拓扑排序+二分图
C++ 代码
#include<iostream>
#include<cstring>
#include<queue>
using namespace std;
typedef long long ll;
const ll N=2e5+10,M=4e5+10;
ll e[M],h[N],ne[M],idx;
ll color[N];//1为L,2为R
ll a[N],b[N],op[N];//1无关L->R 2相关R->L
ll d[N],toop[N],res;
ll n,m;
queue<ll>q;
void add(ll a,ll b) {
e[idx]=b;
ne[idx]=h[a];
h[a]=idx++;
}
bool dfs(ll u,ll c) {
color[u]=c;
for(ll i=h[u]; i!=-1; i=ne[i]) {
ll j=e[i];
if(!color[j]) {
if(!dfs(j,3-c)) {
return 0;
}
} else if(color[j]==c) {
return 0;
}
}
return 1;
}
bool toopsort() {
for(ll i=1; i<=n; i++) {
if(d[i]==0) {
q.push(i);
}
}
while(q.size()) {
ll t=q.front();
q.pop();
toop[t]=++res;
for(ll i=h[t]; i!=-1; i=ne[i]) {
ll j=e[i];
d[j]--;
if(d[j]==0) {
q.push(j);
}
}
}
return res==n;
}
int main() {
scanf("%lld%lld",&n,&m);
memset(h,-1,sizeof(h));
for(ll i=1; i<=m; i++) {
scanf("%lld%lld%lld",&op[i],&a[i],&b[i]);
add(a[i],b[i]),add(b[i],a[i]);
}
for(ll i=1; i<=n; i++) {
if(!color[i]) {
if(!dfs(i,1)) {
puts("No");
return 0;
}
}
}
memset(h,-1,sizeof(h));
idx=0;
for(ll i=1;i<=m;i++){
}
for(ll i=1; i<=m; i++) {
if(op[i]==1) {
if(color[a[i]]==2) {
add(b[i],a[i]);
d[a[i]]++;
} else {
add(a[i],b[i]);
d[b[i]]++;
}
} else {
if(color[a[i]]==2) {
add(a[i],b[i]);
d[b[i]]++;
} else {
add(b[i],a[i]);
d[a[i]]++;
}
}
}
if(toopsort()) {
puts("Yes");
for(ll i=1; i<=n; i++) {
if(color[i]==1) {
printf("L ");
} else {
printf("R ");
}
printf("%lld\n",toop[i]);
}
} else {
puts("No");
}
return 0;
}