Description

We have an undirected graph with N vertices and M edges. The vertices are numbered 1 through N, and the edges are numbered 1 through M. Edge i connects vertices ai and bi. The graph is connected.

On this graph, Q pairs of brothers are participating in an activity called Stamp Rally. The Stamp Rally for the i-th pair will be as follows:

One brother starts from vertex xi, and the other starts from vertex yi.

The two explore the graph along the edges to visit zi vertices in total, including the starting vertices. Here, a vertex is counted only once, even if it is visited multiple times, or visited by both brothers.

The score is defined as the largest index of the edges traversed by either of them. Their objective is to minimize this value.

Find the minimum possible score for each pair.

Solution

这题一个直接的思路就是二分答案,加边之后判连通块大小

由于有 \(Q\) 组询问,考虑整体二分

注意到这一题中不仅 \([L,mid]\) 之间的边有贡献, \([1,mid]\) 之间的边也有贡献

如果 \(dfs\) 处理的话,复杂度就不对了

考虑 \(bfs\) 序整体二分:

我们每一次都需要把 \([1,mid]\) 之间的边加入,而对于整体二分中同层的节点,\(mid\) 是单调的

所以维护一个单调指针对于每一层扫一遍即可,每当进入新的层之后我们就把指针变成 \(0\),并清空并查集

因为只有 \(log\) 层,所以只会清空 \(log\) 次,所以总复杂度就是 \(O(n*logn)\)

#include<bits/stdc++.h>
using namespace std;
const int N=100010;
int n,m,Q,fa[N],sz[N],ans[N];
struct node{int x,y,z,id;}e[N];
struct sub{int l,r;vector<node>S;};
queue<sub>q;
inline void Clear(){for(int i=1;i<=n;i++)fa[i]=i,sz[i]=1;}
inline int find(int x){return fa[x]==x?x:fa[x]=find(fa[x]);}
inline void merge(int x,int y){
if(find(x)==find(y))return ;
x=find(x);y=find(y);
fa[y]=x;sz[x]+=sz[y];
}
inline int getval(int x,int y){
x=find(x);y=find(y);
int ret=sz[x]+sz[y];
if(x==y)ret>>=1;
return ret;
}
void solve(){
int p=0;
while(!q.empty()){
sub s=q.front(),L,R;q.pop();
if(s.l==s.r){
for(int i=s.S.size()-1;i>=0;i--)ans[s.S[i].id]=s.l;
continue;
}
int mid=(s.l+s.r)>>1;
if(p>mid)p=0,Clear();
while(p<mid)p++,merge(e[p].x,e[p].y);
for(int i=s.S.size()-1;i>=0;i--){
node t=s.S[i];
if(getval(t.x,t.y)<t.z)R.S.push_back(t);
else L.S.push_back(t);
}
L.l=s.l;L.r=mid;R.l=mid+1;R.r=s.r;
q.push(L);q.push(R);
}
}
int main(){
freopen("pp.in","r",stdin);
freopen("pp.out","w",stdout);
scanf("%d%d",&n,&m);
sub s;node t;
for(int i=1;i<=m;i++)scanf("%d%d",&e[i].x,&e[i].y);
scanf("%d",&Q);
for(int i=1;i<=Q;i++){
scanf("%d%d%d",&t.x,&t.y,&t.z);
t.id=i;s.S.push_back(t);
}
s.l=1;s.r=m;q.push(s);Clear();
solve();
for(int i=1;i<=Q;i++)printf("%d\n",ans[i]);
return 0;
}

