QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#72281#4401. Prizechenshi0 0ms0kbC++1.8kb2023-01-15 12:09:252023-01-15 12:09:28

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-01-15 12:09:28]
  • 评测
  • 测评结果:0
  • 用时:0ms
  • 内存:0kb
  • [2023-01-15 12:09:25]
  • 提交

answer

#include<iostream>
#include<algorithm>
using namespace std;
const int o=1e6+10;
int n,K,q,T;
struct Edge{int v,p,w;};
struct Tree{
	int rt,fa[o],h[o],cnt,H[o],Cnt,tp[o],seq[o],dfn[o],s[o],hs[o],d[o],dep[o];bool vis[o];Edge e[o],E[o*4];
	inline void ad(int U,int V){e[++cnt].v=V;e[cnt].p=h[U];h[U]=cnt;}
	inline void Ad(int U,int V,int W){E[++Cnt].v=V;E[Cnt].p=H[U];E[H[U]=Cnt].w=W;}
	inline void add(int U,int V,int W){Ad(U,V,W);Ad(V,U,-W);}
	void Dfs(int nw){
		s[nw]=1;
		for(int i=h[nw];i;i=e[i].p)
			d[e[i].v]=d[nw]+1,Dfs(e[i].v),s[nw]+=s[e[i].v],hs[nw]=(s[hs[nw]]>s[e[i].v]?hs[nw]:e[i].v);
	}
	void dfs(int nw,int ld){
		tp[nw]=ld;seq[dfn[nw]=++cnt]=nw;
		if(hs[nw]) dfs(hs[nw],ld);
		for(int i=h[nw];i;i=e[i].p) if(e[i].v^hs[nw]) dfs(e[i].v,e[i].v);
	}
	inline void init(){
		for(int i=1;i<=n;++i){
			cin>>fa[i];
			if(fa[i]>0) ad(fa[i],i);
			else rt=i;
		}
		Dfs(rt);cnt=0;dfs(rt,rt);
	}
	inline int lca(int x,int y){
		for(;tp[x]^tp[y];x=fa[tp[x]]) if(d[tp[x]]<d[tp[y]]) swap(x,y);
		return (d[x]<d[y])?x:y;
	}
	void ddfs(int nw){vis[nw]=1;for(int i=H[nw];i;i=E[i].p) if(!vis[E[i].v]) dep[E[i].v]=dep[nw]+E[i].w,ddfs(E[i].v);}
	inline int dis(int x,int y){return dep[x]+dep[y]-dep[lca(x,y)]*2;}
}T1,T2;
inline bool cmp(int A,int B){return T2.dfn[A]<T2.dfn[B];}
int main(){
	cin>>n>>K>>q>>T;T1.init();T2.init();
	for(int i=1;i<=K;++i) cout<<T1.seq[i]<<" ";
	cout<<endl;
	sort(T1.seq+1,T1.seq+K+1,cmp);
	for(int i=1;i<K;++i) cout<<"? "<<T1.seq[i]<<" "<<T1.seq[i+1]<<"\n";
	cout<<"!"<<endl;
	for(int i=1,x,y,l1,l2,w;i<K;++i)
		x=T1.seq[i],y=T1.seq[i+1],l1=T1.lca(x,y),l2=T2.lca(x,y),
		cin>>w,T1.add(l1,x,w),cin>>w,T1.add(l1,y,w),cin>>w,T2.add(l2,x,w),cin>>w,T2.add(l2,y,w);
	T1.ddfs(T1.seq[1]);T2.ddfs(T1.seq[1]);
	for(int x,y;T--;cout<<T1.dis(x,y)<<" "<<T2.dis(x,y)<<"\n") cin>>x>>y;
	cout<<endl;
	return 0;
}

详细

Subtask #1:

score: 0
Time Limit Exceeded

Test #1:

score: 0
Time Limit Exceeded

input:

500000 64682 64681 100000
46115
470589
209303
2979
473162
343535
79503
299539
404621
102085
237721
279170
392890
165201
441593
456314
218991
358478
86614
410800
159785
169761
95368
285837
297549
370283
378974
26449
444381
39320
149913
404523
144109
174828
263837
49847
468694
478535
152644
216598
301...

output:

422989 414496 290928 388223 160563 301045 470257 259625 222733 231286 345214 169817 435263 277447 386014 210139 455433 225855 264772 199736 355788 288506 233893 146148 454958 267562 498596 183745 352665 151125 266374 43142 9414 204593 212097 311775 25324 300764 6643 94847 396968 428563 311355 255767...

result:


Subtask #2:

score: 0
Time Limit Exceeded

Test #13:

score: 0
Time Limit Exceeded

input:

500000 88721 177440 100000
30974
23891
211201
125199
180489
387190
218020
498838
230147
307989
484136
257785
353027
304420
311738
169842
334090
486070
126212
328609
174959
368840
238722
418092
488389
226349
427271
457322
332454
12958
197530
264474
355717
482774
221286
282148
216441
266659
213750
628...

output:

63742 11431 300071 157785 268420 71772 84553 267656 174540 21500 451751 82419 58833 165916 94199 78203 263216 146169 306934 50728 338250 199716 469441 135516 133967 123248 375309 17045 459156 413018 49645 73720 188292 322328 493921 152164 219927 140202 236207 266137 180568 32077 371348 66876 354136 ...

result:


Subtask #3:

score: 0
Time Limit Exceeded

Test #25:

score: 0
Time Limit Exceeded

input:

500000 200 199 40000
76296
130139
291501
292412
139543
433345
372726
451574
18315
465578
324564
477223
237354
81532
65170
465332
342130
9670
193303
193680
129668
149532
268907
89969
398275
356210
324593
433492
482232
466692
135343
433758
102545
287283
432859
351864
305769
489532
101532
450535
295762...

output:

20242 414878 185020 125537 353357 496468 308518 188057 254952 120898 414314 11748 435424 326112 345902 271794 473882 337923 135188 438050 45188 88306 260313 116954 457474 435919 366460 431766 397351 392326 178950 199724 227083 282259 70917 121346 109196 193669 242154 12225 466790 155481 287973 15749...

result:


Subtask #4:

score: 0
Time Limit Exceeded

Test #37:

score: 0
Time Limit Exceeded

input:

1000000 1000 999 100000
678746
439069
32542
85937
936926
284219
461661
203235
533462
940676
230275
621140
780674
254931
562355
229273
201341
493976
358955
963527
880412
91220
474599
160086
698841
591551
718276
844558
39859
765917
34722
401724
219774
443004
682244
545401
968419
968020
354030
411187
1...

output:

545967 706162 53597 107558 776536 230611 572458 293457 390487 241653 638541 42868 433774 438059 293014 739962 25440 503383 628342 573629 887812 909797 805385 862282 382785 706534 190319 439139 648412 626240 131005 848982 269684 840650 376086 933701 18720 749474 336321 160119 795534 671698 201133 610...

result:


Subtask #5:

score: 0
Skipped

Dependency #4:

0%