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 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·1051 ≤ 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 ≠ v1 ≤ 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;
}

  

posted @ 2018-04-11 13:58  蒟蒻JHY  阅读(313)  评论(0编辑  收藏  举报