Discription

It's the turn of the year, so Bash wants to send presents to his friends. There are n cities in the Himalayan region and they are connected by m bidirectional roads. Bash is living in city s. Bash has exactly one friend in each of the other cities. Since Bash wants to surprise his friends, he decides to send a Pikachu to each of them. Since there may be some cities which are not reachable from Bash's city, he only sends a Pikachu to those friends who live in a city reachable from his own city. He also wants to send it to them as soon as possible.

He finds out the minimum time for each of his Pikachus to reach its destination city. Since he is a perfectionist, he informs all his friends with the time their gift will reach them. A Pikachu travels at a speed of 1 meters per second. His friends were excited to hear this and would be unhappy if their presents got delayed. Unfortunately Team Rocket is on the loose and they came to know of Bash's plan. They want to maximize the number of friends who are unhappy with Bash.

They do this by destroying exactly one of the other n - 1 cities. This implies that the friend residing in that city dies, so he is unhappy as well.

Note that if a city is destroyed, all the roads directly connected to the city are also destroyed and the Pikachu may be forced to take a longer alternate route.

Please also note that only friends that are waiting for a gift count as unhappy, even if they die.

Since Bash is already a legend, can you help Team Rocket this time and find out the maximum number of Bash's friends who can be made unhappy by destroying exactly one city.

Input

The first line contains three space separated integers nm and s (2 ≤ n ≤ 2·105, 1 ≤ s ≤ n) — the number of cities and the number of roads in the Himalayan region and the city Bash lives in.

Each of the next m lines contain three space-separated integers uv and w (1 ≤ u, v ≤ nu ≠ v, 1 ≤ w ≤ 109) denoting that there exists a road between city u and city vof length w meters.

It is guaranteed that no road connects a city to itself and there are no two roads that connect the same pair of cities.

Output

Print a single integer, the answer to the problem.

Example

Input
4 4 3
1 2 1
2 3 1
2 4 1
3 1 1
Output
2
Input
7 11 2
1 2 5
1 3 5
2 4 2
2 5 2
3 6 3
3 7 3
4 6 2
3 4 2
6 7 3
4 5 7
4 7 7
Output
4

Note

In the first sample, on destroying the city 2, the length of shortest distance between pairs of cities (3, 2) and (3, 4) will change. Hence the answer is 2.

跑一遍最短路,把最短路dag建出来,然后就是一个灭绝树裸题了。

#include<bits/stdc++.h>
#define ll long long
#define pb push_back
const int maxn=300005;
using namespace std;
vector<int> g[maxn];
int n,m,hd[maxn],ne[maxn*2],S,ans,siz[maxn];
int to[maxn*2],val[maxn*2],num,id[maxn],cnt;
int f[maxn][22],FA[maxn],dep[maxn],ci[35];
struct node{
int x;
ll dis;
bool operator <(const node &u)const{
return dis>u.dis;
}
};
bool v[maxn];
ll d[maxn]; inline void add(int x,int y,int z){
to[++num]=y,ne[num]=hd[x],hd[x]=num,val[num]=z;
} inline void dij(){
priority_queue<node> q;
memset(d,0x7f,sizeof(d));
d[S]=0,q.push((node){S,0}); node x;
while(!q.empty()){
x=q.top(),q.pop();
if(v[x.x]) continue;
v[x.x]=1; for(int i=hd[x.x];i;i=ne[i]) if(x.dis+(ll)val[i]<d[to[i]]){
d[to[i]]=d[x.x]+(ll)val[i];
q.push((node){to[i],d[to[i]]});
}
}
} inline void build(int x,int fa){
g[fa].pb(x);
dep[x]=dep[fa]+1,f[x][0]=fa;
for(int i=1;ci[i]<=dep[x];i++) f[x][i]=f[f[x][i-1]][i-1];
} inline int LCA(int x,int y){
if(dep[x]<dep[y]) swap(x,y);
int derta=dep[x]-dep[y];
for(int i=0;ci[i]<=derta;i++) if(ci[i]&derta) x=f[x][i]; if(x==y) return x; int s=log(dep[x])/log(2)+1;
for(;s>=0;s--){
if(ci[s]>dep[x]) continue;
if(f[x][s]!=f[y][s]) x=f[x][s],y=f[y][s];
}
return f[x][0];
} void dfs(int x){
siz[x]=1;
for(int i=g[x].size()-1,to;i>=0;i--){
to=g[x][i];
dfs(to),siz[x]+=siz[to];
}
if(x!=S) ans=max(ans,siz[x]);
} inline void solve(){
for(int i=1;i<=n;i++)
for(int j=hd[i];j;j=ne[j]) if(d[i]+(ll)val[j]==d[to[j]]) id[to[j]]++; queue<int> q; int x;
q.push(S),dep[S]=0;
while(!q.empty()){
x=q.front(),q.pop();
if(x!=S) build(x,FA[x]); for(int i=hd[x];i;i=ne[i]) if(d[x]+(ll)val[i]==d[to[i]]){
if(!FA[to[i]]) FA[to[i]]=x;
else FA[to[i]]=LCA(FA[to[i]],x);
if(!(--id[to[i]])) q.push(to[i]);
}
} dfs(S);
} int main(){
// freopen("data.in","r",stdin);
// freopen("data.out","w",stdout); ci[0]=1;
for(int i=1;i<=20;i++) ci[i]=ci[i-1]<<1;
int uu,vv,ww;
scanf("%d%d%d",&n,&m,&S);
for(int i=1;i<=m;i++){
scanf("%d%d%d",&uu,&vv,&ww);
add(uu,vv,ww),add(vv,uu,ww);
} dij();
solve(); printf("%d\n",ans);
return 0;
}

  

