UOJ Logo

NOI.AC

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#207167#3730. 路径覆盖Josephcheng1007157ms37436kbC++112.0kb2024-07-27 18:33:562024-07-27 20:09:43

answer

#include<bits/stdc++.h>
#define N 200005
using namespace std;

int n,m,pos,u,v,tot;
int head[N],fa[N],dfn[N],siz[N],tr[N],dep[N],anc[25][N],cnt[N];
bool vis[N];

struct Edge
{
	int u,v,nxt;
}e[N<<1];

void addEdge(int u,int v)
{
	e[++pos]={u,v,head[u]};
	head[u]=pos;
}

void dfs1(int t,int deep)
{
	dep[t]=deep;
	for(int i=head[t];i;i=e[i].nxt){
		int v=e[i].v;
		if(!vis[v]){
			vis[v]=true;
			dfs1(v,deep+1);
			fa[v]=t;
		}
	}
}

void dfs2(int t)
{
	siz[t]=1;
	dfn[t]=++tot;
	for(int i=head[t];i;i=e[i].nxt){
		int v=e[i].v;
		if(!vis[v]){
			vis[v]=true;
			dfs2(v);
			siz[t]+=siz[v];
		}
	}
}

int lowbit(int a){return a&(-a);}

void add(int t,int val)
{
	if(t==0) return ;
	for(int i=t;i<=n;i+=lowbit(i))
		tr[i]+=val;
}

int sum(int t)
{
	int res=0;
	for(int i=t;i;i-=lowbit(i))
		res+=tr[i];
	return res;
}

int query(int t)
{
	return sum(dfn[t]+siz[t]-1)-sum(dfn[t]-1);
}

void init()
{
	for(int i=1;i<=n;i++)
		anc[0][i]=fa[i];
	for(int p=1;p<=21;p++)
		for(int i=1;i<=n;i++)
			anc[p][i]=anc[p-1][anc[p-1][i]];
}

int LCA(int u,int v)
{
	if(dep[u]<dep[v]) swap(u,v);
	for(int p=21;p>=0;p--)
		if(dep[u]-(1<<p)>=dep[v])
			u=anc[p][u];
	if(u==v) return u;
	for(int p=21;p>=0;p--)
		if(anc[p][u]!=anc[p][v])
			u=anc[p][u],v=anc[p][v];
	return fa[u];
}

int main()
{
//	freopen("1.in","r",stdin);
	scanf("%d%d",&n,&m);
	for(int i=1;i<n;i++){
		scanf("%d%d",&u,&v);
		addEdge(u,v);
		addEdge(v,u);
	}
	vis[1]=true;dfs1(1,1);
	memset(vis,false,sizeof vis);
	vis[1]=true;dfs2(1);
	for(int i=1;i<=n;i++)
		add(i,0);
	init();
	for(int i=1;i<=m;i++){
		scanf("%d%d",&u,&v);
		int w=LCA(u,v);
//		printf("[&]:%d %d %d\n",u,v,w);
		if(w==u||w==v){
			int z;
			if(u!=w) z=u;
			else z=v;
			add(dfn[z],1);
			add(dfn[w],-1);
		}else{
			add(dfn[u],1);
			add(dfn[v],1);
			add(dfn[w],-2);
		}
//		for(int i=1;i<=n;i++)
//			printf("%d ",query(i));
//		puts("");
	}
	for(int i=2;i<=n;i++)
		cnt[query(i)]++;
	for(int i=0;i<=m;i++)
		printf("%d ",cnt[i]);
}

Details

小提示:点击横条可展开更详细的信息

Test #1:

score: 5
Accepted
time: 2ms
memory: 1632kb

input:

1000 1000
177 510
837 486
186 514
879 576
539 998
726 753
2 299
285 688
215 231
119 515
182 691
784 ...

output:

45 123 120 92 75 50 41 30 22 15 27 15 16 9 10 8 3 6 13 7 5 5 7 5 4 6 1 2 4 5 7 6 2 4 4 2 3 0 2 1 3 1...

