HDU3726 UVA1749 Graph and Queries

Bridge - 判断图是否连通 - 图的遍历 or 并查集搜索 or 强连通算法 You are given an undirected connected graph with N vertices and M edges that does not contain self-loops and double edges. The i-th edge (1≤i≤M) connects Vertex ai and Vertex bi. An edge whose removal disconnec 阅读详情

Graph and Queries


Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2264 Accepted Submission(s): 510


Problem Description
You are given an undirected graph with N vertexes and M edges. Every vertex in this graph has an integer value assigned to it at the beginning. You're also given a sequence of operations and you need to process them as requested. Here's a list of the possible operations that you might encounter:
1) Deletes an edge from the graph.
The format is [D X], where X is an integer from 1 to M, indicating the ID of the edge that you should delete. It is guaranteed that no edge will be deleted more than once.
2) Queries the weight of the vertex with K-th maximum value among all vertexes currently connected with vertex X (including X itself).
The format is [Q X K], where X is an integer from 1 to N, indicating the id of the vertex, and you may assume that K will always fit into a 32-bit signed integer. In case K is illegal, the value for that query will be considered as undefined, and you should return 0 as the answer to that query.
3) Changes the weight of a vertex.
The format is [C X V], where X is an integer from 1 to N, and V is an integer within the range [-106, 106].

The operations end with one single character, E, which indicates that the current case has ended.
For simplicity, you only need to output one real number - the average answer of all queries.



Input
There are multiple test cases in the input file. Each case starts with two integers N and M (1 <= N <= 2 * 104, 0 <= M <= 6 * 104), the number of vertexes in the graph. The next N lines describes the initial weight of each vertex (-106 <= weight[i] <= 106). The next part of each test case describes the edges in the graph at the beginning. Vertexes are numbered from 1 to N. The last part of each test case describes the operations to be performed on the graph. It is guaranteed that the number of query operations [Q X K] in each case will be in the range [1, 2 * 105], and there will be no more than 2 * 105 operations that change the values of the vertexes [C X V].

There will be a blank line between two successive cases. A case with N = 0, M = 0 indicates the end of the input file and this case should not be processed by your program.



Output
For each test case, output one real number – the average answer of all queries, in the format as indicated in the sample output. Please note that the result is rounded to six decimal places.


Sample Input
3 3
10
20
30
1 2
2 3
1 3
D 3
Q 1 2
Q 2 1
D 2
Q 3 2
C 1 50
Q 1 1
E

3 3
10
20
20
1 2
2 3
1 3
Q 1 1
Q 1 2
Q 1 3
E

0 0


Sample Output
Case 1: 25.000000
Case 2: 16.666667

Hint
For the first sample:
D 3 -- deletes the 3rd edge in the graph (the remaining edges are (1, 2) and (2, 3))
Q 1 2 -- finds the vertex with the second largest value among all vertexes connected with 1. The answer is 20.
Q 2 1 -- finds the vertex with the largest value among all vertexes connected with 2. The answer is 30.
D 2 -- deletes the 2nd edge in the graph (the only edge left after this operation is (1, 2))
Q 3 2 -- finds the vertex with the second largest value among all vertexes connected with 3. The answer is 0 (Undefined).
C 1 50 -- changes the value of vertex 1 to 50.
Q 1 1 -- finds the vertex with the largest value among all vertex connected with 1. The answer is 50.
E -- This is the end of the current test case. Four queries have been evaluated, and the answer to this case is (20 + 30 + 0 + 50) / 4 = 25.000.

For the second sample, caution about the vertex with same weight:
Q 1 1 – the answer is 20
Q 1 2 – the answer is 20
Q 1 3 – the answer is 10




Source
2010 Asia Tianjin Regional Contest


第一次写treap。。。感觉和伸展树差不多,就是操作少了点。。。当做伸展树入门之前的一点准备。。。。。

