【刷题】BZOJ 5415 [Noi2018]归程

www.lydsy.com/JudgeOnline/upload/noi2018day1.pdf

Solution

考试的时候打的可持久化并查集,没调出来QAQ
后面知道了kruskal重构树这个东西,感觉好简单啊
这道题就建出kruskal重构树后,对于两个点找到它们的LCA,其子树min就是答案

#include<bits/stdc++.h>
#define ui unsigned int
#define ll long long
#define db double
#define ld long double
#define ull unsigned long long
const int MAXN=200000+10,MAXM=400000+10,inf=0x3f3f3f3f;
int T,n,m,e,beg[MAXN],nex[MAXM<<1],to[MAXM<<1],w[MAXM<<1],wt[MAXN<<1],Mn[MAXN<<1],cnt,d[MAXN],fa[MAXN<<1],Jie[20][MAXN<<1];
ll lastans;
struct node{
    int u,v,l,a;
    inline bool operator < (const node &A) const {
        return a>A.a;
    }
};
node side[MAXM];
struct cmp{
    inline bool operator () (int a,int b) const {
        return d[a]>d[b];
    }
};
std::priority_queue<int,std::vector<int>,cmp> q;
template<typename T> inline void read(T &x)
{
    T data=0,w=1;
    char ch=0;
    while(ch!='-'&&(ch<'0'||ch>'9'))ch=getchar();
    if(ch=='-')w=-1,ch=getchar();
    while(ch>='0'&&ch<='9')data=((T)data<<3)+((T)data<<1)+(ch^'0'),ch=getchar();
    x=data*w;
}
template<typename T> inline void write(T x,char ch='\0')
{
    if(x<0)putchar('-'),x=-x;
    if(x>9)write(x/10);
    putchar(x%10+'0');
    if(ch!='\0')putchar(ch);
}
template<typename T> inline void chkmin(T &x,T y){x=(y<x?y:x);}
template<typename T> inline void chkmax(T &x,T y){x=(y>x?y:x);}
template<typename T> inline T min(T x,T y){return x<y?x:y;}
template<typename T> inline T max(T x,T y){return x>y?x:y;}
inline void insert(int x,int y,int z)
{
    to[++e]=y;
    nex[e]=beg[x];
    beg[x]=e;
    w[e]=z;
}
inline void dijkstra()
{
    for(register int i=1;i<=n;++i)d[i]=inf;
    d[1]=0;
    q.push(1);
    while(!q.empty())
    {
        int x=q.top();
        q.pop();
        for(register int i=beg[x];i;i=nex[i])
            if(d[to[i]]>d[x]+w[i])d[to[i]]=d[x]+w[i],q.push(to[i]);
    }
}
inline int found(int x)
{
    if(x!=fa[x])fa[x]=found(fa[x]);
    return fa[x];
}
inline void init()
{
    dijkstra();
    cnt=n;
    std::sort(side+1,side+m+1);
    for(register int i=1;i<=n;++i)Mn[i]=d[i];
    for(register int i=1;i<=n+n-1;++i)fa[i]=i,wt[i]=inf;
    for(register int i=1,u,v;i<=m;++i)
    {
        u=found(side[i].u),v=found(side[i].v);
        if(u==v)continue;
        else
        {
            cnt++;
            Mn[cnt]=min(Mn[u],Mn[v]);
            wt[cnt]=side[i].a;
            fa[u]=fa[v]=cnt;
            Jie[0][u]=Jie[0][v]=cnt;
        }
    }
    for(register int j=1;j<=19;++j)
        for(register int i=1;i<=cnt;++i)Jie[j][i]=Jie[j-1][Jie[j-1][i]];
}
inline int Get(int x,int a)
{
    for(register int i=19;i>=0;--i)
        if(wt[Jie[i][x]]>a)x=Jie[i][x];
    return x;
}
int main()
{
    read(T);
    while(T--)
    {
        e=0;lastans=0;
        read(n);read(m);
        for(register int i=1;i<=n;++i)beg[i]=0;
        for(register int i=1,u,v,l,a;i<=m;++i)
        {
            read(u);read(v);read(l);read(a);
            side[i]=(node){u,v,l,a};
            insert(u,v,l);insert(v,u,l);
        }
        init();
        ll Q,K,S;
        read(Q);read(K);read(S);
        while(Q--)
        {
            ll v0,p0,v,p;read(v0);read(p0);
            v=(v0+1ll*K*lastans-1)%n+1;
            p=(p0+1ll*K*lastans)%(S+1);
            write(lastans=Mn[Get(v,p)],'\n');
        }
    }
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/hongyj/p/9417962.html