QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#114043 | #6350. MIT | Crysfly | WA | 2ms | 31372kb | C++17 | 3.5kb | 2023-06-20 17:53:33 | 2023-06-20 17:53:35 |
Judging History
answer
// what is matter? never mind.
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse,sse2,sse3,sse4,popcnt,abm,mmx,avx,avx2")
#include<bits/stdc++.h>
#define For(i,a,b) for(int i=(a);i<=(b);++i)
#define Rep(i,a,b) for(int i=(a);i>=(b);--i)
#define int long long
using namespace std;
inline int read()
{
char c=getchar();int x=0;bool f=0;
for(;!isdigit(c);c=getchar())f^=!(c^45);
for(;isdigit(c);c=getchar())x=(x<<1)+(x<<3)+(c^48);
if(f)x=-x;return x;
}
#define fi first
#define se second
#define pb push_back
#define mkp make_pair
typedef pair<int,int>pii;
typedef vector<int>vi;
#define maxn 100005
#define inf 0x3f3f3f3f
int n,m;
vector<pii>e[maxn];
int fa[maxn],dep[maxn],sz[maxn];
int dfn[maxn],que[maxn],out[maxn],dis[maxn],idx;
int st[20][maxn],lg[maxn];
int depmin(int u,int v){return dep[u]<dep[v]?u:v;}
void dfs(int u){
dfn[u]=++idx,que[idx]=u,sz[u]=1;
for(auto it:e[u]){
int v=it.fi,w=it.se;
if(v==fa[u])continue;
fa[v]=u,dep[v]=dep[u]+1,dis[v]=dis[u]+w;
dfs(v),sz[u]+=sz[v];
}out[u]=idx;
}
void init(){
dfs(1);
For(i,2,n)st[0][i]=fa[que[i]],lg[i]=lg[i>>1]+1;
For(j,1,19)
For(i,1,n-(1<<j)+1)
st[j][i]=depmin(st[j-1][i],st[j-1][i+(1<<(j-1))]);
}
int lca(int u,int v){
if(u==v)return u;
if((u=dfn[u])>(v=dfn[v]))swap(u,v); ++u;
int k=lg[v-u+1];
return depmin(st[k][u],st[k][v-(1<<k)+1]);
}
int dist(int u,int v){
if(!u||!v)return -((!u)+(!v));
return dis[u]+dis[v]-2*dis[lca(u,v)];
}
struct diam{
int u,v,d;
diam(int x=0,int y=0){u=x,v=y,d=dist(x,y);}
bool operator <(const diam&b)const{return d<b.d;}
void ins(int x){
if(!x)return;
int du=dist(u,x),dv=dist(v,x);
if(du<dv)swap(u,v),swap(du,dv);
if(du>d)v=x,d=du;
}
void operator +=(diam b){ins(b.u),ins(b.v);}
friend diam operator +(diam a,diam b){a+=b;return a;}
friend diam operator *(diam a,diam b){return max(max(diam(a.u,b.u),diam(a.v,b.v)),max(diam(a.u,b.v),diam(a.v,b.u)));}
};
diam tr[maxn<<2];
int leaf[maxn];
void up(int p){tr[p]=tr[p<<1]+tr[p<<1|1];}
void build(int p,int l,int r){
if(l==r)return tr[p]=diam(l,l),leaf[l]=p,void();
int mid=l+r>>1;
build(p<<1,l,mid),build(p<<1|1,mid+1,r),up(p);
}
void upd(int x){
int p=leaf[x];
tr[p]=diam(0,0);
while(p>>=1)up(p);
}
bool vis[maxn];
int res;
int rt,siz[maxn],mxp[maxn],allsz;
void getrt(int u,int pa){
siz[u]=vis[u],mxp[u]=0;
for(auto it:e[u]){
int v=it.fi,w=it.se;
if(v==pa)continue;
getrt(v,u),siz[u]+=siz[v],mxp[u]=max(mxp[u],siz[v]);
}
mxp[u]=max(mxp[u],allsz-siz[u]);
if(mxp[u]<=mxp[rt])rt=u;
}
signed main()
{
n=read();
For(i,2,n){
int u=read(),v=read(),w=read();
e[u].pb(mkp(v,w)),e[v].pb(mkp(u,w));
}
init();
build(1,1,n);
int x=tr[1].u,y=tr[1].v;
vis[x]=vis[y]=1,res=dist(x,y),mxp[0]=n+1;
upd(x),upd(y);
// cout<<"x,y "<<x<<" "<<y<<endl;
cout<<res<<" ";
rt=x;
For(i,3,n){
x=tr[1].u,y=tr[1].v;
// cout<<"i: "<<i<<" "<<rt<<endl;
int dx=dist(x,rt),dy=dist(y,rt);
if(dx<dy)swap(dx,dy),swap(x,y);
vis[x]=1,upd(x);
// cout<<"x: "<<x<<endl;
res+=dx;
int rrt=rt;
if(i%2!=0){
rt=0,allsz=i;
int mn=n+1,t;
Rep(_,n,1){
int u=que[_];
siz[_]+=vis[u];
mxp[_]=max(mxp[_],i-siz[_]);
if(mxp[_]<mn)rt=u,mn=mxp[_];
if(_>1){
t=dfn[fa[u]];
siz[t]+=siz[_];
mxp[t]=max(mxp[t],siz[_]);
}
siz[_]=mxp[_]=0;
}
rt=que[rt];
res-=dist(rt,rrt);
}
// cout<<"RT "<<rrt<<" "<<rt<<endl;
if(i%2==0) cout<<res<<" ";
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 24836kb
input:
7 1 3 99 2 3 82 3 4 4 4 5 43 5 6 5 4 7 3
output:
181 280 287
result:
ok 3 number(s): "181 280 287"
Test #2:
score: -100
Wrong Answer
time: 2ms
memory: 31372kb
input:
1000 328 12 58771429 881 504 17030494 847 622 20805817 12 922 58532669 879 929 52585738 495 726 27873950 855 17 37971674 797 941 76999719 702 610 719916 479 127 97829887 731 557 55300256 282 615 88802280 651 318 64099880 540 51 6547869 896 54 87823596 674 879 80674008 77 691 54802376 193 851 7869172...
output:
48829042195 97562386542 155990839003 229780454465 278250058875 340437040993 391807579935 439935108288 490160188158 540241124081 606407921369 693050309767 761675986231 816963079749 864243585865 931402342994 978424191229 1049587593065 1110331512499 1165168484444 1211890690155 1264864058466 13113506550...
result:
wrong answer 3rd numbers differ - expected: '146216474947', found: '155990839003'