lanqiao oj 机房

简介: lanqiao oj 机房

用户登录

我们把延迟当作边的权值

#include<iostream>
#include<algorithm>
#include<cstring>
#include<vector>
#include<queue>
 
using namespace std ; 
const int N = 2e5 +10 ;
struct edge{
  int from , to ;
  edge(int fromm , int too ){
    from = fromm , to = too  ;
  }
};
vector<edge> e[N] ;
struct s_node{
  int id; long long n_dis ;
  s_node(int idd, long long n_diss){
    id = idd , n_dis = n_diss ;
  }
  bool operator < (const s_node &o) const {
    return n_dis > o.n_dis ;  
  }
};
int n , m ;
long long dis[N] ;
int t[N] ;
bool done[N] ;
void dijkstra(int s, int final){
  memset(dis,0x3f,sizeof(dis)) ;
  memset(done, 0 , sizeof(done)) ;
  dis[s] = 0 ;
  
  priority_queue<s_node> q ;
  q.push(s_node(s,dis[s])) ;
  while(!q.empty()){
    s_node u = q.top() ;
    q.pop() ;
    if(u.id == final) return  ;
    if(done[u.id]) continue ; 
    done[u.id] = 1 ;
    for(int i = 0 ; i < e[u.id].size() ; i ++){
      edge y = e[u.id][i] ;
      if(done[y.to]) continue ; 
      if(dis[y.to] > dis[u.id] + t[u.id]){
        dis[y.to] = dis[u.id] + t[u.id] ;
        q.push(s_node(y.to , dis[y.to])) ;
        
      }
    }
  }
  
}
int main(){
  cin >> n >> m ; 
  for(int i = 1 ; i < n ;i ++){
    int a , b ; cin >> a >> b ;
    e[a].push_back({a,b}) ;
    e[b].push_back({b,a}) ;
    t[a] ++ , t[b] ++ ;
  }
  while(m --){
    int l , r ; cin >> l >> r ;
    dijkstra(l,r) ;
    cout << dis[r] + t[r] << endl ;
  }
  return 0 ;
}
目录
相关文章
|
3天前
lanqiao OJ 1388 寒假作业
lanqiao OJ 1388 寒假作业
15 0
|
3天前
lanqiao OJ 389 摆花
lanqiao OJ 389 摆花
8 2
|
3天前
lanqiao OJ 108 发现环
lanqiao OJ 108 发现环
7 1
|
3天前
lanqiao OJ 3513 岛屿个数(2023省赛)
lanqiao OJ 3513 岛屿个数(2023省赛)
9 2
|
3天前
lanqiao OJ 525 传球游戏
lanqiao OJ 525 传球游戏
11 2
|
3天前
lanqiao OJ 89 路径之谜
lanqiao OJ 89 路径之谜
7 1
|
1天前
lanqiao oj 1122 蓝桥王国
lanqiao oj 1122 蓝桥王国
5 0
|
3天前
lanqiao OJ141 穿越雷区
lanqiao OJ141 穿越雷区
7 0
|
1天前
lanqiao oj 1085 小猪存钱罐
lanqiao oj 1085 小猪存钱罐
6 0
|
1天前
lanqiao oj 1050 补给
lanqiao oj 1050 补给
8 0