Title Description
Given an n- n-spots m without edges to FIG. M, now we want to this orientation of FIG.
There P P limitation conditions, each shaped like (x_i, y_i) ( X I , Y I ), represented by a directed graph in which the new, x_i X I be able to walk along the edges of some y_i Y i .
Now you find, whether the direction of each side can be uniquely determined. Please also given side direction can be uniquely determined by these.
answer
- It is easy to find, if I i have added a rear edge, if there is a ring, which above all B side
- Then we consider how the L and R judgment, it is clear that, after completion of sentence B, the rest are acyclic
- Says will not because the direction of two different target path
- This inspired us to use a tree difference, then you know one side of the point
Code
1 #include <cstdio> 2 #include <iostream> 3 #include <algorithm> 4 #include <cstring> 5 using namespace std; 6 const int N=100010; 7 int n,m,p,cnt,head[N],f2[N],f1[N]; 8 struct edge { int x,y,from,id; }e[N*2]; 9 void init (int x,int y,int z) { e[++cnt].x=x,e[cnt].y=y,e[cnt].id=z,e[cnt].from=head[x],head[x]=cnt; } 10 bool vis[N],vis1[N<<1]; 11 char ans[N]; 12 void dfs (int x) 13 { 14 vis[x]=true; 15 for (int i=head[x];i!=-1;i=e[i].from) 16 { 17 int y=e[i].y; 18 if (!vis[y]) vis1[e[i].id]=true,dfs(y); 19 else if (!vis1[e[i].id]) ans[e[i].id]='B',vis1[e[i].id]=true,f1[x]++,f1[y]--; 20 } 21 } 22 void dfs1 (int x) 23 { 24 vis[x]=true; 25 for (int i=head[x];i!=-1;i=e[i].from) 26 if (!vis[e[i].y]) 27 { 28 int y=e[i].y; dfs1(y); 29 if (f1[y]>0) ans[e[i].id]='B'; 30 else if ((f2[y]>0&&(i&1))||(f2[y]<0&&(!(i&1)))) ans[e[i].id]='L'; 31 else if ((f2[y]<0&&(i&1))||(f2[y]>0&&(!(i&1)))) ans[e[i].id]='R'; 32 f1[x]+=f1[y];f2[x]+=f2[y]; 33 } 34 } 35 int main() 36 { 37 scanf("%d%d",&n,&m),memset(head,-1,sizeof(head)); 38 for (int i=1,x,y;i<=m;i++) scanf("%d%d",&x,&y),init(x,y,i),init(y,x,i); 39 scanf("%d",&p); 40 for (int i=1,x,y;i<=p;i++) scanf("%d%d",&x,&y),f2[x]++,f2[y]--; 41 memset(vis,false,sizeof(vis)),memset(vis1,false,sizeof(vis1)); 42 for (int i=1;i<=n;i++) if (!vis[i]) dfs(i); 43 memset(vis,false,sizeof(vis)); 44 for (int i=1;i<=n;i++) if (!vis[i]) dfs1(i); 45 for (int i=1;i<=m;i++) if (!ans[i]) printf("B"); else printf("%c",ans[i]); 46 }