| 比赛 |
2026.4.11 |
评测结果 |
AEEEEEEEEEA |
| 题目名称 |
rldcot |
最终得分 |
19 |
| 用户昵称 |
梦那边的美好ME |
运行时间 |
1.304 s |
| 代码语言 |
C++ |
内存使用 |
3.43 MiB |
| 提交时间 |
2026-04-11 12:47:13 |
显示代码纯文本
#include <bits/stdc++.h>
using namespace std;
#define ll long long
struct node{
ll v,w;
};
ll n,m;
vector<node> g[105];
ll p[105],d[105];
ll dis[105];
void dfs(ll u,ll fa,ll dep,ll len){
p[u]=fa;
d[u]=dep;
dis[u]=len;
for (auto &e:g[u]){
if (e.v!=fa) dfs(e.v,u,dep+1,len+e.w);
}
}
ll lca(ll u,ll v){
if (d[u]<d[v]) swap(u,v);
while (d[u]>d[v]) u=p[u];
while (u!=v){
u=p[u];
v=p[v];
}
return u;
}
int main(){
freopen("rldcot.in","r",stdin);
freopen("rldcot.out","w",stdout);
ios::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
cin>>n>>m;
for (int i=0;i<n-1;i++){
int u,v,w;
cin>>u>>v>>w;
g[u].push_back({v,w});
g[v].push_back({u,w});
}
dfs(1,0,1,0);
while (m--){
int l,r;
cin>>l>>r;
set<ll> s;
for (int i=l;i<=r;i++){
for (int j=i;j<=r;j++){
int anc=lca(i,j);
s.insert(dis[anc]);
}
}
cout<<s.size() <<'\n';
}
return 0;
}