bzoj 2387: [Ceoi2011]Traffic
bzoj 2387: [Ceoi2011]Traffic
题目描述
The center of Gdynia is located on an island in the middle of the Kacza river. Every morning thousands of cars drive through this island from the residential districts on the western bank of the river (using bridge connections to junctions on the western side of the island) to the industrial areas on the eastern bank (using bridge connections from junctions on the eastern side of the island).
The island resembles a rectangle, whose sides are parallel to the cardinal directions. Hence, we view it as an A × B rectangle in a Cartesian coordinate system, whose opposite corners are in points (0, 0) and (A,B). On the island, there are n junctions numbered from 1 to n. The junction number i has coordinates (xi, yi). If a junction has coordinates of the form (0, y), it lies on the western side of the island. Similarly, junctions with the coordinates (A, y) lie on the eastern side. Junctions are connected by streets. Each street is a line segment connecting two junctions. Streets can be either unidirectional or bidirectional. No two streets may have a common point (except for, possibly, a common end in a junction). There are are no bridges or tunnels. You should not assume anything else about the shape of the road network. In particular, there can be streets going along the river bank or junctions with no incoming or outgoing streets. Because of the growing traffic density, the city mayor has hired you to check whether the current road network on the island is sufficient.
He asked you to write a program which determines how many junctions on the eastern side of the island are reachable from each junction on the western side.
格丁尼亚的中心位于Kacza河中的一座岛屿。每天清晨,成千上万辆汽车通过岛屿从西岸的住宅区 (由桥连接岛的西部)到东岸的工业区(由桥连接岛的东部)。
该岛类似于矩形,它的边平行于主方向。故可将它看作是笛卡尔坐标系中的一个A*B的矩形,它的对角分别为(0, 0)和(A, B)。 岛上有n个交通节点,编号为1…n(junction, 此处可理解为广义的路口),第i个节点坐标为(xi, yi)。 如果一个节点的坐标为(0, y),它就位于岛的西岸。类似的,坐标为(A, y)的节点位于岛的东岸。 各个节点由街道连接,每条街道用线段连接两个节点。街道有单向行驶或双向行驶之分。除端点外任意两条街道都没有公共点。也没有桥梁或者隧道。 你不能对道路网络形状做任何其他假设。沿河岸的街道或节点可能没有入口或者出口街道。
由于交通堵塞日趋严重,市长聘请你测试岛上当前的道路网是否足够。要求你写一个程序确定从岛的西岸的每个节点能够到达东岸的多少个节点。
输入
The first line of the standard input contains four integers n, m, A and B (1 <= n <= 300 000, 0 <= m <= 900 000, 1 <= A,B <= 10^9). They denote the number of junctions in the center of Gdynia, the number of streets and dimensions of the island, respectively. In each of the following n lines there are two integers xi, yi (0 <= xi <= A, 0 <= yi <= B) describing the coordinates of the junction number i. No two junctions can have the same coordinates. The next m lines describe the streets. Each street is represented in a single line by three integers ci, di, ki (1 <= ci, di <= n, ci ≠di, ki ∈ {1, 2}). Their meaning is that junctions ci and di are connected with a street. If ki = 1, then this is a unidirectional street from ci to di. Otherwise, the street can be driven in both directions. Each unordered pair {ci, di} can appear in the input at most once. You can assume that there is at least one junction on the western side of the island from which it is possible to reach some junction on the eastern side of the island. Additionally, in test cases worth at least 30 points, n,m <= 6 000.
第1行包含4个整数n, m, A, B(1≤n≤300000, 0≤m≤900000,1≤A,B≤10^9),分别表示格丁尼亚市中心的节点数,街道数和岛屿的尺寸。
接下来的n行,每行包含两个整数xi,yi (0≤xi≤A,0≤yi≤B),表示第i个节点的坐标。任意两个节点的坐标都不相同。
再往下的m行表示街道,每条街道用3个整数ci, di, ki(1≤ci, di≤n, ci≠di, ki∈{1,2}),表示节点ci、di有街道连接。如果ki=1,表示从ci到di的街道是单向的,否则,这条街道可以双向行驶。
每个无序对{ci, di}最多出现1次。
你可以假设西岸节点中至少有1个能够到达东岸的一些节点。
至少有30分的测试数据,n, m≤6000。
输出
Your program should write to the standard output one line for each junction on the western side of the island. This line should contain the number of junctions on the eastern side that are reachable from that junction. The output should be ordered according to decreasing y-coordinates of the junctions.
为每个西岸节点输出1行,包括从这个节点出发能够到达东岸的节点数目,
输出根据这些节点的y坐标降序排序。
solution
orz Jessie
首先这是一个平面图;
如果把不能到达的点排除掉,那么左边能到的右边点一定是连续的一段,右边到左边也是一样
于是我们把左边能到的右边点按y从小到大编号,
然后按编号从小到大一个个bfs,遇到有打过编号的点就返回(现在一定不如之前)
再从大到小做一次,即可得出每一个点能到的区间[l,r]
由于每个点最多被编号1次,所以稳得很
#include<cstdio>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#define maxn 300005
using namespace std;
int n,m,A,B,head[maxn],HEAD[maxn],l[maxn],r[maxn];
int t1,t2,ts,tt,t,tot,flag[maxn];
queue<int>q;
struct node{
int v,nex;
}E[1800005],e[1800005];
struct no{
int id,v,x,y;
}S[maxn],T[maxn],s[maxn];
bool cmp(no a,no b){return a.v<b.v;}
bool Cmp(no a,no b){return a.v>b.v;}
void lj(int t1,int t2){
E[++tot].v=t2;E[tot].nex=HEAD[t1];HEAD[t1]=tot;
e[tot].v=t1;e[tot].nex=head[t2];head[t2]=tot;
}
void add(int t1,int t2){
E[++tot].v=t2;E[tot].nex=HEAD[t1];HEAD[t1]=tot;
e[tot].v=t2;e[tot].nex=head[t1];head[t1]=tot;
E[++tot].v=t1;E[tot].nex=HEAD[t2];HEAD[t2]=tot;
e[tot].v=t1;e[tot].nex=head[t2];head[t2]=tot;
}
int main(){
cin>>n>>m>>A>>B;
for(int i=1;i<=n;i++){
scanf("%d%d",&s[i].x,&s[i].y);
if(s[i].x==0)S[++ts]=(no){i,s[i].y};
if(s[i].x==A)T[++tt]=(no){i,s[i].y};
}
for(int i=1;i<=m;i++){
scanf("%d%d%d",&t1,&t2,&t);
if(t==1)lj(t1,t2);
else add(t1,t2);
}
sort(T+1,T+tt+1,cmp);
for(int i=1;i<=ts;i++){
q.push(S[i].id);flag[S[i].id]=1;
}
while(!q.empty()){
int x=q.front();q.pop();
for(int i=HEAD[x];i;i=E[i].nex){
if(!flag[E[i].v]){flag[E[i].v]=1;q.push(E[i].v);}
}
}
int cnt=0;
for(int i=1;i<=tt;i++){
if(flag[T[i].id])T[i].x=++cnt;
}
for(int k=1;k<=tt;k++){
if(T[k].x>0){
q.push(T[k].id);
while(!q.empty()){
int x=q.front();q.pop();
for(int i=head[x];i;i=e[i].nex){
if(l[e[i].v]==0){l[e[i].v]=T[k].x;q.push(e[i].v);}
}
}
}
}
for(int k=tt;k>=1;k--){
if(T[k].x>0){
q.push(T[k].id);
while(!q.empty()){
int x=q.front();q.pop();
for(int i=head[x];i;i=e[i].nex){
if(r[e[i].v]==0){r[e[i].v]=T[k].x;q.push(e[i].v);}
}
}
}
}
sort(S+1,S+ts+1,Cmp);
for(int i=1;i<=ts;i++){
if(l[S[i].id]==0)puts("0");
else printf("%d\n",r[S[i].id]-l[S[i].id]+1);
}
return 0;
}
bzoj 2387: [Ceoi2011]Traffic的更多相关文章
- [Ceoi2011]Traffic
#2387. [Ceoi2011]Traffic Online Judge:Bzoj-2387,Luogu-4700 Label:Yy,Tarjan缩点,dfs 题目描述 格丁尼亚的中心位于Kacza ...
- Luogu4700 CEOI2011 Traffic Tarjan、搜索
传送门 题意:给出平面上$N$个点,它们一定在左下角为$(0,0)$,右上角为$(A,B)$的一个矩形内的整点上(包括边界),而且会给出$M$条呈直线的边,其中有有向边也有无向边,保证任意两条边不会在 ...
- bzoj AC倒序
Search GO 说明:输入题号直接进入相应题目,如需搜索含数字的题目,请在关键词前加单引号 Problem ID Title Source AC Submit Y 1000 A+B Problem ...
- 【BZOJ】【1018】【SHOI2008】堵塞的交通traffic
线段树 这题的线段树+分类讨论蛮神奇的……我以前学的线段树简直就是渣渣QAQ 看了下ydc题解里的思想>_>用线段树维护连通性!那么就自己写吧……每个节点表示一段区间的连通性(我的叶子节点 ...
- [BZOJ 1018] [SHOI2008] 堵塞的交通traffic 【线段树维护联通性】
题目链接:BZOJ - 1018 题目分析 这道题就说明了刷题少,比赛就容易跪..SDOI Round1 Day2 T3 就是与这道题类似的..然而我并没有做过这道题.. 这道题是线段树维护联通性的经 ...
- 数据结构(线段树):BZOJ 1018: [SHOI2008]堵塞的交通traffic
1018: [SHOI2008]堵塞的交通traffic Time Limit: 3 Sec Memory Limit: 162 MBSubmit: 2638 Solved: 864 Descri ...
- BZOJ 1018 [SHOI2008]堵塞的交通traffic
1018: [SHOI2008]堵塞的交通traffic Time Limit: 3 Sec Memory Limit: 162 MBSubmit: 2247 Solved: 706[Submit ...
- BZOJ 1018: [SHOI2008]堵塞的交通traffic [线段树 区间信息]
1018: [SHOI2008]堵塞的交通traffic Time Limit: 3 Sec Memory Limit: 162 MBSubmit: 3064 Solved: 1027[Submi ...
- bzoj千题计划108:bzoj1018: [SHOI2008]堵塞的交通traffic
http://www.lydsy.com/JudgeOnline/problem.php?id=1018 关键点在于只有两行 所以一个2*m矩形连通情况只有6种 编号即对应代码中的a数组 线段树维护 ...
随机推荐
- 通过cmd查看环境变量名对应的环境变量值
在VS环境中通常要添加路径,不过基本都是按照往上提供的方法添加变量名形如:$(VC_IncludePath),但是如何通过cmd命令找到真正的路径呢 未完待续……
- 借鉴一些关于js框架的东西
八款Js框架介绍及比较,Dojo .Scriptaculous .Prototype .yui-ext .Jquery .Mochikit.mootools .moo.fx,componentartu ...
- Struts2 In Action笔记_页面到动作的数据流入和流出
因为回答百度知道的一个问题,仔细查看了<Struts2 In Action>,深入细致的看了 “数据转移OGNL 和 构建视图-标签”,很多东西才恍然大悟. 一直觉得国外写的书很浮,不具有 ...
- AJAX Control Toolkit的AsynFileUpload控件资料收集
基于AJAX的文件上传显示进度条实现 http://plkong.iteye.com/blog/238159 asp.net ajax AjaxFileUpload使用 多文件上传 http://bl ...
- Bootstrap 警告框(Alert)插件
警告消息大多来是用来向终端用户提示警告或确认的消息,使用警告框插件,您可以向所有的警告框消息添加取消功能. 用法 您有以下两种方式启用警告框的可取消功能. 1.通过data属性:通过数据添加可取消功能 ...
- Linux MySQL 8.0 忘记密码
不小忘了MySQL的密码,按照书上和网上的内容都没能修改成功,终于在借鉴了多篇文章成功之后找到原因,修改密码成功 修改 MySQL 密码 第一步:关闭 MySQL 进程 systemctl stop ...
- linux硬件基础
1. 服务器分类 机架式服务器(主要用这个). 刀片式服务器. 塔式服务器. 2. 机架式服务器 服务器的尺: U - 2U. 服务器核心之电源: 双电源 AB 路. 服务器核心之 CPU-计算 CP ...
- hadoop: Shuffle过程详解 (转载)
原文地址:http://langyu.iteye.com/blog/992916 另一篇博文:http://www.cnblogs.com/gwgyk/p/3997849.html Shuffle过程 ...
- sql优化系列1
sql中索引是否会用到,进而影响查询效率. 带通配符(%)的like语句 1.不能用null作索引,任何包含null值的列都将不会被包含在索引中.即使索引有多列这样的情况下,只要这些列中有一列含有nu ...
- LOJ #6008. 「网络流 24 题」餐巾计划
#6008. 「网络流 24 题」餐巾计划 题目描述 一个餐厅在相继的 n nn 天里,每天需用的餐巾数不尽相同.假设第 i ii 天需要 ri r_iri 块餐巾.餐厅可以购买新的餐巾,每块餐 ...