QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#817284#6356. 树SGColin100 ✓16ms9256kbC++201.3kb2024-12-16 21:24:462024-12-16 21:24:47

Judging History

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

  • [2024-12-16 21:24:47]
  • 评测
  • 测评结果:100
  • 用时:16ms
  • 内存:9256kb
  • [2024-12-16 21:24:46]
  • 提交

answer

#include<bits/stdc++.h>
#define N 100010
#define gc getchar
#define mid ((l+r)>>1)
using namespace std;

inline int rd(){
  int x=0; bool f=0; char c=gc();
  while(!isdigit(c)){if(c=='-')f=1;c=gc();}
  while(isdigit(c)){x=(x<<1)+(x<<3)+(c^48);c=gc();}
  return f?-x:x;
}

int n,m,tot,hd[N],fa[N],cnt[N];

struct edge{int to,nxt;}e[N<<1];

inline void add(int u,int v){
  e[++tot].to=v; e[tot].nxt=hd[u]; hd[u]=tot;
}

void dfs(int u){
  for(int i=hd[u],v;i;i=e[i].nxt)
    if((v=e[i].to)!=fa[u]){fa[v]=u;dfs(v);}
}

struct Q{int op,x,ans;}q[N];

struct UFS{
  int f[N];
  UFS(){memset(f,0,sizeof(f));}
  int find(int x){return x==f[x]?x:f[x]=find(f[x]);}
  inline void merge(int x,int fa){f[find(x)]=find(fa);}
}ufs;

int main(){
  n=rd(); m=rd();
  for(int i=1,u,v;i<n;++i){
    u=rd(); v=rd();
    add(u,v); add(v,u);
  }
  fa[1]=1;
  dfs(1); char c;
  for(int i=1;i<=m;++i){
    c=gc(); while(!isalpha(c)) c=gc();
    q[i].op=(c=='C'); q[i].x=rd();
    if(q[i].op){ufs.f[q[i].x]=q[i].x;++cnt[q[i].x];}
  }
  for(int i=1;i<=n;++i) if(!ufs.f[i]) ufs.f[i]=fa[i];
  for(int i=m;i;--i)
    if(q[i].op){
      --cnt[q[i].x];
      if(!cnt[q[i].x]) ufs.merge(q[i].x,fa[q[i].x]);
    }
    else q[i].ans=ufs.find(q[i].x);
  for(int i=1;i<=m;++i) if(!q[i].op) printf("%d\n",q[i].ans);
  return 0;
}


Details

Tip: Click on the bar to expand more detailed information

Pretests


Final Tests

Test #1:

score: 10
Accepted
time: 1ms
memory: 4288kb

input:

1000 1000
1 2
2 3
3 4
4 5
5 6
6 7
7 8
8 9
9 10
10 11
11 12
12 13
13 14
14 15
15 16
16 17
17 18
18 19
19 20
20 21
21 22
22 23
23 24
24 25
25 26
26 27
27 28
28 29
29 30
30 31
31 32
32 33
33 34
34 35
35 36
36 37
37 38
38 39
39 40
40 41
41 42
42 43
43 44
44 45
45 46
46 47
47 48
48 49
49 50
50 51
51 52
5...

output:

1
1
1
1
1
1
1
1
1
348
1
413
1
1
348
348
134
260
1
413
1
260
405
348
485
405
413
134
1
297
15
405
345
303
260
386
126
291
413
15
217
413
134
647
31
134
31
260
223
134
377
305
31
172
172
31
265
508
31
248
413
265
134
305
265
265
31
305
31
1
771
265
355
188
265
305
298
134
426
647
457
1
508
151
265
217...

result:

ok 484 numbers

Test #2:

score: 10
Accepted
time: 1ms
memory: 4232kb

input:

1000 1000
1 2
2 3
3 4
4 5
5 6
6 7
7 8
8 9
9 10
10 11
11 12
12 13
13 14
14 15
15 16
16 17
17 18
18 19
19 20
20 21
21 22
22 23
23 24
24 25
25 26
26 27
27 28
28 29
29 30
30 31
31 32
32 33
33 34
34 35
35 36
36 37
37 38
38 39
39 40
40 41
41 42
42 43
43 44
44 45
45 46
46 47
47 48
48 49
49 50
50 51
51 52
5...

output:

1
129
1
129
95
199
199
199
381
1
95
381
199
129
129
688
381
199
199
199
199
79
95
129
381
199
381
79
381
381
381
499
381
500
199
161
95
381
495
199
1
354
340
988
381
36
36
199
199
129
161
476
161
36
376
1
381
129
199
381
381
381
240
129
199
38
38
444
38
340
95
354
161
199
321
38
421
354
1
446
289
24...

