CEOI 2011Traffic

首先这个题它给定我们的是一个平面图。(除端点外,没有相交的边)

那么我们可以知道对于: 

每一个在左侧的点 能够到达的 右侧的点一定是一段区间(对应的是y值)。

因为如果区间中有一个点它无法到达,那么其他点必然也无法到那个点,否则会相交。

这样的话我们可以先从左边BFS一遍,如果右边有无法到的点,就不管了。

然后我们考虑怎么求这一段区间。

我们发现可以只需要维护区间的上界和下界就好了。

那么我们把右侧的点按y降序排列,从上往下依次BFS。

每个点第一次被经过时,此时对应的右侧的点,一定是其上界。

同理,从下往上BFS,也可以得到下界。

值得注意的是,我没有额外把那些不符合条件的点去掉,而是把它对于区间的贡献看成0,做了个前缀和。

#include <string>
#include <cstdio>
#include <queue>
#include <cstring>
#include <algorithm>
#define RG register
using namespace std;

inline int gi () {
    int x=0,w=0; char ch=0;
    while (ch<'0' || ch>'9') {if (ch=='-') w=1; ch=getchar ();}
    while (ch>='0' && ch<='9') x=(x<<3)+(x<<1)+(ch^48), ch=getchar ();
    return w?-x:x;
}

const int N=3e5+10;
const int M=9e5+10;

queue <int> q;
int xx[M],yy[M],typ[M];
int tot,head[N],next[M<<1],to[M<<1];
int n,m,A,B,Wcnt,Ecnt,Minx[N],Maxx[N],pre[N],vis[N],arr[N];

struct Dot {int id,y;} Wes[N],Eas[N];
inline bool CMPmax (Dot a, Dot b) {return a.y>b.y;}

inline void make (int from, int To) {       
    next[++tot]=head[from];
    head[from]=tot; to[tot]=To;
}

inline void Clear_Graph () {
    tot=0;
    memset (to, 0, sizeof (to));
    memset (next, 0, sizeof (next));
    memset (head, 0, sizeof (head));
}

inline void BFS_TO () {
    RG int i,x,y;
    for (i=1;i<=Wcnt;++i) arr[Wes[i].id]=1, q.push (Wes[i].id);
    while (!q.empty ()) {
        x=q.front (), q.pop ();
        for (i=head[x];i;i=next[i])
            if (!arr[y=to[i]]) arr[y]=1, q.push (y);
    }
}

inline void BFS_uplim (int x) {
    RG int i,Nx,y;
    q.push (Eas[x].id), vis[Eas[x].id]=1;
    while (!q.empty ()) {
        Nx=q.front (), q.pop ();
        for (i=head[Nx];i;i=next[i])
            if (!vis[y=to[i]]) vis[y]=1, Minx[y]=x, q.push (y);
    }
}

inline void BFS_downlim (int x) {
    RG int i,Nx,y;
    q.push (Eas[x].id), vis[Eas[x].id]=1;
    while (!q.empty ()) {
        Nx=q.front (), q.pop ();
        for (i=head[Nx];i;i=next[i])
            if (!vis[y=to[i]]) vis[y]=1, Maxx[y]=x, q.push (y);
    }
}

int main ()
{
    RG int i,x,y;
    n=gi (), m=gi (), A=gi (), B=gi ();
    for (i=1;i<=n;++i) {
        x=gi (), y=gi ();
        if (x==0) Wes[++Wcnt].y=y, Wes[Wcnt].id=i;
        if (x==A) Eas[++Ecnt].y=y, Eas[Ecnt].id=i;
    }
    for (i=1;i<=m;++i) {
        xx[i]=gi (), yy[i]=gi (), typ[i]=gi ();
        make (xx[i], yy[i]);
        if (typ[i]==2) make (yy[i], xx[i]);
    }
    BFS_TO (); Clear_Graph ();
    for (i=1;i<=m;++i) {
        make (yy[i], xx[i]);
        if (typ[i]==2) make (xx[i], yy[i]);
    }
    sort (Wes+1, Wes+Wcnt+1, CMPmax);
    sort (Eas+1, Eas+Ecnt+1, CMPmax);
    for (i=1;i<=Ecnt;++i)
        pre[i]=arr[Eas[i].id]?pre[i-1]+1:pre[i-1];
    //for (i=1;i<=Ecnt;++i) printf ("%d %d
", pre[i], Eas[i].id); 
    for (i=1;i<=Ecnt;++i)
        if (arr[Eas[i].id]) BFS_uplim (i);
    //for (i=1;i<=n;++i) printf ("%d ", Minx[i]); puts ("");
    memset (vis, 0, sizeof (vis));
    for (i=Ecnt;i>=1;--i)
        if (arr[Eas[i].id]) BFS_downlim (i);
//    for (i=1;i<=n;++i) printf ("%d ", Maxx[i]); puts ("");
    for (i=1;i<=Wcnt;++i) printf ("%d
", pre[Maxx[Wes[i].id]]-pre[Minx[Wes[i].id]-1]);
    return 0;
}
原文地址:https://www.cnblogs.com/Bhllx/p/9863773.html