result:

ok 1001 tokens

Test #2:

score: 5
Accepted
time: 0ms
memory: 1628kb

input:

1000 1000
805 382
148 659
394 856
487 66
879 291
857 494
719 202
141 780
455 246
83 312
563 4
822 47...

output:

40 122 140 110 59 55 42 31 25 23 13 12 8 10 17 12 8 13 10 6 9 6 4 8 2 1 2 4 5 3 3 4 6 3 4 3 0 1 1 1 ...

result:

ok 1001 tokens

Test #3:

score: 5
Accepted
time: 0ms
memory: 1632kb

input:

1000 1000
364 785
443 330
676 952
200 930
220 1000
716 317
826 696
79 509
272 158
271 949
267 188
28...

output:

58 116 104 103 78 62 44 19 27 32 15 11 14 12 15 7 7 10 8 4 7 4 5 4 2 2 2 1 2 5 5 1 6 1 3 4 3 6 2 4 2...

result:

ok 1001 tokens

Test #4:

score: 5
Accepted
time: 0ms
memory: 1628kb

input:

1000 1000
975 265
634 600
924 62
898 506
189 380
587 540
959 836
200 542
859 558
929 214
252 958
908...

output:

66 93 102 102 70 40 42 31 20 20 18 12 19 18 13 13 6 3 9 11 7 5 6 9 7 5 7 4 5 5 3 1 7 1 4 1 5 1 2 5 2...

result:

ok 1001 tokens

Test #5:

score: 5
Accepted
time: 0ms
memory: 1632kb

input:

1000 1000
263 358
162 528
555 642
304 627
788 315
749 516
451 64
111 977
222 135
670 589
623 148
250...

output:

46 110 99 110 79 44 46 35 20 25 21 17 10 12 13 12 9 3 11 5 5 7 5 9 3 6 10 1 3 4 8 5 2 8 4 1 2 2 2 0 ...

result:

ok 1001 tokens

Test #6:

score: 5
Accepted
time: 1ms
memory: 1632kb

input:

1000 1000
693 558
223 100
12 316
364 608
149 604
793 80
4 749
415 543
640 840
849 776
27 813
619 798...

output:

51 95 127 105 68 53 41 37 22 21 18 16 17 10 14 13 12 8 6 6 4 6 8 6 5 3 4 9 7 4 5 3 2 2 3 2 1 5 4 3 2...

result:

ok 1001 tokens

Test #7:

score: 5
Accepted
time: 297ms
memory: 28476kb

input:

200000 200000
152924 133355
132784 159949
48163 16199
107570 97541
108052 7611
121634 61497
40491 18...

output:

10512 22056 24296 20427 14769 10544 7761 6236 4996 4260 3672 3161 2627 2420 2194 1921 1758 1636 1487...

result:

ok 200001 tokens

Test #8:

score: 5
Accepted
time: 436ms
memory: 28412kb

input:

200000 200000
147949 131398
138160 49746
60026 2627
110862 120595
62030 171586
162532 147784
9808 16...

output:

10402 21912 24583 20104 14665 10469 7785 6218 5122 4154 3569 3107 2720 2361 2228 1899 1802 1574 1418...

result:

ok 200001 tokens

Test #9:

score: 5
Accepted
time: 578ms
memory: 28436kb

input:

200000 200000
18246 146321
152738 59315
13160 121883
79818 99769
172133 53584
44172 71493
152611 922...

output:

10546 22129 24732 19896 14755 10456 7727 6217 5014 4257 3624 3038 2715 2358 2152 1960 1779 1616 1439...

result:

ok 200001 tokens

Test #10:

score: 5
Accepted
time: 393ms
memory: 28484kb

input:

200000 200000
177068 157332
161550 30570
34766 134765
130545 145981
49533 61233
120580 103500
120552...

output:

10366 22004 24521 20243 14685 10480 7948 6143 4970 4263 3606 3154 2687 2431 2210 1936 1811 1667 1481...

result:

ok 200001 tokens

Test #11:

score: 5
Accepted
time: 562ms
memory: 28460kb

input:

200000 200000
81290 157046
174387 104800
168076 137362
101422 158473
114059 106215
172207 45206
6535...

output:

10421 22218 24402 20067 14460 10638 7833 6254 5062 4399 3611 3157 2781 2455 2173 1972 1815 1610 1533...

result:

ok 200001 tokens

Test #12:

score: 5
Accepted
time: 567ms
memory: 28472kb

input:

200000 200000
80458 68136
78172 113909
67739 14907
163591 44122
47776 5528
8314 105652
144363 48914
...

output:

10483 22007 24570 19994 14649 10494 7858 6259 5105 4284 3559 3138 2846 2411 2197 1982 1801 1602 1518...

result:

ok 200001 tokens

Test #13:

score: 5
Accepted
time: 602ms
memory: 28472kb

input:

200000 200000
126535 165361
110140 165275
187945 23092
125642 132277
47001 160386
160529 654
196022 ...

output:

10568 22042 24353 20150 14725 10323 7947 6218 5050 4259 3611 3143 2699 2398 2127 1981 1784 1655 1486...

result:

ok 200001 tokens

Test #14:

score: 5
Accepted
time: 579ms
memory: 28440kb

input:

200000 200000
124605 132337
37126 155194
177080 45019
62577 123915
100781 98898
178482 75891
77362 1...

output:

10393 21879 24491 20298 14694 10389 7911 6160 5075 4177 3642 3083 2647 2450 2099 1980 1797 1574 1510...

result:

ok 200001 tokens

Test #15:

score: 5
Accepted
time: 589ms
memory: 28456kb

input:

200000 200000
16019 146675
78225 135779
18131 51731
161892 52507
42027 111152
81034 185338
122292 18...

output:

10432 21923 24450 20187 14651 10448 7909 6137 5054 4337 3656 3038 2718 2410 2221 1998 1789 1544 1423...

result:

ok 200001 tokens

Test #16:

score: 5
Accepted
time: 617ms
memory: 28496kb

input:

200000 200000
165721 192656
138852 49059
86153 31626
138928 128658
199703 43701
112683 140272
41586 ...

output:

10282 21983 24614 20177 14590 10371 7777 6197 5089 4216 3606 3132 2803 2463 2183 1930 1799 1656 1536...

result:

ok 200001 tokens

Test #17:

score: 5
Accepted
time: 365ms
memory: 37436kb

input:

200000 199999
163369 45918
160316 159985
114894 121112
165819 62891
183049 30002
39996 157895
71254 ...

output:

0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ...

result:

ok 200000 tokens

Test #18:

score: 5
Accepted
time: 639ms
memory: 36364kb

input:

200000 200000
114571 129412
84680 111567
41875 21990
110441 49406
112540 167381
122214 82036
139727 ...

output:

0 2 1 0 1 1 1 1 0 2 1 1 0 1 1 0 0 1 1 1 0 0 1 0 2 1 1 1 1 0 1 1 2 0 2 0 1 2 1 1 1 2 0 1 1 1 1 0 0 1 ...

result:

ok 200001 tokens

Test #19:

score: 5
Accepted
time: 459ms
memory: 29960kb

input:

200000 200000
152950 172286
81402 40455
102388 180772
41862 9052
16836 122285
124563 50714
138444 40...

output:

9337 19966 21990 17842 12903 9087 6901 5208 4210 3564 2960 2444 2066 1839 1507 1355 1146 1117 898 86...

result:

ok 200001 tokens

Test #20:

score: 5
Accepted
time: 471ms
memory: 37428kb

input:

200000 200000
91696 108358
15991 131997
141623 126634
82046 133943
90417 65186
157116 190193
38945 2...

output:

0 0 0 1 1 1 0 0 1 1 1 1 0 2 0 0 2 1 1 0 0 2 1 2 1 0 1 0 1 2 0 0 4 1 0 2 0 0 1 0 1 2 0 2 1 0 2 1 1 1 ...

result:

ok 200001 tokens