result:

ok 501 numbers

Test #3:

score: 10
Accepted
time: 1ms
memory: 6372kb

input:

1000 1000
1 2
2 3
3 4
4 5
5 6
6 7
7 8
8 9
9 10
10 11
11 12
12 13
13 14
14 15
15 16
16 17
17 18
18 19
19 20
20 21
21 22
22 23
23 24
24 25
25 26
26 27
27 28
28 29
29 30
30 31
31 32
32 33
33 34
34 35
35 36
36 37
37 38
38 39
39 40
40 41
41 42
42 43
43 44
44 45
45 46
46 47
47 48
48 49
49 50
50 51
51 52
5...

output:

1
1
1
1
1
1
1
1
1
338
1
70
1
70
70
70
1
454
338
1
155
263
155
263
155
283
338
1
454
1
308
338
70
194
155
155
308
263
5
70
194
338
338
426
155
338
298
220
194
5
426
247
289
226
70
155
70
70
5
1
158
220
338
158
462
106
158
158
338
158
454
338
158
197
338
31
31
155
338
338
462
158
558
289
74
462
220
74...

result:

ok 513 numbers

Test #4:

score: 10
Accepted
time: 2ms
memory: 6632kb

input:

10000 10000
1 2
2 3
3 4
4 5
5 6
6 7
7 8
8 9
9 10
10 11
11 12
12 13
13 14
14 15
15 16
16 17
17 18
18 19
19 20
20 21
21 22
22 23
23 24
24 25
25 26
26 27
27 28
28 29
29 30
30 31
31 32
32 33
33 34
34 35
35 36
36 37
37 38
38 39
39 40
40 41
41 42
42 43
43 44
44 45
45 46
46 47
47 48
48 49
49 50
50 51
51 52...

output:

1
1
1
1
1
1
1
1
1
4797
2677
191
191
2677
2677
191
2677
191
191
191
1017
2698
6259
2698
1017
1017
191
1543
191
2698
4247
1543
1543
3849
191
3849
1017
3354
3354
1543
2698
4711
2698
2698
4539
3849
1543
1543
2698
1017
3849
4261
1017
538
191
4261
191
31
2939
1017
1543
538
4697
1017
2939
2939
3354
1543
27...

result:

ok 5020 numbers

Test #5:

score: 10
Accepted
time: 2ms
memory: 6516kb

input:

10000 10000
1 2
2 3
3 4
4 5
5 6
6 7
7 8
8 9
9 10
10 11
11 12
12 13
13 14
14 15
15 16
16 17
17 18
18 19
19 20
20 21
21 22
22 23
23 24
24 25
25 26
26 27
27 28
28 29
29 30
30 31
31 32
32 33
33 34
34 35
35 36
36 37
37 38
38 39
39 40
40 41
41 42
42 43
43 44
44 45
45 46
46 47
47 48
48 49
49 50
50 51
51 52...

output:

1
1
1894
1894
1
3884
3884
3209
1
1
523
4277
2539
1
4277
1894
1474
523
3028
1474
4277
1894
3028
321
321
2539
3884
1311
1
1474
4277
1022
3028
4277
1
4277
1311
3209
321
1894
1311
3710
523
2216
2539
1047
2539
3209
6201
3209
4433
2539
2539
3209
1586
2539
1586
3028
4433
4637
4637
1258
1894
4564
3209
523
2...

result:

ok 5011 numbers

Test #6:

score: 10
Accepted
time: 2ms
memory: 6696kb

input:

10000 10000
1 2
2 3
3 4
4 5
5 6
6 7
7 8
8 9
9 10
10 11
11 12
12 13
13 14
14 15
15 16
16 17
17 18
18 19
19 20
20 21
21 22
22 23
23 24
24 25
25 26
26 27
27 28
28 29
29 30
30 31
31 32
32 33
33 34
34 35
35 36
36 37
37 38
38 39
39 40
40 41
41 42
42 43
43 44
44 45
45 46
46 47
47 48
48 49
49 50
50 51
51 52...

output:

1
1
1
1
2385
1
2385
1
1696
1696
1
2385
1
1696
1492
3128
3741
2385
31
177
177
1935
177
2385
4786
3899
177
2385
2385
177
177
2385
2385
3899
177
3128
456
456
1935
456
456
3128
177
1492
456
2385
3899
3899
456
177
456
1696
3128
4105
3128
4786
4786
4105
456
4105
4105
4105
4105
4786
3128
177
177
456
1516
4...

