CF757F Team Rocket Rises Again

全体起立,全体起立,这是我A的第一道黑题(虽然是CF的);

来一波番茄攻击;

不扯淡了,这道题也是学习支配树(之前)应该做的题;

灾难不同的是,那个是直接给你有向图,这里给的是无向图;

我们要求的是删除一个点会造成多少点的最短路发生变化,那么我们可以根据最短路再建一个有向图,这样就和灾难一样了;

很不幸,我建了四个图;

因为一开始写挂了所以图的编号是乱的;(这并不影响我AC)
add是无向图,add2是有向图,add4是反图,add3是建出来的支配树;

首先我们为什么要建反图:

{

一个点是否对其他的点有价值,就是看是否他在最短路径上;

我们根据有向图算出了拓扑序,拓扑序靠前的就是靠近起始点的;

我们需要建支配树,那么当前点要当谁的儿子?

当然是所有连向他的点的LCA,因为所有连向他的点删掉后(点数大于等于2)还可以在其他的点上走;

我们当初只建了一个有向图连向当前点,我们还要回去找LCA,所以要建一个反图;
}

然后子树大小即为答案;(取最大值)

#include<cstdio>
#include<queue>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll;
const int maxn=3e5+;
int n,m,s;
int pre[maxn*],last[maxn],other[maxn*],l;
ll len[maxn*];
int pre2[maxn*],last2[maxn],other2[maxn*],l2;
int pre3[maxn*],last3[maxn],other3[maxn*],l3;
int pre4[maxn*],last4[maxn],other4[maxn*],l4;
void add(int x,int y,ll z)
{
l++;
pre[l]=last[x];
last[x]=l;
other[l]=y;
len[l]=z;
}
void add2(int x,int y)
{
l2++;
pre2[l2]=last2[x];
last2[x]=l2;
other2[l2]=y;
//len[l]=z;
} void add3(int x,int y)
{
l3++;
pre3[l3]=last3[x];
last3[x]=l3;
other3[l3]=y;
//len[l]=z;
}
void add4(int x,int y)
{
l4++;
pre4[l4]=last4[x];
last4[x]=l4;
other4[l4]=y;
}
priority_queue<pair<ll,int> > qq;
bool vis[maxn];
ll dis[maxn];
void dijkstra(int x)
{
memset(vis,,sizeof(vis));
memset(dis,0x3f,sizeof(dis));
dis[x]=;
qq.push(make_pair(,x));
while(!qq.empty())
{
int u=qq.top().second;
qq.pop();
if(vis[u]) continue;
vis[u]=;
for(int p=last[u];p;p=pre[p])
{
int v=other[p];
if(dis[v]>dis[u]+len[p])
{
dis[v]=dis[u]+len[p];
qq.push(make_pair(-dis[v],v));
}
}
}
}
queue<int> q;
int topo[maxn],sum,in_eage[maxn];
void toposort()//此时用有向图找出拓扑序
{
for(int i=;i<=n;i++)
{
if(in_eage[i]==&&dis[i]!=dis[]) q.push(i);//此时要抛弃不在有向图上的点
}
while(!q.empty())
{
int x=q.front();
q.pop();
topo[++sum]=x;
for(int p=last2[x];p;p=pre2[p])
{
int v=other2[p];
in_eage[v]--;
if(in_eage[v]==)
{
q.push(v);
}
}
}
}
int father[maxn],f[maxn][];
int dep[maxn]; void rmq(int x)
{
f[x][]=father[x];
for(int i=;i<=;i++)
{
f[x][i]=f[f[x][i-]][i-];
}
} int LCA(int x,int y)
{
if(dep[x]<dep[y]) swap(x,y);
for(int j=;j<=;j++)
{
if((dep[x]-dep[y])&(<<j)) x=f[x][j];
}
if(x==y) return x;
for(int j=;j>=;j--)
{
if(f[x][j]!=f[y][j])
{
x=f[x][j];
y=f[y][j];
}
}
return father[x];
} void build()//重新建树的时候用反图
{
father[n+]=n+;//建立虚拟节点
f[n+][]=n+;
dep[n+]=;
for(int i=;i<=n;i++)//拓扑序从小到大开始遍历,根据反图,当前建的树的子树大小即为支配数
{
if(!topo[i]) break;//并不是所有点都在有向图上 ,但是图上的点拓扑序是连续的
int x=topo[i];
if(!last4[x])//将没有出边的点连上虚拟节点
{
dep[x]=;
father[x]=n+;
f[x][]=n+;
add3(n+,x);//3为生成的支配树
continue;
} int lca=other4[last4[x]];
for(int p=last4[x];p;p=pre4[p])
{
int v=other4[p];
lca=LCA(lca,v);
}
father[x]=lca;
dep[x]=dep[lca]+;
add3(lca,x);
rmq(x);
}
} int siz[maxn];
int ans;
void dfs(int x)
{
siz[x]=;
for(int p=last3[x];p;p=pre3[p])
{
int v=other3[p];
dfs(v);
siz[x]+=siz[v];
}
if(x!=n+&&x!=s) ans=max(ans,siz[x]);
}
int main()
{
scanf("%d%d%d",&n,&m,&s);
for(int i=;i<=m;i++)
{
int x,y;ll z;
scanf("%d%d%lld",&x,&y,&z);
add(x,y,z);
add(y,x,z);//建无向图
}
dijkstra(s);//求最短路
for(int i=;i<=n;i++)
{
for(int p=last[i];p;p=pre[p])
{
int v=other[p];
if(dis[v]==dis[i]+len[p])
{
add2(i,v);//根据最短路建立有向图
add4(v,i); //反图
in_eage[v]++;//rudu
}
}
}
toposort();
build();
dfs(n+);
printf("%d",ans);
return ;
}