这题是treap+离线处理+并查集。。。就是先把所有命令都存起来然后从后往前处理。。。因为要求删边。。离线处理过后就变成的加边,每一个连通分量里都有一棵树,加边的时候当两个点在一个连通分量里就不用做什么了,不在同一个连通分量里就要将这两个树合并。。注意要结点数少的合到结点数大的里面去。。。然后查询第k大就在一个连通分量里查询就可以了(第1大就是最右边的值,第二大就是右边从左边数第二个)。。。

注意 在remove操作了重复delete结点在HDU上交G++可能会WA。。在UVA上RE。。。

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#define ll long long
using namespace std;
const int MAXC=500010;
const int MAXN=20010;
const int MAXM=60010;
struct Node
{
    Node *ch[2];
    int r;
    int s;
    int v;
    Node(int v):v(v){ch[0]=ch[1]=NULL,r=rand(),s=1;}
    bool operator <(const Node& rhs) const
    {
        return r<rhs.r;
    }
    int cmp(int x)
    {
        if(x==v)
            return -1;
        return x<v?0:1;
    }
    void maintain()
    {
        s=1;
        if(ch[0]!=NULL)
            s+=ch[0]->s;
        if(ch[1]!=NULL)
            s+=ch[1]->s;
    }
};
struct Command
{
    char type;
    int x,p;
}commands[MAXC];
void rotate(Node* &root,int d)
{
    Node *k=root->ch[d^1];root->ch[d^1]=k->ch[d];k->ch[d]=root;
    root->maintain();k->maintain();root=k;
}
void insert(Node* &root,int x)
{
    if(root==NULL)
    {
        root=new Node(x);
    }
    else
    {
        int d=(x<root->v)?0:1;
        insert(root->ch[d],x);
        if(root->ch[d]->r>root->r)
            rotate(root,d^1);
    }
    root->maintain();
}
void remove(Node* &root,int x)
{
    int d=root->cmp(x);
    if(d==-1)
    {
        Node *u=root;
        if(root->ch[0]==NULL)
        {
            root=root->ch[1];
            delete u;
        }
        else if(root->ch[1]==NULL)
        {
            root=root->ch[0];
            delete u;
        }
        else
        {
            int d2=(root->ch[0]->r>root->ch[1]->r)?1:0;
            rotate(root,d2);
            remove(root->ch[d2],x);
        }
    }
    else
        remove(root->ch[d],x);
    if(root!=NULL)
        root->maintain();
}
int n,m,weight[MAXN],from[MAXM],to[MAXM],removed[MAXM];
int bin[MAXN];
Node *root[MAXN];
int find(int x)
{
    return bin[x]==x?x:bin[x]=find(bin[x]);
}
int kth(Node *root,int k)
{
    if(root==NULL||k<=0||k>root->s)
        return 0;
    int s=(root->ch[1]==NULL?0:root->ch[1]->s);
    if(k==s+1)
        return root->v;
    else if(k<=s)
        return kth(root->ch[1],k);
    else
        return kth(root->ch[0],k-s-1);
}
void merge(Node* &scr,Node* &dest)
{
    if(scr->ch[0]!=NULL)
        merge(scr->ch[0],dest);
    if(scr->ch[1]!=NULL)
        merge(scr->ch[1],dest);
    insert(dest,scr->v);
    delete scr;
    scr=NULL;
}
void removetree(Node* &x)
{
    if(x->ch[0]!=NULL)
        removetree(x->ch[0]);
    if(x->ch[1]!=NULL)
        removetree(x->ch[1]);
    delete x;
    x=NULL;
}
void add_edge(int x)
{
    int u=find(from[x]),v=find(to[x]);
    if(u!=v)
    {
        if(root[u]->s<root[v]->s)
        {
            bin[u]=v;
            merge(root[u],root[v]);
        }
        else
        {
            bin[v]=u;
            merge(root[v],root[u]);
        }
    }
}
int query_cnt;
ll query_sum;
void query(int x,int k)
{
    query_cnt++;
    query_sum+=kth(root[find(x)],k);
}
void change_weight(int x,int v)
{
    int u=find(x);
    remove(root[u],weight[x]);
    insert(root[u],v);
    weight[x]=v;
}
int main()
{
    int n,m,i,flag=1;
    while(scanf("%d%d",&n,&m)==2&&(n||m))
    {
        for(i=1;i<=n;i++)
            scanf("%d",&weight[i]);
        for(i=1;i<=m;i++)
            scanf("%d%d",&from[i],&to[i]);
        memset(removed,0,sizeof(removed));
        int c=0;
        while(1)
        {
            char type;
            int x,p=0,v=0;
            scanf(" %c",&type);
            if(type=='E')
                break;
            scanf("%d",&x);
            if(type=='D')
                removed[x]=1;
            if(type=='Q')
                scanf("%d",&p);
            if(type=='C')
            {
                scanf("%d",&v);
                p=weight[x];
                weight[x]=v;
            }
            commands[c++]=(Command){type,x,p};
        }
        for(i=1;i<=n;i++)
        {
            bin[i]=i;
            if(root[i]!=NULL)
                removetree(root[i]);
            root[i]=new Node(weight[i]);
        }
        for(i=1;i<=m;i++)
            if(!removed[i])
            add_edge(i);
        query_cnt=query_sum=0;
        for(i=c-1;i>=0;i--)
        {
            if(commands[i].type=='D')
                add_edge(commands[i].x);
            if(commands[i].type=='Q')
                query(commands[i].x,commands[i].p);
            if(commands[i].type=='C')
                change_weight(commands[i].x,commands[i].p);
        }
        printf("Case %d: %.6f\n",flag++,query_sum/(query_cnt*1.0));
    }
    return 0;
}