result:

ok 4987 numbers

Test #7:

score: 10
Accepted
time: 2ms
memory: 6824kb

input:

10000 10000
1 2
2 3
3 4
4 5
5 6
6 7
7 8
8 9
9 10
10 11
11 12
12 13
13 14
14 15
15 16
16 17
17 18
18 19
19 20
20 21
21 22
22 23
23 24
24 25
25 26
26 27
27 28
28 29
29 30
30 31
31 32
32 33
33 34
34 35
35 36
36 37
37 38
38 39
39 40
40 41
41 42
42 43
43 44
44 45
45 46
46 47
47 48
48 49
49 50
50 51
51 52...

output:

1
1
1
1
2749
3661
1
1
3928
1
2749
2236
1
3928
3661
3928
3928
1
1
3928
2749
1
1861
1
1
3928
2236
1
3928
1861
1861
1
2749
2749
2848
1507
3661
4343
2749
3446
1
3928
1
2366
3928
1861
3928
1
1
3446
1
1861
3446
3928
3928
1
329
4749
4377
1861
1283
804
1283
329
4377
329
3661
3446
3928
329
4377
2366
2617
437...

result:

ok 4992 numbers

Test #8:

score: 10
Accepted
time: 12ms
memory: 9204kb

input:

100000 100000
1 2
2 3
3 4
4 5
5 6
6 7
7 8
8 9
9 10
10 11
11 12
12 13
13 14
14 15
15 16
16 17
17 18
18 19
19 20
20 21
21 22
22 23
23 24
24 25
25 26
26 27
27 28
28 29
29 30
30 31
31 32
32 33
33 34
34 35
35 36
36 37
37 38
38 39
39 40
40 41
41 42
42 43
43 44
44 45
45 46
46 47
47 48
48 49
49 50
50 51
51 ...

output:

1
20863
20863
20863
1
1
20863
20863
20863
20863
33
20863
20863
20863
20863
20863
33
10850
13909
33
32877
33
37598
33
32877
20863
32877
32877
14665
62
62
62
22843
33861
45523
32877
10850
37598
16968
45523
16968
15862
22843
37598
22843
32877
62
6604
6604
45523
62
6604
41237
6604
33861
47807
10850
4690...

result:

ok 49991 numbers

Test #9:

score: 10
Accepted
time: 16ms
memory: 9244kb

input:

100000 100000
1 2
2 3
3 4
4 5
5 6
6 7
7 8
8 9
9 10
10 11
11 12
12 13
13 14
14 15
15 16
16 17
17 18
18 19
19 20
20 21
21 22
22 23
23 24
24 25
25 26
26 27
27 28
28 29
29 30
30 31
31 32
32 33
33 34
34 35
35 36
36 37
37 38
38 39
39 40
40 41
41 42
42 43
43 44
44 45
45 46
46 47
47 48
48 49
49 50
50 51
51 ...

output:

24269
37453
40703
1
24269
1
40703
1
24269
14484
24269
1
37453
14484
14484
17750
17750
20129
35205
40703
24269
17750
35205
1766
24269
17750
37453
24269
40720
34165
1766
24269
1
40720
40720
23030
37453
12576
40720
28424
1766
37453
34165
40720
37453
15372
24269
37453
40720
12576
12576
24715
20626
14484...

result:

ok 49991 numbers

Test #10:

score: 10
Accepted
time: 11ms
memory: 9256kb

input:

100000 100000
1 2
2 3
3 4
4 5
5 6
6 7
7 8
8 9
9 10
10 11
11 12
12 13
13 14
14 15
15 16
16 17
17 18
18 19
19 20
20 21
21 22
22 23
23 24
24 25
25 26
26 27
27 28
28 29
29 30
30 31
31 32
32 33
33 34
34 35
35 36
36 37
37 38
38 39
39 40
40 41
41 42
42 43
43 44
44 45
45 46
46 47
47 48
48 49
49 50
50 51
51 ...

output:

1
1
18112
18112
29541
29541
29541
18112
1
29541
29541
1
1
29541
39554
39554
39554
1
39283
29541
39554
1
5464
39554
29541
16120
1
29541
39554
5464
16120
39554
24812
39554
22434
5464
39554
5464
16120
46850
39554
25498
22434
8205
31387
8205
542
46850
39554
16120
25498
31387
48703
5473
39554
39554
21619...

result:

ok 50008 numbers