AtCoder Grand Contest 002 D - Stamp Rally
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的更多相关文章
- AtCoder Grand Contest 002
AtCoder Grand Contest 002 A - Range Product 翻译 告诉你\(a,b\),求\(\prod_{i=a}^b i\)是正数还是负数还是零. 题解 什么鬼玩意. ...
- AtCoder Grand Contest 002 F:Leftmost Ball
题目传送门:https://agc002.contest.atcoder.jp/tasks/agc002_f 题目翻译 你有\(n*k\)个球,这些球一共有\(n\)种颜色,每种颜色有\(k\)个,然 ...
- Atcoder Grand Contest 002 F - Leftmost Ball(dp)
Atcoder 题面传送门 & 洛谷题面传送门 这道 Cu 的 AGC F 竟然被我自己想出来了!!!((( 首先考虑什么样的序列会被统计入答案.稍微手玩几组数据即可发现,一个颜色序列 \(c ...
- AtCoder Grand Contest 002 (AGC002) F - Leftmost Ball 动态规划 排列组合
原文链接https://www.cnblogs.com/zhouzhendong/p/AGC002F.html 题目传送门 - AGC002F 题意 给定 $n,k$ ,表示有 $n\times k$ ...
- 【想法题】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 ...
- [Atcoder Grand Contest 002] Tutorial
Link: AGC002 传送门 A: …… #include <bits/stdc++.h> using namespace std; int a,b; int main() { sca ...
- AtCoder Grand Contest 002题解
传送门 \(A\) 咕咕 int main(){ cin>>a>>b; if(b<0)puts(((b-a+1)&1)?"Negative": ...
- AtCoder Grand Contest 012
AtCoder Grand Contest 012 A - AtCoder Group Contest 翻译 有\(3n\)个人,每一个人有一个强大值(看我的假翻译),每三个人可以分成一组,一组的强大 ...
- AtCoder Grand Contest 011
AtCoder Grand Contest 011 upd:这篇咕了好久,前面几题是三周以前写的... AtCoder Grand Contest 011 A - Airport Bus 翻译 有\( ...
随机推荐
- C语言作业第二次总结
1.作业亮点 1.1作业整体概况 本次作业全体同学能够按时完成作业,且大部分同学的作业体现了自己的思路和方法,具备了一定变成能力. 1.2推荐博客 林岳-代码注释清晰,详细.->博文 王艺斌-算 ...
- 冲刺NO.10
Alpha冲刺第十天 站立式会议 项目进展 项目核心功能逐步构建完成,测试工作也已开始.主要对部分功能组合进行测试以测试系统可用性. 问题困难 项目的主要困难在这个时间点主要存在于测试工作中,测试工作 ...
- Django 测试驱动开发
第一章 1.编写functional_tests.py from selenium import webdriver browser = webdriver.Firefox() browser.get ...
- Java中RuntimeException和Exception的区别
[TOC] 1. 引入RuntimeException public class RuntimeException { public static void main(String[] args) { ...
- RESTful三问
我觉得学习一个技术,其实就是要弄明白三件事情:是什么(what),为什么(why),怎么用(how).正是所谓的三W方法. 所以打算总结一个"三问"系列.为了自己学习,也分享给别人 ...
- 14-TypeScript简单工厂模式
在TypeScript中,要调用功能,通常在调用方通过实例化被调用方对象来调用相关方法,但这种实现在调用方和被调用方形成了强耦合的关系. 另外如果被调用方有种实现,在调用方需要根据场景去实例化不同的类 ...
- 美团点餐—listview内部按钮点击事件
PS:长时间不写博客了,今天来写一下美团的这个点餐界面,今天先写一个加号减号的接口调用,下一篇是整体,有点菜,评价,商家,还有左边的listview和右边的展示项.进入这篇正题,像listview,G ...
- 20165226 2017-2018-4 《Java程序设计》第6周学习总结
20165226 2017-2018-4 <Java程序设计>第6周学习总结 教材学习内容总结 第八章 常用实用类 string类 并置 两个常量进行并置,得到的仍是常量. public ...
- Delphi Web开发连载 --ThinkDelphi (序)
如果把Delphi比作男人,那他曾经独步天下,笑傲江湖过: 如果把Delphi比作女子,那她曾经貌美如花,倾国倾城过! 但那只是历史,那只是曾经, 弹指一挥间,Delphi却似乎英雄迟暮,美人已老.. ...
- linux下执行java类(运行java定时器)
假如有一个定时器TimerTest.java import java.io.IOException; import java.util.Timer; public class TimerTest { ...