BZOJ 2594 [Wc2006]水管局长数据加强版 LCT

题意:链接

方法: LCT

解析:

搞了一个上午加1个小时的题,TM最后大错误居然是排序元素太多排不回原来的样子!

我要重新学排序!

这题是用LCT维护动态最小生成树,但是最小生成树上删边应该是做不到的,所以我们可以离线操作,之后先把所有该删的边删了然后倒着搞所有询问,这样删边就变成了加边,之后询问就是x到y路径上的最大边权。

图是动态的,所以想到LCT,但是LCT不能搞最大边权怎么办!

把每个边看做一个点。

假设这是第i个边,那么把他看做第i+n个点。

显然点权就是边权,然后将这个边连接的两个点分别与我们视作的点进行连边就OK了,也不难。

然后剩下的部分就是细节问题了。比如什么查找边之类的,也挺好写。

唯一的忠告就是!排序一定要写好cmp!

代码:

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define M 1000010
#define Q 100010
#define N 1100010
using namespace std;
int n,m,q;
int fffa[N];
int mx[N],ch[N][2],fa[N],rt[N],val[N],the_no_of_the_max[N];
int rev[N];
struct node
{
    int from,to,val,addtag,id;
}edge[M];
struct query
{
    int opt,from,to,ans,id; 
}que[Q];
void pushup(int x)
{
    if(!x)return;
    mx[x]=val[x];
    the_no_of_the_max[x]=x;
    if(ch[x][0]!=0&&mx[ch[x][0]]>mx[x])mx[x]=mx[ch[x][0]],the_no_of_the_max[x]=the_no_of_the_max[ch[x][0]];
    if(ch[x][1]!=0&&mx[ch[x][1]]>mx[x])mx[x]=mx[ch[x][1]],the_no_of_the_max[x]=the_no_of_the_max[ch[x][1]];
}
void reverse(int x)
{
    if(!x)return;
    swap(ch[x][0],ch[x][1]);
    rev[x]^=1;
}
void pushdown(int x)
{
    if(rev[x])
    {
        if(ch[x][0]!=0)
            reverse(ch[x][0]);
        if(ch[x][1]!=0)
            reverse(ch[x][1]);
        rev[x]=0;
    }
}
void down(int x)
{
    if(!x)return;
    if(!rt[x])down(fa[x]);
    pushdown(x);
}
void rotate(int x)
{
    if(!x)return;
    int y=fa[x],kind=ch[y][1]==x;
    ch[y][kind]=ch[x][!kind];
    fa[ch[y][kind]]=y;
    ch[x][!kind]=y;
    fa[x]=fa[y];
    fa[y]=x;
    if(rt[y])rt[y]=0,rt[x]=1;
    else ch[fa[x]][ch[fa[x]][1]==y]=x;
    pushup(y);
}
void splay(int x)
{
    if(!x)return;
    down(x);
    while(!rt[x])
    {
        int y=fa[x],z=fa[y];
        if(rt[y])rotate(x);
        else if((ch[y][1]==x)==(ch[z][1]==y))rotate(y),rotate(x);
        else rotate(x),rotate(x);
    }
    pushup(x);
}
void access(int x)
{
    if(!x)return;
    int y=0;
    while(x)
    {
        splay(x);
        rt[ch[x][1]]=1,rt[y]=0;
        ch[x][1]=y;
        pushup(x);
        y=x,x=fa[x];
    }
}
void movetoroot(int x)
{
    if(!x)return;
    access(x);
    splay(x);
    reverse(x);
}
void link(int x,int y)
{
    if(!x)return;
    movetoroot(x);
    fa[x]=y;
}
void cut(int x,int y)
{
    if(!x)return;
    movetoroot(x);
    access(y);
    splay(y);
    ch[y][0]=0;
    fa[x]=0;
    rt[x]=1;
    pushup(y);
}
int cmp1(node a,node b)
{
    if(a.val==b.val)
    {
        if(a.from==b.from)return a.to<b.to;
        return a.from<b.from;
    }
    return a.val<b.val;
}
int cmp2(node a,node b)
{
    if(a.from==b.from)return a.to<b.to;
    return a.from<b.from;
}
bool operator < (node a,node b)
{
    if(a.from==b.from)return a.to<b.to;
    return a.from<b.from;
}
int find(int x,int y)
{
    int ret;
    node tmp;
    tmp.from=x,tmp.to=y,tmp.val=0,tmp.addtag=0;
    ret=lower_bound(edge+1,edge+m+1,tmp)-edge;
    return ret;
}
int findfa(int x)
{
    if(fffa[x]!=x)return fffa[x]=findfa(fffa[x]);
    return x;
}
inline int Get_Int()
{
    int x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}
