QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#672373#3047. Wind of ChangePNNNNWA 4211ms74156kbC++174.4kb2024-10-24 16:37:512024-10-24 16:37:51

Judging History

你现在查看的是最新测评结果

  • [2024-10-24 16:37:51]
  • 评测
  • 测评结果:WA
  • 用时:4211ms
  • 内存:74156kb
  • [2024-10-24 16:37:51]
  • 提交

answer

#include<bits/stdc++.h>
#define int long long
using namespace std;

const int inf=0x3f3f3f3f3f3f3f3f;

int n,ans[250005];

struct DFZ{
	int crt,siz[250005],vis[250005],sz;
	struct line{
		int y,w;
	};vector <line> to[250005];
	inline void rana(int x,int last){
		siz[x]=1;int maxn=0;
		for(auto [y,w]:to[x]){
			if(vis[y]||y==last)continue;
			rana(y,x);if(crt)return;
			siz[x]+=siz[y],maxn=max(maxn,siz[y]);
		}maxn=max(maxn,sz-siz[x]);
		if(maxn<=sz/2){
			crt=x,siz[last]=sz-siz[x];
		}return;
	}
	inline void find(int x,int last){
		crt=0,rana(x,last);return;
	}
}T1,T2;

int dis[250005];

namespace SP{
	int dep[250005],fa[250005],siz[250005],mxs[250005],top[250005],dfn[250005],cnt;
	inline void dfs1(int x,int last){
		dep[x]=dep[last]+1,fa[x]=last,siz[x]=1;
		for(auto [y,w]:T2.to[x]){
			if(y==last)continue;
			dis[y]=dis[x]+w,dfs1(y,x),siz[x]+=siz[y];
			if(siz[y]>siz[mxs[x]])mxs[x]=y;
		}return;
	}
	inline void dfs2(int x,int last){
		dfn[x]=++cnt,top[x]=last;
		if(!mxs[x])return;dfs2(mxs[x],last);
		for(auto [y,w]:T2.to[x]){
			if(y!=fa[x]&&y!=mxs[x])dfs2(y,y);
		}return;
	}
	inline int lca(int x,int y){
		while(top[x]!=top[y]){
			if(dep[top[x]]<dep[top[y]])swap(x,y);
			x=fa[top[x]];
		}if(dep[x]>dep[y])swap(x,y);return x;
	}
	inline void init(){
		dfs1(1,0),dfs2(1,1);
		for(int i=1;i<=n;i++){
			T2.to[i].clear();
		}return;
	}
}


//点分治其二
int val[250005],tag[250005],minn;

struct node{
    int x,w;
};

inline bool operator <(const node A,const node B){return A.w<B.w;}

inline void push(int x,int last,int dist){
    if(tag[x])minn=min(minn,val[x]+dist);
	for(auto [y,w]:T2.to[x]){
		if(!T2.vis[y]&&y!=last)push(y,x,dist+w);
	}return;
}

inline void update(int x,int last,int dist){
    if(tag[x])ans[x]=min(ans[x],minn+val[x]+dist);
	for(auto [y,w]:T2.to[x]){
		if(!T2.vis[y]&&y!=last)update(y,x,dist+w);
	}return;
}

inline void calc(int x){
	T2.vis[x]=1;

    minn=inf;
    for(auto [y,w]:T2.to[x]){
		if(!T2.vis[y])push(y,x,w);
	}
    if(tag[x])ans[x]=min(ans[x],minn+val[x]);

    minn=(tag[x]?val[x]:minn);
	for(int i=0;i<T2.to[x].size();i++){
        int y=T2.to[x][i].y,w=T2.to[x][i].w;
        if(T2.vis[y])continue;
        update(y,x,w),push(y,x,w);
    }

    minn=(tag[x]?val[x]:minn);
    for(int i=T2.to[x].size()-1;i>=0;i--){
        int y=T2.to[x][i].y,w=T2.to[x][i].w;
        if(T2.vis[y])continue;
        update(y,x,w),push(y,x,w);
    }

	for(auto [y,w]:T2.to[x]){
		if(T2.vis[y])continue;
		T2.sz=T2.siz[y],T2.find(y,x),calc(T2.crt);
	}return;
}