Codeforces 757 F Team Rocket Rises Again的更多相关文章

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

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

  2. codeforces 757F Team Rocket Rises Again

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

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

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

  4. CF757F Team Rocket Rises Again

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

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

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

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

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

  7. Codeforces 959 F. Mahmoud and Ehab and yet another xor task

    \(>Codeforces\space959 F. Mahmoud\ and\ Ehab\ and\ yet\ another\ xor\ task<\) 题目大意 : 给出一个长度为 \ ...

  8. Codeforces 835 F. Roads in the Kingdom

    \(>Codeforces\space835 F. Roads in the Kingdom<\) 题目大意 : 给你一棵 \(n\) 个点构成的树基环树,你需要删掉一条环边,使其变成一颗 ...

  9. Codeforces 731 F. Video Cards(前缀和)

    Codeforces 731 F. Video Cards 题目大意:给一组数,从中选一个数作lead,要求其他所有数减少为其倍数,再求和.问所求和的最大值. 思路:统计每个数字出现的个数,再做前缀和 ...

随机推荐

  1. javase(11)_juc并发库

    一.传统线程技术 public static void main(String[] args) { Thread thread = new Thread(){ @Override public voi ...

  2. 【求助】NdisSend,自定义数据包发送失败?

    做ndis hook的时候,自定义了一个数据包,包结构应该没有问题,填充NDIS_PACKET结构是这样的,先初始化:        NdisAllocatePacketPool(&nStat ...

  3. C语言特点_01

    C语言特点: 1.C语言的32个关键字 auto 局部变量(自动储存) break 无条件退出程序最内层循环 case switch语句中选择项 char 单字节整型数据 const 定义不可更改的常 ...

  4. Ubuntu 18.04修改默认源

    安装Ubuntu 18.04后,使用国外源太慢了,修改为国内源会快很多. 修改阿里源为Ubuntu 18.04默认的源 备份/etc/apt/sources.list #备份 cp /etc/apt/ ...

  5. JS 绘制心形线

    JS 绘制心形线 <!DOCTYPE HTML> <html> <head> <meta charset="utf-8"> < ...

  6. Python 模块和包的概念

    模块&包(* * * * *) 模块(modue)的概念: 在计算机程序的开发过程中,随着程序代码越写越多,在一个文件里代码就会越来越长,越来越不容易维护. 为了编写可维护的代码,我们把很多函 ...

  7. Django中重定向页面的时候使用命名空间

    urls.py from django.urls import path from . import views app_name='front' urlpatterns = [ path('',vi ...

  8. 解决wordpress部分博客文章页面无法显示的问题

    搭建完wordpress,试着写了一篇博客.文章发布后,首页已经能显示出文章的标题,但是点进去后却提示该页无法显示. 百度一番,先后尝试网上的修改apache配置等方法后依然无效.折腾到最后无意间发现 ...

  9. linux无人值守安装介绍(一)

    一.术语解释 PXE(Pre-boot ExecutionEnvironment)是由Intel设计的协议,它可以使计算机通过网络而不是从本地硬盘.光驱等设备启动.现代的网卡,一般都内嵌支持PXE的R ...

  10. pytorch保存模型等相关参数,利用torch.save(),以及读取保存之后的文件

    本文分为两部分,第一部分讲如何保存模型参数,优化器参数等等,第二部分则讲如何读取. 假设网络为model = Net(), optimizer = optim.Adam(model.parameter ...