C. Modern Art
time limit per test
2.0 s
memory limit per test
256 MB
input
standard input
output
standard output
Sergey is a modern artist who uses 3D printer in his works. He has just come up with the idea of his next work. He is going to print ncolored bars and place them horizontally one over another (the bars can be considered as horizontal segments on the plane, which have positive lengths and belong to pairwise distinct lines parallel to X-axis), but he also wants to satisfy some conditions (he is an artist, after all). There are two types of conditions: precendence (type A) and intersection (type B).
If there is a precendence condition between two bars i and j, the bar i must be placed strictly to the left of the bar j (that is, X-coordinate of any point of bar i must be strictly less than X-coordinate of any point of bar j).
Intersection condition between bars i and j means that both bars must have at least one point which is not an end such that the X-coordinates of these points are equal.
If there are no conditions between some two bars, they can be placed in any order relatively to each other.
Help Sergey to find perfect X-coordinates for his bars or say that it's impossible. If his work is successful he might even share his earnings with you!
Input
The first line contains three space-separated integers n, a and b (1 ≤ n ≤ 105, 0 ≤ a, b ≤ 105) — the number of bars and the numbers of conditions of type A and type B.
Each of the next a lines contains two space-separated integers fa[i] and sa[i] (1 ≤ fa[i], sa[i] ≤ n, fa[i] ≠ sa[i]) — the indices of bars for which the condition of type A must be satisfied.
Each of the next b lines contains two space-separated integers fb[i] and sb[i] (1 ≤ fb[i], sb[i] ≤ n, fb[i] ≠ sb[i]) — the indices of bars for which the condition of type B must be satisfied.
Output
If the required arrangement of bars doesn't exist, write «NO» (without quotes).
Otherwise in the first line write «YES» (without quotes). Next, write n lines, the i-th of which should contain two integers li and ri(0 ≤ li < ri ≤ 109) — the X-coordinates of the left and right ends of the i-th bar.
Examples
input
Copy
4 2 1 1 2 2 3 3 4
output
Copy
YES 0 1 2 3 4 6 5 7
input
Copy
3 2 0 1 2 2 1
output
Copy
NO
题意:构造n个线段,满足a个1条件,b个2条件。
1条件:线段u的所有点必须严格在限度v的所有点的左边。
2条件:线段u和线段v有交点。
题解:我们令1-n为n条线段的左端点,n+1 - n*2为n条线段的右端点。
如果存在一条有向边u v,那就代表u必须在v左边。
所以对于条件1
我们连u+n到v即可。
对于条件2
我们连v到u+n和u到v+n两种情况。
最后的图如果有环就是NO。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
using namespace std;
struct node{
int to,nex;
}edge[800005];
int head[200005],cnt,vis[200005],ans[200005];
void add(int u,int v){
edge[cnt].to=v;
edge[cnt].nex=head[u];
head[u]=cnt++;
}
vector<int>sp;
void dfs(int t){
vis[t]=1;
for(int i=head[t];~i;i=edge[i].nex){
int v=edge[i].to;
if(vis[v]==1){
printf("NO\n");
exit(0);
}
else if(!vis[v]){
dfs(v);
}
}
vis[t]=-1;
sp.push_back(t);
}
int main(){
int n,a,b,i,j;
memset(head,-1,sizeof(head));
scanf("%d%d%d",&n,&a,&b);
for(i=1;i<=n;i++)add(i,i+n);
for(i=1;i<=a;i++){
int u,v;
scanf("%d%d",&u,&v);
add(u+n,v);
}
for(i=1;i<=b;i++){
int u,v;
scanf("%d%d",&u,&v);
add(v,u+n);
add(u,v+n);
}
for(i=1;i<=2*n;i++){
if(!vis[i])dfs(i);
}
int now=0;
for(i=sp.size()-1;i>=0;i--)ans[sp[i]]=now++;
printf("YES\n");
for(i=1;i<=n;i++)printf("%d %d\n",ans[i],ans[i+n]);
return 0;
}