void init()
{
    for(int i=1;i<=n+m;i++)
    {
        fffa[i]=i;
    }
    memset(val,0,sizeof(val));
    for(int i=1;i<=n+m;i++)
    {
        rt[i]=1,the_no_of_the_max[i]=i;
    }
}
int main()
{
    n=Get_Int(),m=Get_Int(),q=Get_Int(); 
    init();
    for(int i=1;i<=m;i++)
    {
        edge[i].from=Get_Int(),edge[i].to=Get_Int(),edge[i].val=Get_Int();
        if(edge[i].from>edge[i].to)
        {
            swap(edge[i].from,edge[i].to);
        }
    }
    sort(edge+1,edge+1+m,cmp1);
    for(int i=1;i<=m;i++)
    {
        edge[i].id=i;
        val[i+n]=edge[i].val;
        mx[i+n]=edge[i].val;
        the_no_of_the_max[i+n]=i+n;
    }
    sort(edge+1,edge+1+m,cmp2);
    for(int i=1;i<=q;i++)
    {
        que[i].opt=Get_Int(),que[i].from=Get_Int(),que[i].to=Get_Int(); 
        if(que[i].from>que[i].to)
            swap(que[i].from,que[i].to);        
        if(que[i].opt==2)
        {
            int no=find(que[i].from,que[i].to);
            edge[no].addtag=1;
            que[i].id=edge[no].id;
        }
    }
    sort(edge+1,edge+1+m,cmp1);
    int the_number_of_the_edges_which_have_been_chosen=0;
    for(int i=1;i<=m;i++)
    {
        if(edge[i].addtag==1)continue;
        int x=edge[i].from,y=edge[i].to;
        int fx=findfa(x),fy=findfa(y);
        if(fx!=fy)
        {
            the_number_of_the_edges_which_have_been_chosen++;
            fffa[fx]=fy;
            link(x,i+n),link(i+n,y);
            if(the_number_of_the_edges_which_have_been_chosen==n-1)break;
        }
    }
    for(int i=q;i>=1;i--)
    {
        if(que[i].opt==1)
        {
            int x=que[i].from,y=que[i].to;
            movetoroot(x);
            access(y);
            splay(y);
            que[i].ans=mx[y];
        }else
        {
            int x=que[i].from,y=que[i].to;
            movetoroot(x);
            access(y);
            splay(y);
            int t=the_no_of_the_max[y]; 
            int tmpt=que[i].id;
            if(edge[tmpt].val<val[t])
            {
                cut(edge[t-n].from,t),cut(t,edge[t-n].to);
                link(x,tmpt+n),link(tmpt+n,y);
            } 
        }
    }
    for(int i=1;i<=q;i++)
    {
        if(que[i].opt==1)
            printf("%d\n",que[i].ans);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
BZOJ 2908 题目是一个数据下载任务。这个任务要求下载指定的数据文件,并统计文件中小于等于给定整数的数字个数。 为了完成这个任务,首先需要选择一个合适的网址来下载文件。我们可以使用一个网络爬虫库,如Python中的Requests库,来帮助我们完成文件下载的操作。 首先,我们需要使用Requests库中的get()方法来访问目标网址,并将目标文件下载到我们的本地计算机中。可以使用以下代码实现文件下载: ```python import requests url = '目标文件的网址' response = requests.get(url) with open('本地保存文件的路径', 'wb') as file: file.write(response.content) ``` 下载完成后,我们可以使用Python内置的open()函数打开已下载的文件,并按行读取文件内容。可以使用以下代码实现文件内容读取: ```python count = 0 with open('本地保存文件的路径', 'r') as file: for line in file: # 在这里实现对每一行数据的判断 # 如果小于等于给定整数,count 加 1 # 否则,不进行任何操作 ``` 在每一行的处理过程中,我们可以使用split()方法将一行数据分割成多个字符串,并使用int()函数将其转换为整数。然后,我们可以将该整数与给定整数进行比较,以判断是否小于等于给定整数。 最后,我们可以将统计结果打印出来,以满足题目的要求。 综上所述,以上是关于解决 BZOJ 2908 数据下载任务的简要步骤和代码实现。 希望对您有所帮助。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值