One-Way Streets (oneway)
One-Way Streets (oneway)
题目描述
Once upon a time there was a country with nn cities and mm bidirectional roads connecting them. Technical development led to faster and larger road vehicles which presented a problem—the roads were becoming too narrow for two vehicles travelling in opposite direction. A decision to solve this problem involved turning all the roads into single-lane, one-way (unidirectional) roads.
Making the roads one-way comes at a cost because some of those pairs of cities that were previously connected might no longer be reachable after the change. The government compiled a list of important pairs of cities for which it has to be possible to start in the first city and reach the second one. Your task is to determine in which direction to direct the traffic on every road. It is guaranteed that a solution exists.
For some roads there is no choice about the direction of traffic if you want to obtain a solution. The traffic will flow from the first to the second city (right direction, indicated by letter R) or from the second city towards the first (left direction, indicated by letter L).
However, for some roads there exists a solution with this road directed left, and another (possibly different) solution with the road directed right. You should indicate such roads with a letter B for both directions.
Output a string of length jj. Its i−thi−th character should be
• RR if all solutions require the i−thi−th road to be directed right
• LL if all solutions require the i−thi−th road to be directed left
• BB if a solution exists where the i−thi−th road is directed left, and a solution also exists where the i−thi−th road is directed right
给定一张nn 个点mm条边的无向图,现在想要把这张图定向。
有pp 个限制条件,每个条件形如(xi,yi)(xi,yi),表示在新的有向图当中, xixi要能够沿着一些边走到yiyi。
现在请你求出,每条边的方向是否能够唯一确定。同时请给出这些能够唯一确定的边的方向。
输入
The first line contains the number of cities nn and the number of roads mm. The following mm lines describe the roads with pairs of numbers aiai and bibi, which indicate that there is a road between cities aiai and bibi. There can be more than one road between the same pair of cities and a road can even connect the city with itself.
The next line contains the number of pairs of cities pp that have to be reachable. The next pp lines contain pairs of cities xixi and yiyi, meaning that there has to be a way to start in city xixi and reach yiyi.
第一行两个空格隔开的正整数n,mn,m
接下来mm行,每行两个空格隔开的正整数ai,biai,bi,表示ai,biai,bi 之间有一条边。
接下来一行一个整数pp表示限制条件的个数。
接下来pp行,每行两个空格隔开的正整数xi,yixi,yi,描述一个(xi,yi(xi,yi 的限制条件。
输出
Output a string of length mm as described in the description of the task.
输出一行一个长度为mm 的字符串,表示每条边的答案:
·若第ii 条边必须得要是aiai 指向bibi 的,那么这个字符串的第ii个字符应当为 R;
·若第ii条边必须得要是bibi 指向aiai 的,那么这个字符串的第ii个字符应当为 L;
·否则,若第ii条边的方向无法唯一确定,那么这个字符串的第ii个字符应当为 B。
样例输入
5 6
1 2
1 2
4 3
2 3
1 3
5 1
2
4 5
1 3
样例输出
BBRBBL
提示
Constraints
• 1≤n,m,p≤100,0001≤n,m,p≤100,000
• 1≤ai,bi,xi,yi≤n1≤ai,bi,xi,yi≤n
Subtask 1 (30 points)
• n,m≤1000,p≤100n,m≤1000,p≤100
Subtask 2 (30 points)
• p≤100p≤100
Subtask 3 (40 points)
• no additional constraints
Comment
Let’s show that the fifth road "1 3" can be directed in either direction. Two possible orientations of roads with different directions of the fifth road are LLRLRL and RLRRLL.
来源
solution
边双显然是未知,因为正反两个方案都是合法的。
那就把边双缩成点,然后相当于每次给两个点定向。
我用的是树上倍增。
其他神犇:并查集,差分 orz
tarjan时注意重边,还有图可能是不连通的
#include<cstdio>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<vector>
#define maxn 100000
using namespace std;
int n,m,head[maxn],dfn[maxn],low[maxn],t1,t2,tot=1;
int sc,ins[maxn],zh[maxn],top,co,dy[maxn],p,fa[maxn][22],bj[maxn][22];
int deep[maxn],ans[maxn],vis[maxn];
vector<int>G[maxn];
struct node{
int u,v,nex;
}e[maxn*2];
void lj(int t1,int t2){
tot++;e[tot].u=t1;e[tot].v=t2;e[tot].nex=head[t1];head[t1]=tot;
}
void tarjan(int k,int fa){
dfn[k]=low[k]=++sc;
ins[k]=1,zh[++top]=k;
for(int i=head[k];i;i=e[i].nex){
if(i!=(fa^1)){
if(!dfn[e[i].v]){
tarjan(e[i].v,i);
low[k]=min(low[k],low[e[i].v]);
}
else if(ins[e[i].v])low[k]=min(low[k],dfn[e[i].v]);
}
}
if(low[k]==dfn[k]){
co++;
while(1){
dy[zh[top]]=co;
if(zh[top]==k){top--;break;}
top--;
}
}
}
void dfs(int k,int fath){
vis[k]=1;
fa[k][0]=fath;deep[k]=deep[fath]+1;
int sz=G[k].size();
for(int i=0;i<sz;i++){
int v=G[k][i];
if(v!=fath)dfs(v,k);
}
}
int main()
{
cin>>n>>m;
for(int i=1;i<=m;i++){
scanf("%d%d",&t1,&t2);
lj(t1,t2);lj(t2,t1);
}
for(int i=1;i<=n;i++){
if(!dfn[i])tarjan(i,0);
}
for(int i=1;i<=m;i++){
int t=i+i;int u=dy[e[t].u],v=dy[e[t].v];
if(u!=v){
G[u].push_back(v);G[v].push_back(u);
}
}
for(int i=1;i<=co;i++)if(!vis[i])dfs(i,0);
for(int j=1;j<=20;j++)
for(int i=1;i<=co;i++){
fa[i][j]=fa[fa[i][j-1]][j-1];
}
cin>>p;
for(int i=1;i<=p;i++){
scanf("%d%d",&t1,&t2);
t1=dy[t1],t2=dy[t2];
if(t1==t2)continue;
if(deep[t1]>deep[t2]){
int x=20;
while(x>=0){
if(deep[fa[t1][x]]>=deep[t2]){
bj[t1][x]=1;t1=fa[t1][x];
}
x--;
}
}
if(deep[t1]<deep[t2]){
int x=20;
while(x>=0){
if(deep[fa[t2][x]]>=deep[t1]){
bj[t2][x]=-1;t2=fa[t2][x];
}
x--;
}
}
int x=20;
while(x>=0){
if(fa[t1][x]!=fa[t2][x]){
bj[t1][x]=1,bj[t2][x]=-1;
t1=fa[t1][x],t2=fa[t2][x];
}
x--;
}
if(t1!=t2)bj[t1][0]=1,bj[t2][0]=-1;
}
for(int j=20;j>0;j--){
for(int i=1;i<=co;i++){
if(bj[i][j]!=0){
bj[i][j-1]=bj[i][j];
bj[fa[i][j-1]][j-1]=bj[i][j];
}
}
}
for(int i=1;i<=m;i++){
int t=i+i;int u=dy[e[t].u],v=dy[e[t].v];
if(u!=v){
if(fa[u][0]==v)ans[i]=bj[u][0];
else ans[i]=-bj[v][0];
}
}
for(int i=1;i<=m;i++){
if(ans[i]==1)printf("R");
if(ans[i]==0)printf("B");
if(ans[i]==-1)printf("L");
}
return 0;
}
One-Way Streets (oneway)的更多相关文章
- Luogu4652 CEOI2017 One-Way Streets 树上差分
传送门 题意:给出$N$个点.$M$条无向边的图,现在你需要给它定向,并满足$Q$个条件:每个条件形如$(x_i,y_i)$,表示定向之后需要存在路径从$x_i$走向$y_i$.问每条边是否都有唯一定 ...
- 【刷题】LOJ 2480 「CEOI2017」One-Way Streets
题目描述 给定一张 \(n\) 个点 \(m\) 条边的无向图,现在想要把这张图定向. 有 \(p\) 个限制条件,每个条件形如 \((xi,yi)\) ,表示在新的有向图当中,\(x_i\) 要能够 ...
- loj2480 [CEOI2017]One-Way Streets 边双+树上差分
边双无法确定 缩完边双就是一棵树 树上差分随意弄一下吧... #include <vector> #include <cstdio> #include <cstring& ...
- [CEOI2017]One-Way Streets
题目大意: 给你一个无向图,现在告诉你一些点对(u,v), 要你在保证从u到v的所有路径都不变的情况下,尽可能把所有的边变成单向边, 问你可以唯一确定哪些边的方向,以及方向是从u到v还是从v到u. 思 ...
- @loj - 2480@ 「CEOI2017」One-Way Streets
目录 @description@ @solution@ @accepted code@ @details@ @description@ 给定一张 n 个点 m 条边的无向图,现在想要把这张图定向. 有 ...
- UVALive 2664 One-way traffic
One-way traffic Time Limit: 3000ms Memory Limit: 131072KB This problem will be judged on UVALive. Or ...
- Java实现One-way traffic(单向交通)
One-way traffic In a certain town there are n intersections connected by two- and one-way streets. T ...
- codeforces 723E:One-Way Reform
Description There are n cities and m two-way roads in Berland, each road connects two cities. It is ...
- WCF分布式开发步步为赢(10):请求应答(Request-Reply)、单向操作(One-Way)、回调操作(Call Back).
WCF除了支持经典的请求应答(Request-Reply)模式外,还提供了什么操作调用模式,他们有什么不同以及我们如何在开发中使用这些操作调用模式.今天本节文章里会详细介绍.WCF分布式开发步步为赢( ...
随机推荐
- 【luogu P3608 [USACO17JAN]Balanced Photo平衡的照片】 题解
题目链接:https://www.luogu.org/problemnew/show/P3608 乍一看很容易想到O(N^2)的暴力. 对于每个H[i]从i~i-1找L[i]再从i+1~n找R[i], ...
- Bootstrap 历练实例-轮播(carousel)插件的事件
事件 下表列出了轮播(Carousel)插件中要用到的事件.这些事件可在函数中当钩子使用. 事件 描述 实例 slide.bs.carousel 当调用 slide 实例方法时立即触发该事件. $(' ...
- 在React中使用Redux数据流
问题:数据流是什么呢?为什么要用数据流? 答案:1.数据流是我们的行为与相应的抽象 2.使用数据流帮助我们明确了行为的对应的响应 问题: React与数据流的关系 1.React是纯 V 层的前端框架 ...
- git提交时报错 permission denied
git push 时报错:permission denied xxx 目前很多解决办法是生成公钥和秘钥,这种方法安全可靠,比较适用于一台电脑对应一个git账户,但是多个账户在同一台电脑上提交使用git ...
- Vue-Router基础学习笔记
1.安装vue-router npm install vue-router yarn add vue-router 2.引入注册vue-router import Vue from 'vue' imp ...
- 【PHP】php中json_decode()和json_encode()
1.json_decode() json_decode (PHP 5 >= 5.2.0, PECL json >= 1.2.0) json_decode — 对 JSON 格式的字符串进行 ...
- Fakeapp 入门教程(2):使用篇!
Fakeapp软件的使用主要分成了三个步骤, 使用之前请确保你的电脑配置还可以,推荐配置是:一张显存大于4G的N卡.Fakeapp是有支持CPU选项,但是用CPU跑非常慢. 获取脸部图片 训练模型 生 ...
- Python知识点进阶——迭代器
可迭代对象 可迭代对象可以简单的理解为用for循环遍历的,如list.tuple.dict.set.str 判断一个对象是否是迭代器: 可以将数据类型 和 是否为可迭代对象 比较来判断是否是可以迭代 ...
- pandas知识点(汇总和计算描述统计)
调用DataFrame的sum方法会返还一个含有列的Series: In [5]: df = DataFrame([[1.4,np.nan],[7.1,-4.5],[np.nan,np.nan],[0 ...
- 初学Python不知道做什么项目好?来看看练手项目如何?
对于初学者来说,在学习编程的初期,由于基础知识点的学习是比较无聊的,所以大家可能会有所反感,为了减弱大家的反感,我给大家带来一个简单的小项目——实现屏保计时器,这个项目就算是刚学Python的小伙伴, ...