QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#622394 | #6107. One Path | cjx | WA | 30ms | 4100kb | C++20 | 3.8kb | 2024-10-08 21:23:30 | 2024-10-08 21:23:31 |
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=2010;
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 dfn[N],num,eul[N<<1],red[N],nume,rnk[N],ted[N];
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]);
rnk[dfn[x]=++num]=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=num=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;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3656kb
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: 3624kb
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: 3900kb
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: 3680kb
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: 3904kb
input:
2 0 1 2 340241821
output:
340241821
result:
ok 1 number(s): "340241821"
Test #6:
score: -100
Wrong Answer
time: 1ms
memory: 4100kb
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:
120545921643
result:
wrong answer 1st numbers differ - expected: '83343544566', found: '120545921643'