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. Bootstrap历练实例:导航中的表单

    Bootstrap历练实例:导航中的表单,它是使用class.navbar-form类,这确保了表单适当的垂直对齐和在较窄的视口中折叠的行为,使用这个对齐方式选项来决定导航栏中的内容放置在哪里. 实例 ...

  2. 三. python面向对象

    第七章.面向对象基础 1.面向对象基础 类和对象: a. 创建类 class 类名: def 方法名(self,xxx): pass b. 创建对象 对象 = 类名() c. 通过对象执行方法 对象. ...

  3. iOS开发遇到的坑之三--使用asi框架在xcode下正常运行,但是打包时却不能进行网络访问

    前言: 前两篇博客遇到的问题是前几天在实验室开发的时候遇到的,花了两三天时间在上面,今天突然心血来潮,想把这些”坑”写下来,所以才有了这两篇写的很丑的博客随笔 今天在开发时又遇到一个问题,那就是标题所 ...

  4. html中注释的问题

    在修改jsp页面的时候遇到了一个有点难懂的注释,mark一下 <script> <!-- alert("js code"); //--> </scri ...

  5. python--网络编程之socket

    一 . 网络编程 CS架构 客户端服务端架构 服务端:提供服务的 客户端:享受服务的 BS架构:浏览器和服务端 网络通信流程: 集线器:将所有连接上它的电脑全部联通起来 交换机:升级版的集线器 网卡: ...

  6. 我的Python分析成长之路3

    一 集合                                                                                                 ...

  7. Configure Red Hat Enterprise Linux shared disk cluster for SQL Server

    下面一步一步介绍一下如何在Red Hat Enterprise Linux系统上为SQL Server配置共享磁盘集群(Shared Disk Cluster)及其相关使用(仅供测试学习之用,基础篇) ...

  8. 大数据学习——hive的sql练习题

    ABC三个hive表 每个表中都只有一列int类型且列名相同,求三个表中互不重复的数 create table a(age int) row format delimited fields termi ...

  9. Codeforces Round #352 (Div. 1) B. Robin Hood

    B. Robin Hood 讲道理:这种题我是绝对不去(敢)碰的.比赛时被这个题坑了一把,对于我这种不A不罢休的人来说就算看题解也要得到一个Accepted. 这题网上有很多题解,我自己是很难做出来的 ...

  10. ZipKin原理学习--zipkin支持日志打印追踪信息

       目前在zipkin brave已经提供功能在我们使用logback或log4j等时可以在日志信息中将traceId和spanId等信息打印到运行日志,这样可能对我们通过日志查看解决问题有比较大的 ...