AtCoder Grand Contest 002 D - Stamp Rally的更多相关文章

  1. AtCoder Grand Contest 002

    AtCoder Grand Contest 002 A - Range Product 翻译 告诉你\(a,b\),求\(\prod_{i=a}^b i\)是正数还是负数还是零. 题解 什么鬼玩意. ...

  2. AtCoder Grand Contest 002 F:Leftmost Ball

    题目传送门:https://agc002.contest.atcoder.jp/tasks/agc002_f 题目翻译 你有\(n*k\)个球,这些球一共有\(n\)种颜色,每种颜色有\(k\)个,然 ...

  3. Atcoder Grand Contest 002 F - Leftmost Ball(dp)

    Atcoder 题面传送门 & 洛谷题面传送门 这道 Cu 的 AGC F 竟然被我自己想出来了!!!((( 首先考虑什么样的序列会被统计入答案.稍微手玩几组数据即可发现,一个颜色序列 \(c ...

  4. AtCoder Grand Contest 002 (AGC002) F - Leftmost Ball 动态规划 排列组合

    原文链接https://www.cnblogs.com/zhouzhendong/p/AGC002F.html 题目传送门 - AGC002F 题意 给定 $n,k$ ,表示有 $n\times k$ ...

  5. 【想法题】Knot Puzzle @AtCoder Grand Contest 002 C/upcexam5583

    时间限制: 2 Sec 内存限制: 256 MB 题目描述 We have N pieces of ropes, numbered 1 through N. The length of piece i ...

  6. [Atcoder Grand Contest 002] Tutorial

    Link: AGC002 传送门 A: …… #include <bits/stdc++.h> using namespace std; int a,b; int main() { sca ...

  7. AtCoder Grand Contest 002题解

    传送门 \(A\) 咕咕 int main(){ cin>>a>>b; if(b<0)puts(((b-a+1)&1)?"Negative": ...

  8. AtCoder Grand Contest 012

    AtCoder Grand Contest 012 A - AtCoder Group Contest 翻译 有\(3n\)个人,每一个人有一个强大值(看我的假翻译),每三个人可以分成一组,一组的强大 ...

  9. AtCoder Grand Contest 011

    AtCoder Grand Contest 011 upd:这篇咕了好久,前面几题是三周以前写的... AtCoder Grand Contest 011 A - Airport Bus 翻译 有\( ...

随机推荐

  1. Beta冲刺合集

    Beta冲刺序列: Beta凡事预则立 :Beta No.0 Beta冲刺Day1:Beta No.1 Beta冲刺Day2:Beta No.2 Beta冲刺Day3:Beta No.3 Beta冲刺 ...

  2. beat冲刺计划安排

    1. 团队成员 组长:郭晓迪 组员:钟平辉 柳政宇 徐航 曾瑞 2. 主要计划安排如下: 3. 详细日程任务安排

  3. 《Language Implementation Patterns》之 数据聚合符号表

    本章学习一种新的作用域,叫做数据聚合作用域(data aggregate scope),和其他作用域一样包含符号,并在scope tree里面占据一个位置. 区别在于:作用域之外的代码能够通过一种特殊 ...

  4. java利用iTextWorker生成pdf

    使用itext生成pdf,在linux环境下,中文全部失踪,因为itext要在linux下支持中文字体需要引入itext-asian, 并添加一个字体类. public static class Pd ...

  5. DML数据操作语言之常用函数

    所谓函数,就是输入某一值,得到相应的输出结果的功能.相当于一个加工厂,给了原料,最终产出成品. 其中原料 就是参数(parameter). 产品 就是返回值. 函数大致可以分为以下五个种类: 算术函数 ...

  6. 2017 清北济南考前刷题Day 4 afternoon

    期望得分:30+50+30=110 实际得分:40+0+0=40 并查集合并再次写炸... 模拟更相减损术的过程 更相减损术,差一定比被减数小,当被减数=减数时,停止 对于同一个减数来说,会被减 第1 ...

  7. Nginx动静分离架构

    Nginx动静分离简单来说就将动态与静态资源分开,不能理解成只是单纯的把动态页面和静态页面物理分离,严格意义上说应该是动态请求跟静态请求分开,可以理解成使用Nginx处理静态页面,Tomcat,Res ...

  8. Jenkins中展示HTML测试报告

    背景:测试报告是用reportNG生成的,属于java自动化测试项目. 1)         安装插件 首先要安装HTML Publisher plugin,这个在插件管理里面搜索并安装即可,如下我已 ...

  9. Python内置函数(35)——next

    英文文档: next(iterator[, default]) Retrieve the next item from the iterator by calling its __next__() m ...

  10. maven常见问题处理(3-2)maven打包时跳过测试的几个方法

    运行mvn install时跳过Test方法一:<project> [...] <build> <plugins> <plugin> <group ...