旅行

题目描述

悠悠岁月,不知不觉,距那传说中的 pppfish 晋级泡泡帝已是过去数十年。数十年中,这颗泡泡树上,也是
再度变得精彩,各种泡泡天才辈出,惊艳世人,然而,似乎不论后人如何的出彩,在他们的头顶之上,依然
是有着一道身影而立。
泡泡帝,pppfish。
现在,pppfish 即将带着被自己收服的无数个泡泡怪前往下一个空间,而在前往下一个空间的道路上,有N个
中转站,和M条空间虫洞连接中转站(双向通道,可有重边,可有环),然而,通过虫洞是要一定的条件
的,pppfish 将手下所有泡泡怪编号为
,对于每个空间虫洞,有两个值 和 ,表示此虫洞只
允许编号从 到 的泡泡怪通过,pppfish 现在在 号中转站,他想带尽可能多的泡泡怪到达
号中转
站,于是 pppfish 找到了机智的你,希望你告诉他最多可以带多少个泡泡怪,同时他还想知道所有泡泡怪的
编号(若有多组解取字典序最小的一组)

输入格式

第一行两个用空格隔开的整数N, M(2 ≤ N ≤ 1000, 0 ≤ M ≤ 3000)
接下来\(M\)行,每行四个用空格隔开的整数 \(a,b,l,r\)
表示在 \(a,b\) 中转站间有一个空间虫洞允许编号 \(l~r\) 的泡泡怪通过。

输出格式

第一行一个整数,表示最多能携带的泡泡怪数量。
接下来一行个用空格隔开的正整数,表示泡泡怪的编号,从小到大依次输出,如果没有泡泡怪能通过只要输出 0 就可以了

样例输入 #1

4 4
1 2 1 10
2 4 3 5
1 3 1 5
2 4 2 7

样例输出 #1

6
2 3 4 5 6 7

样例输入 #2

2 2
1 2 1 3
1 2 4 6

样例输出 #2

3
1 2 3

数据范围:

\(30\%\) 的数据 \(1 \leq N, M \leq 10\)
\(100\%\) 的数据 \(2 \leq N \leq 1000, 0 \leq M \leq 3000, 1 \leq a, b \leq N, 1 \leq l \leq r \leq 10^6\)

Code #2 虚树

Code #1 并查集:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

const int N=1005,M=3005,T=1e6+5;

int n,m;
int po[M<<1],tot;

struct node
{
    int u,v,l,r;
    bool operator < (const node &b)const{return r>b.r;}
}e[M];

int pa[N];

inline int find(int x){return x==pa[x]?x:pa[x]=find(pa[x]);}

int main()
{
    scanf("%d%d",&n,&m);

    for(int i=0;i<m;++i)
    {
        int u,v,l,r;
        scanf("%d%d%d%d",&u,&v,&l,&r);
        e[i]={u,v,l,r};
        po[++tot]=l,po[++tot]=r;
    }

    sort(po+1,po+tot+1);
    tot=unique(po+1,po+tot+1)-po-1;

    sort(e,e+m);

    int res=0,st=1e6;

    for(int i=1;i<=tot;++i)
    {
        for(int j=1;j<=n;++j)pa[j]=j;
        int x=po[i];
        for(int j=tot,k=0;j>=i;--j)
        {
            int y=po[j];
            for(;e[k].r>=y;++k)
                if(e[k].l<=x)pa[find(e[k].u)]=find(e[k].v);
            if(find(1)==find(n))
            {
                if(y-x+1>res || x<st)res=y-x+1,st=x;
                break;
            }
        }
    }

    printf("%d\n",res);
    for(int i=st;i<=st+res-1;++i)printf("%d ",i);

    return 0;
}
posted @ 2022-10-13 22:56  FighterQ  阅读(37)  评论(0)    收藏  举报