HDU 3726 Graph and Queries(离线 + Treap) Graph and Queries Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 5338    Accepted Submission(s): 1131   Problem Description You are given ... 阅读详情

相关推荐

【STM32】IEC60730 Class B软件安全实现 Part2:自检库实战指南

本文是STM32 IEC60730 Class B软件安全实现的实战指南,详细介绍了如何将X-CUBE-CLASSB自检库集成到用户项目中。内容涵盖软件包解构、内存划分、启动与运行时自检的集成步骤、关键代码示例及调试技巧,并阐述了为最终通过安全认证所需的文档与流程准备,帮助开发者高效实现MCU功能安全。

weixin_29185167的博客 91

uva1479 - Graph and Queries Treap名次树

You are given an undirected graph with N vertexes and M edges. Every vertex in this graph has an integer value assigned to it at the beginning. You're also given a sequence of operations and you need

564

Apk脱壳圣战之---脱掉“梆梆加固”的保护壳

一、前言 现如今Android用户的安全意识不是很强,又有一些恶意开发者利用应用的名字吸引眼球,包装一个恶意锁机收费的应用,在用户被骗的安装应用之后,立马手机锁机,需要付费方可解锁。这样的恶意软件是非常让人痛恨的。所以本文就用一个案例来分析如何破解这类应用,获取解锁密码,让被骗的用户可以找回爽快! 二、分析软件锁机原理 本文用的是一款叫做:安卓性能激活.apk,关于样本apk文件后面会...

尼古拉斯.赵四的博客 5060

LA 5031 - Graph and Queries

You are given an undirected graph with N vertexes and M edges. Every vertex in this graph has an integer value assigned to it at the beginning. You're also given a sequence of operations and you nee

GAUSS_CLB 882

AD17入门简单教程(一)

AD17入门简单教程 我写这篇文章主要是因为最近刚学习完了AD17,想做一下总结,也希望通过自己心得的帮助一些刚入门AD17的小白。 总结一共分为两大部分,包括AD17的下载安装与汉化(文末有详细链接)、工程的创建、元器件库、PCB、走线中的常用命令介绍,文末还附有完整的PCB工程以及讲解视频。 第一部分工程的创建及元器件库常用命令 1.首先在桌面上创建一个文件夹,然后在AD17点击左上角的文件中...