//虚树
vector <int> vec;

int tmp[500005];

inline bool cmp(int x,int y){return SP::dfn[x]<SP::dfn[y];}

inline void build(){
	sort(vec.begin(),vec.end(),cmp),tmp[0]=0;
	for(int i=0;i<vec.size();i++){
		tmp[++tmp[0]]=vec[i],tag[vec[i]]=1;
		if(i+1<vec.size())tmp[++tmp[0]]=SP::lca(vec[i],vec[i+1]); 
	}
	sort(tmp+1,tmp+1+tmp[0],cmp);
	tmp[0]=unique(tmp+1,tmp+1+tmp[0])-tmp-1;
	for(int i=1;i<tmp[0];i++){
        T2.vis[tmp[i]]=T2.vis[tmp[i+1]]=0;
		int LCA=SP::lca(tmp[i],tmp[i+1]);
		T2.to[LCA].push_back({tmp[i+1],dis[tmp[i+1]]-dis[LCA]});
		T2.to[tmp[i+1]].push_back({LCA,dis[tmp[i+1]]-dis[LCA]});
	}
	T2.sz=tmp[0],T2.find(tmp[1],0),calc(T2.crt);
	for(int x:vec)tag[x]=0;
	for(int i=1;i<tmp[0];i++){
		T2.to[SP::lca(tmp[i],tmp[i+1])].clear();
		T2.to[tmp[i+1]].clear();
	}return;
}

//点分治其一
inline void insert(int x,int last,int dist){
	vec.push_back(x),val[x]=dist;
	for(auto [y,w]:T1.to[x]){
		if(!T1.vis[y]&&y!=last)insert(y,x,dist+w);
	}return;
}

inline void solve(int x){
	T1.vis[x]=1,vec={x},val[x]=0;
	for(auto [y,w]:T1.to[x]){
		if(!T1.vis[y])insert(y,x,w);
	}
    build();
	for(auto [y,w]:T1.to[x]){
		if(T1.vis[y])continue;
		T1.sz=T1.siz[y];
        T1.find(y,x),solve(T1.crt);
	}return;
}

inline int read(){
	register int x(0),t(0);
	static char ch=getchar();
	while(!isdigit(ch)){t|=(ch=='-');ch=getchar();}
	while(isdigit(ch)){x=(x<<1)+(x<<3)+(ch^48);ch=getchar();}
	return t?-x:x;
}

signed main(){

	n=read();
    for(int i=1;i<=n;i++){
        ans[i]=inf;
    }
	for(int i=1;i<n;i++){
		int x=read(),y=read(),w=read();
		T1.to[x].push_back({y,w}),T1.to[y].push_back({x,w});
	}
	for(int i=1;i<n;i++){
		int x=read(),y=read(),w=read();
		T2.to[x].push_back({y,w}),T2.to[y].push_back({x,w});
	}
	SP::init(),T1.sz=n,T1.find(1,0),solve(T1.crt);
	
    for(int i=1;i<=n;i++){
        cout<<ans[i]<<'\n';
    }
	
	return 0;
}

详细

Test #1:

score: 0
Wrong Answer
time: 4211ms
memory: 74156kb

input:

250000
137466 116241 624409157
188961 163586 148491970
13958 122239 46409636
186120 44010 919858866
72320 102560 92679302
158544 236582 882613876
22331 114267 646741536
210782 42861 679450428
56693 45397 898958944
71188 114724 904191407
15203 225401 210626781
31071 144225 278121829
110354 83972 4557...

output:

41101981722
24783524958
17386222407
32630055254
15765403308
20965687585
28581128255
7850606344
4333398222
30625084420
25595872600
26129987591
4056530066
5868557730
6899770122
4822048816
16607032384
14181900087
2746624116
31954709190
9721549372
4572350990
13009044790
28220426146
26855078292
124065150...

result:

wrong answer 4th lines differ - expected: '38045922430', found: '32630055254'