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 n, m 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 u, v and w (1 ≤ u, v ≤ n, u ≠ 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
4 4 3
1 2 1
2 3 1
2 4 1
3 1 1
2
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
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的更多相关文章
- CF757F Team Rocket Rises Again——最短路+支配树
CF757F Team Rocket Rises Again 全体起立,全体起立,这是我A的第一道黑题(虽然是CF的): 来一波番茄攻击: 不扯淡了,这道题也是学习支配树(之前)应该做的题: 和灾难不 ...
- codeforces 757F Team Rocket Rises Again
链接:http://codeforces.com/problemset/problem/757/F 正解:灭绝树. mdzz倍增lca的根节点深度必须是1..我因为这个错误调了好久. 我们考虑先求最短 ...
- codeforces757F Team Rocket Rises Again【支配树+倍增+拓扑+spfa】
先跑spfa求出最短路构成的DAG,然后在DAG上跑出支配树dfs出size取max即可 关于支配树,因为是DAG,支配点就是入点在支配树上的lca,所以一边拓扑一边预处理倍增,然后用倍增求lca # ...
- CF757F Team Rocket Rises Again
题意 建出最短路图(DAG)之后就跟这题一样了. code: #include<bits/stdc++.h> using namespace std; #define int long l ...
- Solution -「CF 757F」Team Rocket Rises Again
\(\mathcal{Description}\) link. 给定 \(n\) 个点 \(m\) 条边的无向图和一个源点 \(s\).要求删除一个不同与 \(s\) 的结点 \(u\),使得 ...
- cf757F Team Rocket Rises Again (dijkstra+支配树)
我也想要皮卡丘 跑一遍dijkstra,可以建出一个最短路DAG(从S到任意点的路径都是最短路),然后可以在上面建支配树 并不会支配树,只能简单口胡一下在DAG上的做法 建出来的支配树中,某点的祖先集 ...
- Codeforces 959 F. Mahmoud and Ehab and yet another xor task
\(>Codeforces\space959 F. Mahmoud\ and\ Ehab\ and\ yet\ another\ xor\ task<\) 题目大意 : 给出一个长度为 \ ...
- Codeforces 835 F. Roads in the Kingdom
\(>Codeforces\space835 F. Roads in the Kingdom<\) 题目大意 : 给你一棵 \(n\) 个点构成的树基环树,你需要删掉一条环边,使其变成一颗 ...
- Codeforces 731 F. Video Cards(前缀和)
Codeforces 731 F. Video Cards 题目大意:给一组数,从中选一个数作lead,要求其他所有数减少为其倍数,再求和.问所求和的最大值. 思路:统计每个数字出现的个数,再做前缀和 ...
随机推荐
- struts1标签库
Struts提供了五个标签库,即:HTML.Bean.Logic.Template和Nested. HTML标签 : 用来创建能够和Struts 框架和其他相应的HTML 标签交互的HTML 输入表单 ...
- VUE2中axios的使用方法
一,安装 npm install axios 二,在http.js中引入 import axios from 'axios'; 三,定义http request 拦截器,添加数据请求公用信息 axio ...
- app内嵌H5调用分享
最近产品提出了一个需求:我们在合作方的app中提供的部分页面中增加分享页面,具体要求是在3个二维码推广页面调用app的分享接口,分享方式有3种,分别是点击”分享链接“按钮调起分享,点击”分享图片“按钮 ...
- (58)zabbix网络拓扑图配置network map
zabbix网络地图介绍 “zabbix network map”可以简单的理解为动态网络拓扑图,可以针对业务来配置zabbix map, 通过map可以了解应用的整体状况:服务器是否异常.网络是否有 ...
- 《UNIX环境高级编程》笔记——4.文件和目录
一.引言 本章描述文件系统的其他特征和文件的性质.有些背景知识需要注意,例如用户ID与文件权限.文件系统等. 二.函数stat.fstat.fstatat和lstat #include <sys ...
- Python面向对象(约束,异常处理,md5加密)(五)
1. 类的约束 1. 写一个父类. 父类中的某个方法要抛出一个异常 NotImplementedError class Base: def login(self): raise NotImplemen ...
- shell基础学习-难点重点学习
来自shell13问 -e : 啟用反斜線控制字符的轉換(參考下表) -E:關閉反斜線控制字符的轉換(預設如此) -n : 取消行末之換行符號(與 -e 選項下的 \c 字符同意) 要取消一個变量,在 ...
- Java-确定被加载类的路径
如何输出当前类在硬盘的物理路径 package com.tj; import java.net.URL; import java.security.CodeSource; import java.se ...
- joyoi1957 「Poetize5」Vani和Cl2捉迷藏
最小路径可重点覆盖.先传递闭包,然后拆点,\(n-\)最大匹配,看算法竞赛进阶指南. #include <iostream> #include <cstring> #inclu ...
- luogu3760 [TJOI2017]异或和
看这里 #include <iostream> #include <cstring> #include <cstdio> using namespace std; ...