CF757F Team Rocket Rises Again——最短路+支配树的更多相关文章

  1. cf757F Team Rocket Rises Again (dijkstra+支配树)

    我也想要皮卡丘 跑一遍dijkstra,可以建出一个最短路DAG(从S到任意点的路径都是最短路),然后可以在上面建支配树 并不会支配树,只能简单口胡一下在DAG上的做法 建出来的支配树中,某点的祖先集 ...

  2. CF757F Team Rocket Rises Again

    题意 建出最短路图(DAG)之后就跟这题一样了. code: #include<bits/stdc++.h> using namespace std; #define int long l ...

  3. codeforces757F Team Rocket Rises Again【支配树+倍增+拓扑+spfa】

    先跑spfa求出最短路构成的DAG,然后在DAG上跑出支配树dfs出size取max即可 关于支配树,因为是DAG,支配点就是入点在支配树上的lca,所以一边拓扑一边预处理倍增,然后用倍增求lca # ...

  4. Codeforces 757 F Team Rocket Rises Again

    Discription It's the turn of the year, so Bash wants to send presents to his friends. There are n ci ...

  5. Solution -「CF 757F」Team Rocket Rises Again

    \(\mathcal{Description}\)   link.   给定 \(n\) 个点 \(m\) 条边的无向图和一个源点 \(s\).要求删除一个不同与 \(s\) 的结点 \(u\),使得 ...

  6. codeforces 757F Team Rocket Rises Again

    链接:http://codeforces.com/problemset/problem/757/F 正解:灭绝树. mdzz倍增lca的根节点深度必须是1..我因为这个错误调了好久. 我们考虑先求最短 ...

  7. CF757F-Team Rocket Rises Again【最短路,DAG支配树】

    正题 题目链接:https://www.luogu.com.cn/problem/CF757F 题目大意 \(n\)个点\(m\)条边的一张无向图,求删除\(s\)以外的一个点改变\(s\)到最多点的 ...

  8. 康复计划#4 快速构造支配树的Lengauer-Tarjan算法

    本篇口胡写给我自己这样的老是证错东西的口胡选手 以及那些想学支配树,又不想啃论文原文的人- 大概会讲的东西是求支配树时需要用到的一些性质,以及构造支配树的算法实现- 最后讲一下把只有路径压缩的并查集卡 ...

  9. [HDU]4694 Important Sisters(支配树)

    支配树模板 #include<cstdio> #include<cstring> #include<algorithm> using namespace std; ...

随机推荐

  1. jQuery控制页面滚动条上下滚动

    .向上滚动  $(); .向下滚动   $(); 参数解读:$(this)表示要实现上下滚动的对象,-50表示向上滚动50px , +50表示向下滚动50px ,1000表示滚动速度

  2. SQL SERVER 中如何获取日期(一个月的最后一日、一年的第一日等等)

    https://blog.csdn.net/deepwishly/article/details/9101307 这是计算一个月第一天的SQL 脚本:   SELECT DATEADD(mm, DAT ...

  3. 测试winform程序到树莓派运行

    啥也不说了,都在下图中了.winform可以在树莓派上跑了

  4. snort_inline

    snort_inline Link   http://snort-inline.sourceforge.net/oldhome.html What is snort_inline? snort_inl ...

  5. BASIS小问题汇总1

    try to start SAP system but failed 2019-04-04 Symptom: when i tried to start SAP system, using the c ...

  6. 七年开发经验详解JVM的GC 算法

    概述 GC 是 JVM 自带的功能,它能够自动回收对象,清理内存,这是 Java 语言的一大优势,但是GC绝不仅伴随着Java,相反,GC历史比Java更悠久.关于GC,我认为有四个问题需要解决: 为 ...

  7. python之tkinter入坑Pack()------(1)

    tkinter 的pack()可以设置的属性如下: pack_configure(self, cnf={}, **kw)Pack a widget in the parent widget. Use  ...

  8. Python_关键字列表

    1.Python关键字列表

  9. pytorch版yolov3训练自己数据集

    目录 1. 环境搭建 2. 数据集构建 3. 训练模型 4. 测试模型 5. 评估模型 6. 可视化 7. 高级进阶-网络结构更改 1. 环境搭建 将github库download下来. git cl ...

  10. Resource接口

    [转]https://blog.csdn.net/hbtj_1216/article/details/85487787 参考:官方文档 1 简介 Java标准库中的java.net.URL类和标准处理 ...