QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#114034#6350. MITCrysflyTL 3402ms34604kbC++173.1kb2023-06-20 17:39:132023-06-20 17:39:14

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-06-20 17:39:14]
  • 评测
  • 测评结果:TL
  • 用时:3402ms
  • 内存:34604kb
  • [2023-06-20 17:39:13]
  • 提交

answer

// what is matter? never mind.
#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;
		rt=0,allsz=i,getrt(rrt,0);
		res-=dist(rt,rrt);
	//	cout<<"RT "<<rrt<<" "<<rt<<endl;
		if(i%2==0) cout<<res<<" ";
	}
	return 0;
}

详细

Test #1:

score: 100
Accepted
time: 1ms
memory: 23288kb

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: 0
Accepted
time: 17ms
memory: 31300kb

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 146216474947 194713456438 243120633399 291394542517 339657517459 387785045812 435787622310 483691355869 531476208289 579153483709 626793764008 674259794140 721617453738 768862531418 816032104456 863044440224 909960999950 956790589086 1003491332151 1050093110804 1096582356539 ...

result:

ok 500 numbers

Test #3:

score: 0
Accepted
time: 9ms
memory: 31260kb

input:

1000
739 878 29520635
133 940 5021186
160 908 26446479
441 122 80604853
539 396 95959331
635 860 46393560
387 172 58313059
53 442 65841670
121 159 59547874
35 264 28494605
269 78 29571243
436 384 89754669
619 47 52195144
57 336 95094232
936 419 88183176
877 634 14912042
47 100 57449297
533 101 61185...

output:

1335958866 2626054407 3904456915 5174845834 6437211717 7689455676 8928545787 10163335876 11395869036 12622043944 13839303826 15049898576 16256276325 17459646468 18659090162 19856778178 21048658276 22239285858 23421421030 24597109238 25772007715 26944410577 28111355238 29275440829 30439025973 3160110...

result:

ok 500 numbers

Test #4:

score: 0
Accepted
time: 3402ms
memory: 34468kb

input:

10000
8832 9937 83287693
1229 3010 26805846
5184 8703 32371496
5692 201 90600077
7857 7922 2427036
6991 1815 55936149
9126 8434 96181780
2395 5238 99604883
3721 3882 32707216
6961 5616 4158592
479 2786 52279400
9395 164 84498120
9470 462 16429465
8303 9717 36203661
4462 3119 77535638
9010 5633 83602...

output:

501483023048 1002862634577 1504180026111 2005449794525 2506536411562 3007537456615 3508449380203 4009328554633 4510105447462 5010869687212 5511538034705 6012089525686 6512463969594 7012812594398 7513027036015 8013205824561 8513282014876 9013342841924 9513374638228 10013328423574 10513136286282 11012...

result:

ok 5000 numbers

Test #5:

score: 0
Accepted
time: 968ms
memory: 34604kb

input:

10000
4492 5687 48649564
2101 358 76113540
8890 681 20477601
5658 2715 20252482
1495 646 86758431
3635 3432 68949767
4697 4340 46098280
63 5675 93748955
5761 7076 22517422
3184 9956 51745781
5532 8952 73420533
1284 4986 3427090
6181 8228 67376021
3115 7851 49040922
5609 4274 73495454
3082 7140 33903...

output:

1819011399 3610283149 5389719917 7165165127 8935910477 10688388373 12428866897 14143649215 15852590144 17553165073 19249717624 20945981402 22640112945 24322221048 26001998205 27679479690 29351460121 31019617475 32687276454 34352073284 36015148263 37676360382 39336662739 40988468898 42638462940 44286...

result:

ok 5000 numbers

Test #6:

score: -100
Time Limit Exceeded

input:

100000
24880 31580 94970384
94020 94021 53992679
93018 20941 73715500
35013 68974 24682681
84533 55066 72610856
9580 43559 96196239
80047 56875 25109065
83499 20829 59808314
97995 41133 22740692
23426 74367 65415289
57511 75315 31164442
36922 42744 52608303
83057 76938 87278645
55747 45197 84000571
...

output:

5010397956787 10020641445288 15030757289053 20040692351879 25050527457825 30060240975402 35069836607862 40079333710046 45088716306382 50098029965855 55107309142733 60116488016667 65125537215058 70134572054336 75143498785869 80152300239578 85160932266852 90169508996796 95177987864419 100186333199356 ...

result: