当前位置:网站首页>[atcoder1998] stamp Rally
[atcoder1998] stamp Rally
2022-06-11 07:36:00 【CaptainHarryChen】
The question
We have a N (3<=N<=10^5) A node and M(N−1≤M≤10^5) An undirected graph of edges . The node number is 1 To N, Side number is 1 To M. edge i Connection nodes ai and bi. Make sure the graph is connected . On this picture ,Q(1≤Q≤10^5) A pair of brothers are participating in a project called Stamp Rally The activities of . The first i Yes Stamp Rally as follows :
A brother from the node xi Start , Another slave node yi Start .(1≤xi < yi≤N)
Two brothers visit the nodes on the graph along the edge , A total of access zi(3≤zi≤N) Nodes , Including the starting node . ad locum , Even if a node is accessed multiple times , Calculate only once .
Define the score as the maximum number of the edges they cross . Their goal is to minimize this score .
Find out the lowest score for each brother .
Answer key
Add the number of edges to the graph from small to large , Use and collect , Each side added i, Scan all queries , If the size of their Unicom reaches zi, The answer to this question by i.
But this is O(MQ) O ( M Q ) Of .
Use the whole dichotomy , Add the first half first [l,mid] [ l , m i d ] The edge of , Determine which queries have met the criteria , The answers to these questions must be ≤mid ≤ m i d , Divide it into the first half , Then restore the merged query set , Recursive processing [l,mid] [ l , m i d ] Of , and [mid+1,r] [ m i d + 1 , r ] Of .
In order to restore the merge set , You can use rank consolidation .
Code
#include<cstdio>
#include<vector>
#include<algorithm>
using namespace std;
const int MAXN=100005;
int N,M,Q;
struct Edge
{
int u,v;
}E[MAXN];
struct Query
{
int x,y,z;
}q[MAXN];
int id[MAXN],tmp[MAXN],mk[MAXN],ans[MAXN];
int stk[MAXN][2],tp;
int fa[MAXN],siz[MAXN];
int Root(int u)
{
if(fa[u]==0)
return u;
return Root(fa[u]);
}
void Union(int u,int v)
{
int r1=Root(u),r2=Root(v);
if(r1==r2)
return;
if(siz[r1]<siz[r2])
swap(r1,r2);
fa[r2]=r1;
stk[++tp][0]=r2;
stk[tp][1]=r1;
siz[r1]+=siz[r2];
}
void solve(int a,int b,int l,int r)
{
if(a==b)
{
for(int i=l;i<=r;i++)
ans[id[i]]=a;
Union(E[a].u,E[a].v);
return;
}
tp=0;
int mid1=(a+b)/2;
for(int i=a;i<=mid1;i++)
Union(E[i].u,E[i].v);
for(int i=l;i<=r;i++)
{
int r1=Root(q[id[i]].x),r2=Root(q[id[i]].y);
if(r1==r2&&siz[r1]>=q[id[i]].z)
mk[i]=1;
if(r1!=r2&&siz[r1]+siz[r2]>=q[id[i]].z)
mk[i]=1;
}
int it=l-1;
for(int i=l;i<=r;i++)
if(mk[i])
tmp[++it]=id[i];
int mid2=it;
for(int i=l;i<=r;i++)
if(!mk[i])
tmp[++it]=id[i];
for(int i=l;i<=r;i++)
id[i]=tmp[i],mk[i]=0;
while(tp)
{
fa[stk[tp][0]]=0;
siz[stk[tp][1]]-=siz[stk[tp][0]];
tp--;
}
solve(a,mid1,l,mid2);
solve(mid1+1,b,mid2+1,r);
}
int main()
{
scanf("%d%d",&N,&M);
for(int i=1;i<=M;i++)
scanf("%d%d",&E[i].u,&E[i].v);
scanf("%d",&Q);
for(int i=1;i<=Q;i++)
scanf("%d%d%d",&q[i].x,&q[i].y,&q[i].z);
for(int i=1;i<=N;i++)
fa[i]=0,siz[i]=1;
for(int i=1;i<=Q;i++)
id[i]=i;
solve(1,M,1,Q);
for(int i=1;i<=Q;i++)
printf("%d\n",ans[i]);
return 0;
}边栏推荐
- Wc2020 course selection
- What is the difference between gaussdb for redis and redis?
- [STL source code analysis] summary notes (1): Opening
- 3年功能测试拿8K,被新来的反超,其实你在假装努力
- 【集群】LVS+keepalived高可用集群
- 零基础自学SQL课程 | UNION 联合查询
- C language volatile
- Crmeb/v4.4 Standard Version open version mall source code applet official account h5+app mall source code
- Matrix tree theorem
- Decimal to binary
猜你喜欢

After 4 years of naked resignation from the test, the test post of 15K interview was rubbed on the ground, and the result made me collapse and cry
![20200803 T3 my friends [divide and conquer NTT optimization recursion]](/img/35/01201e3136e3dd5cd562a0481f1ee9.jpg)
20200803 T3 my friends [divide and conquer NTT optimization recursion]

2022 melting welding and thermal cutting exam exercises and answers

【IoT】项目管理:如何打造更好的跨职能团队?

3年功能测试拿8K,被新来的反超,其实你在假装努力

QT picture adaptive display control

2022低压电工考题及在线模拟考试

Crmeb/v4.4 Standard Version open version mall source code applet official account h5+app mall source code
![[Oracle database] mammy tutorial day03 Sorting Query](/img/ea/24c9495a2ef4f1786f7b7852bde321.png)
[Oracle database] mammy tutorial day03 Sorting Query

【AtCoder2306】Rearranging(拓扑)
随机推荐
VIM common commands
QT interface nested movement based on qscrollarea
C language three chess games
C language function stack frame
[Oracle database] mammy tutorial day02 use of database management tool sqlplus
【NOIP2016 D1T3】换教室(期望DP+Floyd)(究极思维陷阱!)
模线性方程组(中国剩余定理+通用解法)
【AtCoder2376】Black and White Tree(博弈)
What is the difference between gaussdb for redis and redis?
【AtCoder2305】Decrementing(博弈)
Wc2020 guessing game
20200802 T3 I always like [generating function exclusion, Lagrangian inversion]
Summary of written test questions of shopee 2021 autumn recruitment
2020080 simulation competition [horizontal and vertical coordinates do not affect each other, cactus minimum cut, combined meaning translation formula]
Directrix of ellipse
. Net C Foundation (6): namespace - scope with name
二本畢業,銀行外包測試工作 4 個月有餘。聊聊一些真實感受 ...
Uoj 554 [unr 4] challenges Hamilton [find Hamilton path (adjustment method)]
【AtCoder1980】Mysterious Light(数学模拟)
[STL source code analysis] summary notes (7): ingenious deque