QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#116716#5363. ZYB 的游览计划1kri10 98ms19636kbC++142.0kb2023-06-29 21:18:092023-06-29 21:18:11

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-29 21:18:11]
  • 评测
  • 测评结果:10
  • 用时:98ms
  • 内存:19636kb
  • [2023-06-29 21:18:09]
  • 提交

answer

#include <iostream>
#include <cstdio>
#include <set> 
using namespace std;
int n,m,p[200005];
int u[400005],v[400005],first[200005],nxt[400005];
int f[2005][2005];
int fa[200005],depth[200005],sz[200005],son[200005];
int top[200005],idx,dfn[200005],nfd[200005];
void dfs1(int now,int f,int d){
	fa[now]=f,depth[now]=d;
	sz[now]=1;
	for (int i=first[now];i;i=nxt[i])
		if (v[i]!=f){
			dfs1(v[i],now,d+1);
			sz[now]+=sz[v[i]];
			if (son[now]==0||sz[v[i]]>sz[son[now]])son[now]=v[i];
		}
	return;
}
void dfs2(int now,int f,int t){
	top[now]=t;
	++idx;
	dfn[now]=idx,nfd[idx]=now;
	if (son[now]!=0)dfs2(son[now],now,t);
	for (int i=first[now];i;i=nxt[i])
		if (v[i]!=f&&v[i]!=son[now])dfs2(v[i],now,v[i]);
	return;
}
int lca(int a,int b){
	if (a==0||b==0)return 0;
	while(top[a]!=top[b]){
		if (depth[top[a]]<depth[top[b]])swap(a,b);
		a=fa[top[a]];
	}
	if (depth[a]>depth[b])return b;
	return a;
}
int dis(int a,int b){
	return depth[a]+depth[b]-2*depth[lca(a,b)];
}
namespace DS{
	set<int> c;
	int sum;
	void init(){
		c.clear();
		c.insert(0),c.insert(n+1);
		sum=0;
		return;
	}
	void ins(int x){
		int l,r;
		set<int>::iterator it=c.lower_bound(dfn[x]);
		r=(*it);
		it--;
		l=(*it);
		sum-=dis(nfd[l],nfd[r]);
		sum+=dis(nfd[l],x);
		sum+=dis(x,nfd[r]);
		c.insert(dfn[x]);
		return;
	}
	void del(int x){
		c.erase(dfn[x]);
		int l,r;
		set<int>::iterator it=c.lower_bound(dfn[x]);
		r=(*it);
		it--;
		l=(*it);
		sum+=dis(nfd[l],nfd[r]);
		sum-=dis(nfd[l],x);
		sum-=dis(x,nfd[r]);
		return;
	}
}
int main(){
	scanf("%d%d",&n,&m);
	for (int i=1;i<=n;i++)scanf("%d",&p[i]);
	for (int i=1;i<n;i++){
		scanf("%d%d",&u[i],&v[i]);
		nxt[i]=first[u[i]],first[u[i]]=i;
		u[i+n]=v[i],v[i+n]=u[i];
		nxt[i+n]=first[u[i+n]],first[u[i+n]]=i+n; 
	}
	dfs1(1,0,0);
	dfs2(1,0,1);
	for (int i=1;i<=n;i++){
		DS::init();
		for (int j=i;j>=1;j--){
			DS::ins(p[j]);
			for (int k=1;k<=m;k++)
				f[i][k]=max(f[i][k],f[j-1][k-1]+DS::sum);
		}
	}
	cout<<f[n][m]<<endl;
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Subtask #1:

score: 0
Runtime Error

Test #1:

score: 5
Accepted
time: 3ms
memory: 13668kb

input:

5 2
2 4 3 5 1
1 2
2 3
3 4
4 5

output:

14

result:

ok single line: '14'

Test #2:

score: -5
Runtime Error

input:

90752 2
88555 48815 61754 47133 45462 73740 84783 58077 73713 78537 14562 78533 71607 24890 16284 41187 78450 31531 25296 45169 55240 83197 82146 86338 83180 75924 9923 40553 84394 73069 7278 77214 24896 14446 87566 70680 48218 58608 86578 47698 86173 89371 77350 85782 36318 22735 80925 5435 76359 2...

output:


result:


Subtask #2:

score: 10
Accepted

Test #10:

score: 10
Accepted
time: 54ms
memory: 17148kb

input:

760 217
632 417 493 400 316 482 542 614 36 134 604 291 745 484 451 746 518 378 487 650 633 223 601 259 33 257 309 683 705 627 513 670 130 395 512 115 466 376 575 356 180 716 539 403 431 563 568 468 596 239 296 379 537 224 526 215 725 234 663 603 401 21 75 660 506 393 105 88 462 620 449 338 276 200 4...

output:

35938

result:

ok single line: '35938'

Test #11:

score: 0
Accepted
time: 57ms
memory: 17432kb

input:

919 16
836 94 652 285 192 830 643 430 855 825 844 852 750 773 835 743 310 59 115 589 187 831 277 35 577 683 348 611 459 590 196 531 609 575 754 729 542 502 19 890 695 433 445 678 221 901 140 525 441 689 510 423 709 568 894 802 552 474 878 653 51 866 916 123 2 167 190 302 226 769 658 818 351 757 400 ...

output:

5682

result:

ok single line: '5682'

Test #12:

score: 0
Accepted
time: 45ms
memory: 16812kb

input:

727 116
382 218 586 528 66 508 58 216 234 251 629 222 403 356 596 577 92 221 167 84 417 226 595 388 127 341 656 715 180 71 67 429 25 308 194 494 144 196 675 556 201 289 711 727 594 304 188 330 581 517 471 441 462 254 654 203 159 472 273 280 442 603 677 650 317 389 545 265 475 499 362 15 686 239 205 ...

output:

40024

result:

ok single line: '40024'

Test #13:

score: 0
Accepted
time: 78ms
memory: 19636kb

input:

961 67
180 513 138 39 787 472 499 847 561 275 602 804 331 191 492 75 718 287 647 724 323 224 436 442 389 605 250 679 59 19 746 177 692 461 115 786 289 267 424 58 952 541 422 397 401 484 273 445 660 925 806 789 744 553 314 435 253 209 530 462 594 795 745 644 281 427 637 141 808 76 378 351 348 464 35 ...

output:

13302

result:

ok single line: '13302'

Test #14:

score: 0
Accepted
time: 98ms
memory: 17884kb

input:

979 185
384 598 917 477 487 141 460 529 299 53 462 614 238 772 535 933 915 588 789 816 710 660 866 158 83 139 69 473 965 908 434 733 582 132 318 722 880 18 478 426 154 262 809 216 576 781 375 170 736 130 765 753 122 456 526 373 734 222 425 693 595 877 818 482 114 68 263 308 891 390 845 320 447 922 3...

output:

11084

result:

ok single line: '11084'

Test #15:

score: 0
Accepted
time: 26ms
memory: 16532kb

input:

515 312
449 338 205 182 444 131 496 370 300 32 343 143 475 133 43 172 385 266 283 227 228 458 470 469 196 399 136 329 146 218 367 83 30 304 99 158 26 268 435 1 206 494 324 7 140 506 155 113 112 162 219 109 393 70 490 166 359 231 251 233 277 373 222 246 501 229 429 169 12 473 316 313 264 122 168 398 ...

output:

47230

result:

ok single line: '47230'

Test #16:

score: 0
Accepted
time: 58ms
memory: 17232kb

input:

771 119
83 275 752 576 319 581 324 287 496 137 114 73 541 100 418 488 120 701 234 539 554 677 194 91 561 369 171 230 301 632 389 211 655 157 208 586 606 377 169 724 364 714 538 743 749 407 599 193 311 233 65 550 640 513 135 615 1 421 354 108 726 633 290 682 41 270 22 302 115 670 614 678 246 605 509 ...

output:

12334

result:

ok single line: '12334'

Test #17:

score: 0
Accepted
time: 94ms
memory: 18376kb

input:

971 164
241 934 565 457 914 803 892 300 927 270 769 177 930 250 701 287 623 510 915 164 483 385 29 438 98 431 145 715 855 337 408 303 506 758 795 700 396 495 290 531 235 719 570 174 346 339 898 607 295 808 10 496 561 875 931 155 479 94 275 317 368 361 669 788 366 175 801 709 908 364 616 804 965 412 ...

output:

58248

result:

ok single line: '58248'

Test #18:

score: 0
Accepted
time: 52ms
memory: 17000kb

input:

727 189
725 237 275 99 597 563 257 436 396 342 397 299 611 74 395 701 594 252 357 619 428 115 439 229 339 223 91 344 555 484 97 8 722 133 677 338 2 704 254 265 271 302 231 557 467 391 715 244 498 421 235 112 367 487 29 368 365 469 547 595 60 635 141 171 380 156 170 377 613 316 453 708 44 258 372 452...

output:

33166

result:

ok single line: '33166'

Subtask #3:

score: 0
Runtime Error

Dependency #2:

100%
Accepted

Test #19:

score: 0
Runtime Error

input:

5888 5
1718 907 2359 17 692 2242 39 1614 444 5711 2566 894 4118 3472 3556 2148 616 2871 1299 2933 1836 3376 2157 4664 4793 3123 1129 1074 521 2266 2864 5201 936 5803 4916 5196 1904 1427 4921 2065 2833 3131 3107 1555 2512 5250 4887 4839 3986 3143 4554 4304 4556 3166 1705 3365 3929 2879 4693 3086 4382...

output:


result:


Subtask #4:

score: 0
Runtime Error

Test #29:

score: 0
Runtime Error

input:

14878 6
1663 4532 2022 11114 1768 7744 12403 7698 14863 1406 13199 9405 3528 9898 1205 3076 11342 7459 9401 10025 14498 7178 11457 1802 9923 1648 13136 10720 3002 7332 13780 2094 1716 13215 8118 318 11186 14833 7941 8174 8999 6189 7504 13738 4933 3367 12973 1889 9835 4080 3546 1993 1861 11613 2504 1...

output:


result:


Subtask #5:

score: 0
Skipped

Dependency #1:

0%