qq_45433195的博客 2万+

MRI脑影像分析从哲学到技术:一文搞懂VBM预处理基本原理(全网最详细解析)

   分析大脑解剖学差异最常用方法之一是基于体素的形态学方法(voxel-based morphometry, VBM)。 VBM的基本思想是测量大脑组织,尤其是灰质的局部浓度的差异。在大脑的每个体素处进行操作,然后在两个或多个实验组之间进行统计比较,从而在受调查的组之间建立特定大脑区域中脑组织浓度的统计学显着差异。 VBM分析基于(高分辨率)MRI脑部扫描,涉及大量处理步骤,主要是空间归一化,分割,平滑和统计分析,最终结果是统计图,显示了各组之间组织类型明显不同的区域[^1]。 参考 [^1] ..

意疏的学习笔记 3万+

hdu3726 Graph and Queries

http://www.elijahqi.win/2018/02/25/hdu3726/ Problem Description You are given an undirected graph with N vertexes and M edges. Every vertex in this graph has an integer value assigned to it at the ...

Elijahqi 351

智驾芯片&软件分层&测试

智能驾驶相关的芯片,软件分层,测试相关内容

天赐好车的博客 436

HDU 3726 Graph and Queries

Description You are given an undirected graph with N vertexes and M edges. Every vertex in this graph has an integer value assigned to it at the beginning. You're also given a sequence of operation

Occult 1210

面向元数据的 SAP UI 中枢:namespace sap.ui.mdc 全景解析

如果把应用前端比作一条数据到交互的装配线,sap.ui.mdc 就是那条线的标准化工装:以 PropertyInfo 描述原料,以 Delegate 定义装配方法,以 p13n 与 VariantManagement 交付面向个体的最终产物;而 Condition Model、Operator 与 ValueHelp 则让输入与筛选的语义落地。与 OData V4 服务与 Fiori Elements 宏构件组合使用时,它能在保持一致体验的同时,给自由式开发者留下足够的可编排空间。

2007 年 ~ 2025 年,深耕 SAP 技术 18 年 238

高质量文献综述原来这么写!借助Gemini 3 Pro+四步法,手把手教你写出有逻辑深度的综述

总结与展望先总结现状,说明前人已经做到什么地步了,再指出缺口,前人没解决什么问题、方法有什么局限等等,最后再在这个缺口的基础上,提出你的未来研究方向。当你有个复杂的研究题目,但没办法拆解出关键词,就可以让Gemini 3 Pro来辅助帮你拆解,并帮你写专业的检索式,若是找英文文献,还能帮你将中文关键词翻译成地道的专业术语。做文献笔记,可以建立文献笔记矩阵,可以用Excel建个表格,横向是文献,纵向是关键要素,包括作者/年份、研究问题/假设、理论框架/模型、研究方法/数据来源、核心结论、局限性/Gap。

智能AI让学术科研更容易 992

LA 5031 Graph and Queries (【名次树(treap)】+【并查集】+【离线算法】)

题目链接:https://cn.vjudge.net/problem/UVALive-5031You are given an undirected graph with N vertexes and M edges. Every vertex in this graph has an integer value assigned to it at the beginning. You’re al

linlinsong—ACM界蒟水! 510

FastGPT智能体开发:FastGPT OpenAPI 知识库接口

FastGPT OpenAPI 知识库接口

王国平的博客 323

C - Colorful Graph CodeForces - 246D (暴力坑题)

Colorful Graph CodeForces - 246D You’ve got an undirected graph, consisting of n vertices and m edges. We will consider the graph’s vertices numbered with integers from 1 to n. Each vertex of the gra

月亮船 1518

数学建模模型总结

这是本人在两年内准备参加数学建模竞赛搜集到的模型资料,已经经过了本人的筛选和整理,希望能够对准备参加数学建模竞赛的学弟学妹们提供一些帮助!

精雕软件(JDpaint) 5.5 绿色版

