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 翻译 有\( ...
随机推荐
- 利用PCA降维
参考:<机器学习实战>- Machine Learning in Action 一. 基本思想 PCA(Principal Component Analysis),主成分分析.是目前应用 ...
- Scrum 冲刺 第七日
Scrum 冲刺 第七日 站立式会议 燃尽图 今日任务安排 项目发布说明 站立式会议 返回目录 燃尽图 返回目录 今日任务安排 返回目录 项目发布说明 本版本的新功能 不只是简单打地鼠,还有一些不能打 ...
- 同样是IT培训,为什么人家月薪过万,你才几千,问题在哪?!
听过一句话"360行,行行转IT",虽然有些夸张,但也不难看出IT行业的火爆程度.从李总理提的"互联网+大数据"开始,中国的这场"互联网+" ...
- Java面试题合集(二)
接下来几篇文章准备系统整理一下有关Java的面试题,分为基础篇,javaweb篇,框架篇,数据库篇,多线程篇,并发篇,算法篇等等,陆续更新中.其他方面如前端后端等等的面试题也在整理中,都会有的. 注: ...
- JAVA_SE基础——64.StringBuffer类 ①
字符串特点:字符串是常量:它们的值在创建之后不能更改 字符串的内容一旦发生了变化,那么马上会创建一个新的对象. 注意:字符串的内容不适宜频繁修改,因为一旦修改马上就会创建一个新的对象. publ ...
- JAVA_SE基础——39.继承
在面向对象程序设计中,可以从已有的类派生出新类. 这称做继承(inheritance). 白话解释: 例子1:继承一般是指晚辈从父辈那里继承财产,也可以说是子女拥有父母给予他们的东西. 例子2:猫和狗 ...
- JAVA_SE基础——27.匿名对象
黑马程序员入学blog... 匿名对象:没有引用类型变量指向的对象称作为匿名对象. 匿名对象要注意的事项:1. 我们一般不会给匿名对象赋予属性值,因为永远无法获取到.2. 两个匿名对象永远都不可能是同 ...
- proxymysql的安装与应用
具体的资料我们可以查看官方的文档:https://github.com/sysown/proxysql/wiki/ProxySQL-Configuration 推荐下载最新的Proxysql. 下面跟 ...
- 新概念英语(1-47)A cup of coffee
新概念英语(1-47)A cup of coffee How does Ann like her coffee? A:Do you like coffee, Ann? B:Yes, I do. A:D ...
- 新概念英语(1-23)Which glasses?
Which glasses does the man want? A:Give me some glasses please, Jane? B:Which glasses? These glasses ...