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,要求其他所有数减少为其倍数,再求和.问所求和的最大值. 思路:统计每个数字出现的个数,再做前缀和 ...
随机推荐
- oracle中group by的高级用法
简单的group by用法 select c1,sum(c2) from t1 where t1<>'test' group by c1 having sum(c2)>100; ro ...
- shell脚本,编程题练习。
题目是:将 文件file为 b+b+b+b+b+b+b+b 变为 b+b=b+b=b+b=b+b 解答方法如下:
- 高德定位腾讯定位在APP上无法开启定位权限的解决方案
[备注]公司项目中遇到的问题,如果你在团队工作其中定有不少配合方面的问题,其中的思路是可以借鉴的,因为这也许正是你们现在遇到的问题,总结的不好的地方还请多多指教 因为项目需求的确定,定位成了必不可少的 ...
- 【Linux】用户与权限
追加用户组 groupadd 用户组名 追加新用户 useradd -d 指定用户目录 -s 指定用户使用shell -g 指定用户组 -p 指定用户密码 用户名 更改用户 添加用户到其他组 use ...
- 前端开发中提到的“脚手架”到底指什么,CLI?gulp 和 gulp-cli有什么区别
一般来说,脚手架是帮你减少「为减少重复性工作而做的重复性工作」的工具. gulp和gulp-cli的区别可以看这个task - what does gulp-"cli" stand ...
- python中判断字符串是否为中文
判断字符串是否在中文编码范围内 for c in s: if not ('\u4e00' <= c <= '\u9fa5'): return False ...
- BRVAH(让RecyclerView变得更高效) (3)
本文来自网易云社区 作者:吴思博 3 实现列表加载动画效果 3.1默认动画 我们只需将自建的 adapter 继承它对应满足需求的 Adapter,然后在 Activity 中实例化,通过ope ...
- Cookie测试的测试点
1.禁止使用Cookie:设置浏览器禁止使用Cookie,访问网页后,检查存放Cookie文件中未生成相关文件: 2.Cookie寻出路径:按照操作系统和浏览器对Cookie存放路径的设置,检查存放路 ...
- 汇编CMOS
汇编 端口 端口 前面讲过,各种存储器都和CPU的地址线.数据线.控制线相连.CPU在操控它们的时候,把它们都当作内存来对待,把它们总的看做一个由若干存储单元组成的逻辑存储器,这个逻辑器我们称其为内存 ...
- ubuntu 终端查看图片(eog)
远程登陆服务器的话,是没有办法直接查看图片的,这时我们需要进入图片所在目录,然后通过终端命令查看图片. 想要查看图片,需要通过ssh -X登陆,然后在终端输入命令: eog 图片名