先跑spfa求出最短路构成的DAG,然后在DAG上跑出支配树dfs出size取max即可

关于支配树,因为是DAG,支配点就是入点在支配树上的lca,所以一边拓扑一边预处理倍增,然后用倍增求lca

#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<vector>
using namespace std;
const int N=400005;
int n,m,s,h[N],cnt,fa[N],sz[N],ans,to[N],si[N],hs[N],de[N],fr[N],b[N],tot,d[N],f[N][20];
long long dis[N];
bool v[N];
vector<int>a[N];
struct qwe
{
int ne,to,va;
}e[N<<1];
int read()
{
int r=0,f=1;
char p=getchar();
while(p>'9'||p<'0')
{
if(p=='-')
f=-1;
p=getchar();
}
while(p>='0'&&p<='9')
{
r=r*10+p-48;
p=getchar();
}
return r*f;
}
void add(int u,int v,int w)
{
cnt++;
e[cnt].ne=h[u];
e[cnt].to=v;
e[cnt].va=w;
h[u]=cnt;
}
int lca(int x,int y)
{//cerr<<" "<<x<<" "<<y<<endl;
if(de[x]<de[y])
swap(x,y);
for(int i=18;i>=0;i--)
if(de[f[x][i]]>=de[y])
x=f[x][i];
if(x==y)
return x;//cerr<<" "<<x<<" "<<y<<endl;
for(int i=18;i>=0;i--)
if(f[x][i]!=f[y][i])
x=f[x][i],y=f[y][i];//cerr<<x<<" "<<y<<endl;
return f[x][0];
}
void dfs(int u)
{
sz[u]=1;
for(int i=h[u];i;i=e[i].ne)
{
dfs(e[i].to);
sz[u]+=sz[e[i].to];
}
}
int main()
{
n=read(),m=read(),s=read();
for(int i=1;i<=m;i++)
{
int x=read(),y=read(),z=read();
add(x,y,z),add(y,x,z);
}
queue<int>q;
for(int i=1;i<=n;i++)
dis[i]=1e18;
dis[s]=0;
v[s]=1;
q.push(s);
while(!q.empty())
{
int u=q.front();
q.pop();
v[u]=0;
for(int i=h[u];i;i=e[i].ne)
if(dis[e[i].to]>dis[u]+e[i].va)
{
dis[e[i].to]=dis[u]+e[i].va;
if(!v[e[i].to])
{
v[e[i].to]=1;
q.push(e[i].to);
}
}
}
for(int u=1;u<=n;u++)
for(int i=h[u];i;i=e[i].ne)
if(dis[e[i].to]+e[i].va==dis[u])
a[u].push_back(e[i].to);
memset(h,0,sizeof(h));
cnt=0;
for(int u=1;u<=n;u++)
for(int i=0,len=a[u].size();i<len;i++)
add(a[u][i],u,0),d[u]++;
q.push(s);
de[s]=1;
while(!q.empty())
{
int u=q.front();
q.pop();
b[++tot]=u;
if(a[u].size())
f[u][0]=a[u][0];//cerr<<u<<" "<<f[u][0]<<" ";
for(int i=1,len=a[u].size();i<len;i++)
f[u][0]=lca(f[u][0],a[u][i]);//cerr<<f[u][0]<<endl;
de[u]=de[f[u][0]]+1;
for(int i=1;i<=18;i++)
f[u][i]=f[f[u][i-1]][i-1];
for(int i=h[u];i;i=e[i].ne)
if(!(--d[e[i].to]))
q.push(e[i].to),f[e[i].to][0]=u;
}//cerr<<lca(3,6)<<endl;
// for(int i=1;i<=n;i++)
// {
// for(int j=0;j<5;j++)
// cerr<<f[i][j]<<" ";
// cerr<<endl;
// }
memset(h,0,sizeof(h));
cnt=0;
for(int i=1;i<=n;i++)
if(f[i][0])
add(f[i][0],i,0);//,cerr<<f[i][0]<<" "<<i<<endl;
dfs(s);
for(int i=1;i<=n;i++)
if(i!=s)
ans=max(ans,sz[i]);
printf("%d\n",ans);
return 0;
}

codeforces757F Team Rocket Rises Again【支配树+倍增+拓扑+spfa】的更多相关文章

  1. CF757F Team Rocket Rises Again——最短路+支配树

    CF757F Team Rocket Rises Again 全体起立,全体起立,这是我A的第一道黑题(虽然是CF的): 来一波番茄攻击: 不扯淡了,这道题也是学习支配树(之前)应该做的题: 和灾难不 ...

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

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

  3. 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 ...

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

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

  5. codeforces 757F Team Rocket Rises Again

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

  6. CF757F Team Rocket Rises Again

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

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

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

  8. luogu2597-[ZJOI2012]灾难 && DAG支配树

    Description P2597 [ZJOI2012]灾难 - 洛谷 | 计算机科学教育新生态 Solution 根据题意建图, 新建一个 \(S\) 点, 连向每个没有入边的点. 定义每个点 \( ...

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

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

随机推荐

  1. springMvc 4.0 jackson包改变

    使用之前的json包出包java.lang.NoClassDefFoundError: com/fasterxml/jackson/core/JsonProcessingException错误. sp ...

  2. 【python】python版本升级,从2.6.6升级到2.7.13

    centos6.5系统自带了2.6.6版本的python,有时候为了项目上的需要,需要将python版本升级到2.7.13,下面介绍了如何进行升级. 说明:python从2.6升级到2.7会引发很多问 ...

  3. CentOS下配置静态IP

    第一.在VMware中进行配置使用桥接的方式.点击编辑选择虚拟网络编辑器 选择桥接模式,选择桥接到外部的网卡.选择我们主机下的网卡 第二步.配置虚拟机使用桥接模式 第三步:启动虚拟机进入到 /etc/ ...

  4. uboot中添加自定义命令

    uboot中可以通过修改源程序来添加自定义命令,进一步扩展uboot的功能. 我想在uboot下添加一条新的命令(名为varcpy),用来拷贝uboot中的环境变量. 修改方式如下: 创建新文件com ...

  5. ES6中promise总结

    一.什么是ES6的Promise 讲太多也没有.直接在打印出来就好,console.dir(Promise) Promise 是一个构造函数,自身有all, reject, resolve 这几个眼熟 ...

  6. hihocoder(第十周)二叉树(前序中序推后续)递推实现

    题目 : 后序遍历 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 在参与过了美食节之后,小Hi和小Ho在别的地方又玩耍了一阵子,在这个过程中,小Ho得到了一个非常有意思 ...

  7. Ruby 仿 C 结构体:CStruct 的一些例子

    1. [代码]最简单的例子     # CStruct Examplesrequire 'cstruct' # example:# struct Point in C\C++        (32-b ...

  8. longtable 跨越多个页面时,如何在跨页时自动断行并加上横线及去掉页眉

    参考: http://users.sdsc.edu/~ssmallen/latex/longtable.html 一般的,在首行后面加上 \endfirsthead\hline\endhead\hli ...

  9. L87

    Fear Makes Art More Engaging Emmanuel Kant spoke often about the sublime, and specifically how art b ...

  10. CF785CAnton and Permutation(分块 动态逆序对)

    Anton likes permutations, especially he likes to permute their elements. Note that a permutation of  ...