ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#196534 | #241. distance | CLY | 100 | 177ms | 1292kb | C++ | 1.4kb | 2023-10-27 23:26:41 | 2023-10-27 23:26:43 |
answer
#include <bits/stdc++.h>
using namespace std;
inline int read(){
int x=0; char ch; bool f=0;
while(((ch=getchar())<'0'||ch>'9')&&ch!='-') ;
if(ch=='-') f=1;
else x=ch^48;
while((ch=getchar())>='0'&&ch<='9') x=(x<<1)+(x<<3)+(ch^48);
return f?-x:x;
}
const int N=1e3+5;
int n,m,head[N],tot;
struct edge{
int nex,y,z;
}e[N<<1];
void add(int x,int y,int z){
e[++tot]={head[x],y,z}; head[x]=tot;
}
int mn[20][N<<1],lg[N<<1],d[N],pos[N],cnt;
long long di[N];
void dfs(int x,int f){
mn[0][++cnt]=x; d[x]=d[f]+1; pos[x]=cnt;
for(int i=head[x];i;i=e[i].nex){
int y=e[i].y;
if(y^f){
di[y]=di[x]+e[i].z;
dfs(y,x); mn[0][++cnt]=x;
}
}
}
void rebuild(){
dfs(1,0);
for(int i=2;i<=cnt;i++) lg[i]=lg[i>>1]+1;
int g=1;
for(int j=1;j<=lg[cnt];j++){
for(int i=1;i<=cnt-g;i++){
if(d[mn[j-1][i]]<d[mn[j-1][i+g]]) mn[j][i]=mn[j-1][i];
else mn[j][i]=mn[j-1][i+g];
}
g<<=1;
}
}
int lca(int x,int y){
int l=pos[x],r=pos[y];
if(l>r) swap(l,r);
int k=lg[r-l+1];
return d[mn[k][l]]<d[mn[k][r-(1<<k)+1]]?mn[k][l]:mn[k][r-(1<<k)+1];
}
long long dis(int x,int y){
return di[x]+di[y]-2*di[lca(x,y)];
}
int main(){
n=read(),m=read();
for(int i=1;i<n;i++){
int x=read(),y=read(),z=read();
add(x,y,z); add(y,x,z);
}
rebuild();
while(m--){
int x=read(),y=read();
long long ans=0;
for(int i=1;i<=n;i++){
ans=max(ans,min(dis(i,x),dis(i,y)));
}
printf("%lld\n",ans);
}
return 0;
}
Details
小提示:点击横条可展开更详细的信息
Test #1:
score: 10
Accepted
time: 25ms
memory: 1292kb
input:
1000 1000 2 1 68540200 3 1 91653768 4 1 110081418 5 4 124459973 6 2 113157153 7 6 10361274 8 1 37967...
output:
1331976414 1484469550 1299989233 1082204469 1219072417 1270794406 1212378454 1276468801 1171061991 1...
result:
ok 1000 lines
Test #2:
score: 10
Accepted
time: 17ms
memory: 1288kb
input:
1000 1000 2 1 69499945 3 2 110338871 4 1 75696558 5 4 81322687 6 2 109039910 7 5 86501255 8 3 334976...
output:
1415385565 1318895884 1365724920 1346036448 1319379010 1608706261 1172931889 1271213985 1205763988 1...
result:
ok 1000 lines
Test #3:
score: 10
Accepted
time: 15ms
memory: 1292kb
input:
1000 1000 2 1 69621257 3 1 57880022 4 2 56222404 5 4 123116940 6 3 119846619 7 6 54543961 8 7 100998...
output:
1589052249 1525675847 1587098644 1615880822 1558202615 1551888619 1180075361 1487018866 1598044257 1...
result:
ok 1000 lines
Test #4:
score: 10
Accepted
time: 17ms
memory: 1288kb
input:
1000 1000 2 1 69715436 3 1 41095210 4 3 24004426 5 1 55871725 6 5 39988206 7 1 85826665 8 6 26762326...
output:
1237240317 1404441028 1132480621 1148556268 1273419864 1247391588 1268838352 1302619624 1342881805 1...
result:
ok 1000 lines
Test #5:
score: 10
Accepted
time: 17ms
memory: 1292kb
input:
1000 1000 2 1 69855688 3 2 87175956 4 1 17241329 5 1 72483622 6 1 7270981 7 6 124912637 8 1 10175366...
output:
976871688 1048737020 1040715258 1241819360 1660837417 1294088431 1121692975 1214550822 1504469680 13...
result:
ok 1000 lines
Test #6:
score: 10
Accepted
time: 17ms
memory: 1288kb
input:
1000 1000 2 1 69977000 3 1 34749875 4 2 131984904 5 1 114273779 6 3 18110458 7 1 92951247 8 6 350039...
output:
1479751350 1377757445 1243274546 1445648987 1159375007 1435304578 1400242670 1196363233 1152603251 1...
result:
ok 1000 lines
Test #7:
score: 10
Accepted
time: 17ms
memory: 1288kb
input:
1000 1000 2 1 70117252 3 2 80830621 4 3 125258671 5 2 130881579 6 3 119615057 7 1 132037219 8 2 1099...
output:
1486331468 1590052286 1273577309 1238846956 1497198893 1530513127 1264914899 1432840120 1471093042 1...
result:
ok 1000 lines
Test #8:
score: 10
Accepted
time: 17ms
memory: 1292kb
input:
1000 1000 2 1 70175090 3 2 36796946 4 3 54801027 5 2 4969800 6 5 36131197 7 3 84473389 8 6 13291700 ...
output:
1189374243 1243503418 1042295012 1107507679 1092866274 1299368615 957390902 1043378027 1122734108 10...
result:
ok 1000 lines
Test #9:
score: 10
Accepted
time: 18ms
memory: 1292kb
input:
1000 1000 2 1 70309706 3 2 47232326 4 2 60818619 5 3 130645741 6 5 94083190 7 6 60352131 8 7 9577357...
output:
1488642079 1285672656 1231007258 1391102539 1457776250 1312296297 965883115 1067640282 1375010483 14...
result:
ok 1000 lines
Test #10:
score: 10
Accepted
time: 17ms
memory: 1288kb
input:
1000 1000 2 1 70426922 3 2 129023973 4 2 41311697 5 3 38222266 6 1 104922667 7 2 28427605 8 4 290279...
output:
1398920440 1392919513 1377745031 1577859072 1385104484 1650218024 1320502374 1382522476 1383167606 1...
result:
ok 1000 lines