精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版

Graph and Cycles【SEER 2019】【思维】

2019-2020 ICPC Southeastern European Regional Programming Contest (SEERC 2019) J 不知道说什么,只能说想了大概有三个小时吧,昨晚前两个签到题,然后开了这个第三题,一直想不到,想了个将点看成边,边看成点的做法,然后去跑最小生成树,但是最后值会偏小,因为还有几条边没有放进来,而且还是会RE以及会TLE的,所以,还是不...

既然弱小,就只顾变强就是了 715

IEM模型,地表粗糙度模拟

利用微波反演土壤水分时候,利用IEM模型可以实现对随机地表的模拟

codeforces 246 D. Colorful Graph (set)

D. Colorful Graph You've got an undirected graph, consisting of n vertices and m edges. We will consider the graph's vertices numbered with integers from 1 to n. Each vertex of the graph has

R 1037

文档:亲测可用解决vnc灰屏,黑屏,鼠标大黑叉情况

主要解决vnc-viewer灰屏,黑屏,鼠标大黑叉等现象,详细见文档。

splay<题目续集>

HDU 3436 Queue-jumpers Time Limit: 2000/1000 MS (Java/Others)Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 4506Accepted Submission(s): 1232 Problem Description Ponyo and Ga...

weixin_30328063的博客 470

HDU 3726 Graph and Queries(treap)

treap

LJ__ 454

UVA 1479 Graph and Queries(Treap:名次树+并查集)

UVA 1479 Graph and Queries(Treap:名次树+并查集) 题意:给你一个无向图,每个顶点具有权值,现在对图进行操作,你需要输出最后查询操作的平均值.有查询,删除边,改变点的权值三种操作. 分析:刘汝佳训练指南P234例题.        首先读入所有的操作,点以及边.由于只需要我们输出最后的一个平均结果,所以我们把这些操作逆序处理,使得从图中删除边,变成往图中添加边

code 1673

hdu3726:Graph and Queries(treap+启发式合并+离线)

题目传送门:http://acm.hdu.edu.cn/showproblem.php?pid=3726 题目大意:给出一幅无向边构成的图,每个点有一个点权。有3种操作:删掉一条边,更改一个点的点权,以及查询一个点所在的连通块的第k大点权。 分析:这里我们可以通过对每一个连通块维护一棵treap在log(n)的时间内完成操作2,3,至于操作1,我们发现分离比较难办,于是我们考虑离线读入,然后从

KsCla 1163

HDU 3726 题解

有一个图,三种操作。 D x:删除第x条边。 Q x k:查询x所在的联通分量中,第k大的权值。 C x v:把第x个点的权值改成v。

ez_yww的博客 470

hdu 3726 Graph and Queries , 天津 2010, LA 5031,并查集,Treap,离线处理

Treap树入门练习题目,此题主要来练习Treap的操作 此题也是刘汝佳 大白书,Treap的例题 用指针实现的Treap的操作 注意: 1.不要访问NULL 2.cmp函数的使用,相等时返回-1 其它,待补充。。。 另: 为避免错误和是操作的简洁,可使用一个真实的空指针null代替空指针NULL,。。。 关于内存预申请的优化写法,待补充 //#pragma comment(l

冰 1858

hdu3726——treap,并查集

题意:给定一个无向图,每个节点都赋予一个权值,给出三种操作: 删除一条边。询问节点x所在连通分量的第k大的权值。修改节点x的权值为k。 输出所有询问的平均值。 可以想到为每一个连通分量建一颗treap,修改操作很好实现,先删后加就行。但是对于删除操作,如果把一个连通分量分成了两个,那就要先把分出去的全删了,再建一棵treap,时间复杂度和代码复杂度都很操蛋的。我们可以反过来想:先

cool_Fires的专栏 1078
上一篇: HDU1044 Collect More Jewels
下一篇: UVA11922 Permutation Transformer
eeeaaaaa
博客等级 码龄12年 2粉丝 155原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值