QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#622409 | #6107. One Path | cjx | WA | 498ms | 4444kb | C++20 | 3.8kb | 2024-10-08 21:29:52 | 2024-10-08 21:29:53 |
Judging History
answer
#include<bits/stdc++.h>
#define ll long long
#define pii pair<int,int>
#define fi first
#define se second
using namespace std;
long long read(){
long long x=0,f=1;char ch=getchar();
while(!isdigit(ch))
{if(ch=='-') f=-1;ch=getchar();}
while(isdigit(ch)){x=x*10+ch-48;ch=getchar();}
return x*f;
}
void write(long long x){
if(x<0) putchar('-'),x=-x;
if(x>9) write(x/10);
putchar(x%10+'0');
}
const int N=4010;
int n,k;
int head[N],ver[N<<1],nxt[N<<1],tot;
ll edge[N<<1],dep[N];
set<pair<pii,ll> >sedge;
void add(int x,int y,ll z){
ver[++tot]=y,edge[tot]=z,nxt[tot]=head[x],head[x]=tot;
}
int eul[N<<1],red[N<<1],nume;
int mu,mv,mx,my;
ll mw,me;
struct ST{
int f[N<<1][14];
int Log[N<<1];
int op(int x,int y){
return dep[x]<dep[y]?x:y;
}
void init(int n,int *a){
Log[0]=-1;
for(int i=1;i<=n;i++)f[i][0]=a[i],Log[i]=Log[i>>1]+1;
for(int i=1;i<14;i++)
for(int j=1;j+(1<<i)-1<=n;j++)
f[j][i]=op(f[j][i-1],f[j+(1<<(i-1))][i-1]);
}
int ask(int l,int r){
int k=Log[r-l+1];
return op(f[l][k],f[r-(1<<k)+1][k]);
}
}st;
void dfs1(int x,int fa){
if(x==1)dep[x]=0;
//printf("dep[%d]=%lld\n",x,dep[x]);
red[eul[x]=++nume]=x;
for(int i=head[x];i;i=nxt[i]){
int y=ver[i];
if(y==fa)continue;
dep[y]=dep[x]+edge[i];
dfs1(y,x);
red[++nume]=x;
}
}
ll dist(int x,int y){
return dep[x]+dep[y]-(dep[st.ask(min(eul[x],eul[y]),max(eul[x],eul[y]))]<<1);
}
struct Diam{
int x,y;
ll z;
Diam(int xx=0,int yy=0){
x=xx,y=yy;
if(x)z=dist(x,y);else z=-1;
}
bool operator <(const Diam p)const{
return z<p.z;
}
}f[N];
Diam operator +(const Diam p,const Diam q){
return max(max(p,q),max(max(Diam(p.x,q.x),Diam(p.x,q.y)),max(Diam(p.y,q.x),Diam(p.y,q.y))));
}
void dfs2(int x,int fa){
f[x]=Diam(x,x);
for(int i=head[x];i;i=nxt[i]){
int y=ver[i];
if(y==fa)continue;
dfs2(y,x);
f[x]=f[x]+f[y];
}
//printf("f[%d]=(%d,%d) %lld\n",x,f[x].x,f[x].y,f[x].z);
}
void init(){
memset(head,0,sizeof(head));
tot=nume=0;
for(auto u:sedge){
add(u.fi.fi,u.fi.se,u.se);add(u.fi.se,u.fi.fi,u.se);
}
dfs1(1,0);
st.init(nume,red);
dfs2(1,0);
mu=mv=mx=my=me=0;mw=-1;
}
void dfs3(int x,int fa){
vector<int>son;
vector<ll>val;
vector<Diam>pre,suf;
son.clear();pre.clear();suf.clear();
for(int i=head[x];i;i=nxt[i]){
int y=ver[i];
son.push_back(y);
val.push_back(edge[i]);
pre.push_back(f[y]);
suf.push_back(f[y]);
}
for(int i=1;i<son.size();i++){
pre[i]=pre[i-1]+pre[i];
}
for(int i=son.size()-2;i>=0;i--)
suf[i]=suf[i+1]+suf[i];
Diam now,e;
for(int i=0;i<son.size();i++){
int y=son[i];
if(y==fa)continue;
now=Diam(x,x);
if(i>0)now=now+pre[i-1];
if(i<son.size()-1)now=now+suf[i+1];
f[x]=now;e=f[y];
ll w=f[x].z+f[y].z+val[i];
if(w>mw){
mw=w;mu=x;mv=y;me=val[i];mx=f[x].x;my=f[y].y;
}
f[y]=f[y]+now;
dfs3(y,x);
f[y]=e;
}
}
int main(){
//freopen(".in","r",stdin);
//freopen(".out","w",stdout);
n=read();k=read();
for(int i=1;i<n;i++){
pair<pii,ll> u;
u.fi.fi=read();u.fi.se=read();u.se=read();
sedge.insert(u);
}
init();
write(f[1].z);putchar(' ');
while(k--){
dfs3(1,0);
write(mw);putchar(' ');
if(mu>mv)swap(mu,mv);
if(mx>my)swap(mx,my);
sedge.erase(make_pair(make_pair(mu,mv),me));
sedge.insert(make_pair(make_pair(mx,my),me));
init();
}
puts("");
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 1ms
memory: 3740kb
input:
5 1 1 3 2 4 5 4 3 4 3 2 3 7
output:
14 16
result:
ok 2 number(s): "14 16"
Test #2:
score: 0
Accepted
time: 0ms
memory: 3772kb
input:
7 2 1 2 4 2 3 6 2 4 2 4 5 5 2 6 1 4 7 3
output:
13 20 21
result:
ok 3 number(s): "13 20 21"
Test #3:
score: 0
Accepted
time: 30ms
memory: 3716kb
input:
50 2000 3 34 1 37 39 58720256 17 24 14680064 28 39 1 25 38 1 21 29 1 3 30 1 26 36 1 5 48 14336 4 22 1 26 41 1 41 44 1 5 14 1 23 25 28672 40 41 224 27 39 1 4 20 7340032 7 47 939524096 11 46 114688 30 49 3584 34 44 1 7 35 1 10 29 1 27 33 29360128 16 36 56 8 28 1 13 38 57344 34 45 896 15 35 469762048 1...
output:
1409286145 1761607683 1849688069 1871708167 1877213193 1878589451 1878933517 1879019535 1879041041 1879046419 1879047765 1879048103 1879048160 1879048160 1879048160 1879048160 1879048160 1879048160 1879048160 1879048160 1879048160 1879048160 1879048160 1879048160 1879048160 1879048160 1879048160 187...
result:
ok 2001 numbers
Test #4:
score: 0
Accepted
time: 0ms
memory: 3940kb
input:
10 5 1 4 10 3 7 138 1 9 162 4 10 113 4 6 12 5 6 171 2 10 31 7 8 12 7 10 132
output:
566 769 781 781 781 781
result:
ok 6 numbers
Test #5:
score: 0
Accepted
time: 0ms
memory: 3740kb
input:
2 0 1 2 340241821
output:
340241821
result:
ok 1 number(s): "340241821"
Test #6:
score: 0
Accepted
time: 1ms
memory: 4204kb
input:
2000 0 450 1620 747893383 103 1602 466748018 746 1996 468886757 430 764 455748265 1201 1275 111041658 244 802 715844971 611 899 125646661 1105 1633 478144612 180 573 823370712 161 1018 67225549 1512 1915 538711443 829 1871 761057722 1297 1499 576790037 1492 1832 983172784 1486 1902 78076400 1206 121...
output:
83343544566
result:
ok 1 number(s): "83343544566"
Test #7:
score: 0
Accepted
time: 1ms
memory: 3728kb
input:
2 2000 1 2 128840350
output:
128840350 128840350 128840350 128840350 128840350 128840350 128840350 128840350 128840350 128840350 128840350 128840350 128840350 128840350 128840350 128840350 128840350 128840350 128840350 128840350 128840350 128840350 128840350 128840350 128840350 128840350 128840350 128840350 128840350 128840350 ...
result:
ok 2001 numbers
Test #8:
score: -100
Wrong Answer
time: 498ms
memory: 4444kb
input:
1139 1252 115 643 947372561 358 529 121247876 22 177 680088711 70 692 912515870 602 1032 172209848 908 1064 871258945 1052 1085 268011860 88 405 978751838 65 913 486052321 75 496 113634888 654 841 834034656 209 409 142094069 674 700 147589677 828 885 666263686 486 685 480409259 111 839 151780996 634...
output:
59768151302 90240365913 109345390588 124644045513 137837866321 149756278857 160389594579 169052188193 177682485014 186307692639 194915965611 202174583609 208536512850 214809947180 220700539079 226377386494 231954748593 237449390886 242862314574 248250365715 253297910824 258171191426 262962609442 267...
result:
wrong answer 3rd numbers differ - expected: '110773200826', found: '109345390588'