CSES - Siperia opettaa 4.0 - Results
Submission details
Task:Jimi Hendrix
Sender:Hansuzu
Submission time:2016-08-04 17:18:39 +0300
Language:C++
Status:READY
Result:100
Feedback
groupverdictscore
#1ACCEPTED100
Test results
testverdicttime
#1ACCEPTED0.06 sdetails
#2ACCEPTED0.06 sdetails
#3ACCEPTED0.06 sdetails
#4ACCEPTED0.05 sdetails
#5ACCEPTED0.06 sdetails
#6ACCEPTED0.06 sdetails
#7ACCEPTED0.05 sdetails
#8ACCEPTED0.06 sdetails
#9ACCEPTED0.07 sdetails
#10ACCEPTED0.06 sdetails
#11ACCEPTED0.06 sdetails
#12ACCEPTED0.06 sdetails
#13ACCEPTED0.06 sdetails
#14ACCEPTED0.05 sdetails
#15ACCEPTED0.06 sdetails
#16ACCEPTED0.06 sdetails
#17ACCEPTED0.06 sdetails
#18ACCEPTED0.06 sdetails
#19ACCEPTED0.06 sdetails
#20ACCEPTED0.06 sdetails
#21ACCEPTED0.06 sdetails
#22ACCEPTED0.06 sdetails
#23ACCEPTED0.16 sdetails
#24ACCEPTED0.14 sdetails
#25ACCEPTED0.58 sdetails
#26ACCEPTED0.58 sdetails
#27ACCEPTED0.59 sdetails
#28ACCEPTED0.66 sdetails
#29ACCEPTED0.55 sdetails
#30ACCEPTED0.58 sdetails
#31ACCEPTED0.57 sdetails
#32ACCEPTED0.31 sdetails
#33ACCEPTED0.31 sdetails
#34ACCEPTED0.31 sdetails
#35ACCEPTED0.30 sdetails
#36ACCEPTED0.32 sdetails
#37ACCEPTED0.30 sdetails
#38ACCEPTED0.32 sdetails
#39ACCEPTED0.44 sdetails
#40ACCEPTED0.34 sdetails
#41ACCEPTED0.31 sdetails
#42ACCEPTED0.43 sdetails
#43ACCEPTED0.31 sdetails
#44ACCEPTED0.45 sdetails
#45ACCEPTED0.32 sdetails
#46ACCEPTED0.30 sdetails
#47ACCEPTED0.32 sdetails
#48ACCEPTED0.44 sdetails
#49ACCEPTED0.47 sdetails
#50ACCEPTED0.47 sdetails
#51ACCEPTED0.49 sdetails
#52ACCEPTED0.49 sdetails
#53ACCEPTED0.51 sdetails
#54ACCEPTED0.46 sdetails

Compiler report

input/code.cpp: In function 'void clearsz(int)':
input/code.cpp:20:30: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for (int i=0; i<rt[c].size(); ++i) clearsz(rt[c][i].F);
                              ^
input/code.cpp: In function 'int go(int, int)':
input/code.cpp:29:30: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for (int i=0; i<rt[c].size(); ++i){
                              ^
input/code.cpp:41:32: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for (int i=0; i<rt[c].size(); ++i){
                                ^
input/code.cpp: In function 'std::pair<std::pair<int, int>, std::pair<int, int> > findg(int)':
input/code.cpp:72:30: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for (int i=0; i<rt[c].size(); ++i){
                              ^
input/code.cpp: In function 'void find(int)':
input/code.cpp:105:30: war...

Code

#include <iostream>
#include <vector>
#define MP make_pair
#define F first
#define S second
using namespace std;
int n, m;
string s;


vector<pair<int, char> > rt[505050];
vector<int> crt[505050];

int sz[505050];
int u[505050];

void clearsz(int c){
  if (u[c] || sz[c]==0) return;
  sz[c]=0;
  for (int i=0; i<rt[c].size(); ++i) clearsz(rt[c][i].F);
}

int d;
int nz;
int go(int c, int p){
  sz[c]=1;
  int mx=0;
  int a=0;
  for (int i=0; i<rt[c].size(); ++i){
    int w=rt[c][i].F;
    if (u[w] || sz[w]) continue;
    a=go(w, c);
    if (a) return a;
    sz[c]+=sz[w];
    mx=max(mx, sz[w]);
  }
  mx=max(mx, nz-sz[c]);
  if (mx<=nz/2){
    u[c]=++d;
    int nzt=nz;
    for (int i=0; i<rt[c].size(); ++i){
      int w=rt[c][i].F;
      if (u[w]) continue;
      int nsz=sz[w];
      if (w==p) nsz=nz-sz[c];
      nz=nsz;
      clearsz(w);
      crt[c].push_back(go(w, -1));
      nz=nzt;
    }--d;
    sz[c]=nz;
    return c;
  }
  return 0;
}

int cn;

int na=-1;
int nb=-1;


int pr[505050];

int mxl;
pair<pair<int, int>, pair<int, int> > findg(int c){
  int mxa=0;
  int mxai=c;
  int mxb=0;
  int mxbi=c;
  
  for (int i=0; i<rt[c].size(); ++i){
    if (rt[c][i].F==pr[c] || u[rt[c][i].F]<=mxl) continue;
    
    pr[rt[c][i].F]=c;
    pair<pair<int, int>, pair<int, int> > k=findg(rt[c][i].F);
    if (k.F.F<m-1 && s[k.F.F]==rt[c][i].S){
      ++k.F.F;
    }
    if (k.S.F<m-1 && s[m-k.S.F-1]==rt[c][i].S){
      ++k.S.F;
    }
    if (k.F.F>mxa){
      mxa=k.F.F;
      mxai=k.F.S;
    }
    if (k.S.F>mxb){
      mxb=k.S.F;
      mxbi=k.S.S;
    }
  }
  return MP(MP(mxa, mxai), MP(mxb, mxbi));
}


void find(int c){
  if (sz[c]<m) return;  
  if (na!=-1) return;
  pr[c]=-1;
  mxl=u[c];
  int mxa=0;
  int mxb=0;
  int mxai=c;
  int mxbi=c;
  for (int i=0; i<rt[c].size(); ++i){
    pair<pair<int, int>, pair<int, int> > mx=findg(rt[c][i].F);
    if (s[mx.F.F]==rt[c][i].S)  ++mx.F.F;
    if (s[m-mx.S.F-1]==rt[c][i].S) ++mx.S.F;
    if (mxa+mx.S.F>=m){
      na=mxai;
      nb=mx.S.S;
      return;
    }
    if (mxb+mx.F.F>=m){
      na=mx.F.S;
      nb=mxbi;
      return;
    }
    if (mx.F.F>mxa){
      mxa=mx.F.F;
      mxai=mx.F.S;
    }
    if (mx.S.F>mxb){
      mxb=mx.S.F;
      mxbi=mx.S.S;
    }
  }
  
  for (int i=0; i<crt[c].size(); ++i)find(crt[c][i]);
}


int main(){
  ios_base::sync_with_stdio(0); cin.tie(0);
  cin >> n >> m;
  for (int i=1; i<n; ++i){
    int a, b; char c;
    cin >> a >> b >> c;
    rt[a].push_back(MP(b, c));
    rt[b].push_back(MP(a, c));
  }
  nz=n;
  cn=go(1, -1);
  cin >> s;
  find(cn);
  cout << na << " " << nb << "\n";
}

Test details

Test 1

Verdict: ACCEPTED

input
9 3
1 2 a
2 3 b
2 4 a
4 5 b
...

correct output
1 9

user output
1 9

Test 2

Verdict: ACCEPTED

input
2 1
1 2 b
b

correct output
2 1

user output
2 1

Test 3

Verdict: ACCEPTED

input
2 1
1 2 k
p

correct output
-1 -1

user output
-1 -1

Test 4

Verdict: ACCEPTED

input
10 2
1 4 a
7 5 c
1 9 a
10 1 c
...

correct output
2 1

user output
3 6

Test 5

Verdict: ACCEPTED

input
10 5
6 1 c
7 6 a
7 8 c
6 5 c
...

correct output
-1 -1

user output
-1 -1

Test 6

Verdict: ACCEPTED

input
10 3
3 10 c
1 9 b
4 8 a
10 4 b
...

correct output
10 1

user output
10 9

Test 7

Verdict: ACCEPTED

input
10 5
9 1 c
5 4 c
1 7 a
3 9 c
...

correct output
-1 -1

user output
-1 -1

Test 8

Verdict: ACCEPTED

input
10 2
9 10 c
9 8 c
9 3 c
9 4 c
...

correct output
1 10

user output
1 10

Test 9

Verdict: ACCEPTED

input
10 2
10 3 c
10 4 b
10 8 b
10 1 a
...

correct output
1 9

user output
1 3

Test 10

Verdict: ACCEPTED

input
10 2
6 7 e
6 9 l
6 2 q
6 10 i
...

correct output
1 9

user output
1 9

Test 11

Verdict: ACCEPTED

input
10 2
4 1 s
4 6 y
4 9 p
4 2 t
...

correct output
-1 -1

user output
-1 -1

Test 12

Verdict: ACCEPTED

input
1000 11
987 593 b
428 821 a
865 116 b
676 817 a
...

correct output
998 1

user output
998 819

Test 13

Verdict: ACCEPTED

input
1000 10
782 148 a
498 256 a
389 269 a
156 291 b
...

correct output
652 1

user output
469 561

Test 14

Verdict: ACCEPTED

input
1000 20
657 328 a
185 518 a
778 343 b
56 390 a
...

correct output
-1 -1

user output
-1 -1

Test 15

Verdict: ACCEPTED

input
1000 10
555 641 a
309 489 b
799 943 b
934 662 a
...

correct output
953 332

user output
645 569

Test 16

Verdict: ACCEPTED

input
1000 6
161 816 b
25 712 b
540 233 b
980 204 c
...

correct output
990 1

user output
683 149

Test 17

Verdict: ACCEPTED

input
1000 635
163 159 o
615 797 x
192 770 n
211 860 j
...

correct output
650 951

user output
650 134

Test 18

Verdict: ACCEPTED

input
1000 191
370 983 e
792 207 u
260 334 t
504 718 t
...

correct output
-1 -1

user output
-1 -1

Test 19

Verdict: ACCEPTED

input
1000 40
471 594 o
345 995 n
973 405 y
215 646 o
...

correct output
1 982

user output
778 218

Test 20

Verdict: ACCEPTED

input
1000 100
151 902 a
730 935 b
295 369 b
823 145 a
...

correct output
948 1

user output
414 676

Test 21

Verdict: ACCEPTED

input
1000 2
660 306 n
660 778 l
660 924 y
660 902 n
...

correct output
988 969

user output
988 70

Test 22

Verdict: ACCEPTED

input
1000 2
526 579 a
526 150 b
526 168 a
526 24 b
...

correct output
1000 1

user output
579 168

Test 23

Verdict: ACCEPTED

input
200000 2
140844 190697 r
140844 124115 f
140844 112461 i
140844 19992 d
...

correct output
124115 199952

user output
124115 73325

Test 24

Verdict: ACCEPTED

input
200000 2
105421 5980 b
105421 150207 a
105421 115929 a
105421 77625 b
...

correct output
200000 1

user output
5980 77625

Test 25

Verdict: ACCEPTED

input
200000 50000
38207 51908 a
123390 86325 b
123134 78235 b
104677 7330 a
...

correct output
112796 1

user output
112796 57909

Test 26

Verdict: ACCEPTED

input
200000 150000
116487 13268 b
84833 43815 b
40993 188837 a
197098 131672 a
...

correct output
-1 -1

user output
-1 -1

Test 27

Verdict: ACCEPTED

input
200000 5000
6663 37269 l
74938 58143 w
79955 85205 e
172041 175241 z
...

correct output
199644 1

user output
67105 142940

Test 28

Verdict: ACCEPTED

input
200000 10000
162365 187926 o
25849 45282 n
69475 76654 d
59397 127873 g
...

correct output
-1 -1

user output
-1 -1

Test 29

Verdict: ACCEPTED

input
200000 4736
135910 152433 b
196857 44930 b
180142 58474 b
123198 131735 a
...

correct output
193336 1

user output
58629 1192

Test 30

Verdict: ACCEPTED

input
200000 54739
116541 156209 b
112813 171247 b
134494 179457 b
52303 59188 b
...

correct output
175912 1

user output
24116 102951

Test 31

Verdict: ACCEPTED

input
200000 49999
117084 195231 a
38504 191202 n
134618 132918 i
24748 150597 v
...

correct output
198783 1

user output
35200 11981

Test 32

Verdict: ACCEPTED

input
200000 20
56534 21846 a
127466 99775 b
197009 10481 a
13071 25861 a
...

correct output
154647 179047

user output
111576 24194

Test 33

Verdict: ACCEPTED

input
200000 18
25645 115335 b
138086 169629 b
147195 28130 a
90251 19229 b
...

correct output
1 11192

user output
53824 62914

Test 34

Verdict: ACCEPTED

input
200000 15
130040 52065 a
85066 121947 a
142587 138972 c
98852 109707 b
...

correct output
186271 1

user output
135125 102418

Test 35

Verdict: ACCEPTED

input
200000 13
186972 158038 n
141304 144334 c
150644 92260 d
75472 56494 e
...

correct output
134673 188304

user output
90352 188304

Test 36

Verdict: ACCEPTED

input
200000 22
77116 177355 b
41248 8511 a
196560 61055 b
93074 47152 b
...

correct output
101231 1

user output
7011 177273

Test 37

Verdict: ACCEPTED

input
200000 24
12092 977 b
89606 92554 a
98999 130713 b
29359 58778 a
...

correct output
45421 191976

user output
105435 168998

Test 38

Verdict: ACCEPTED

input
200000 32
42678 46956 a
85804 92343 a
58647 187834 a
40022 25789 a
...

correct output
170898 83806

user output
10897 83806

Test 39

Verdict: ACCEPTED

input
200000 37
97344 4860 b
59032 105557 b
33232 135880 a
41849 198146 b
...

correct output
-1 -1

user output
-1 -1

Test 40

Verdict: ACCEPTED

input
200000 35
165160 188580 b
169019 179034 a
162600 167716 b
12953 68090 b
...

correct output
34556 152796

user output
34556 121411

Test 41

Verdict: ACCEPTED

input
200000 36
25981 130203 b
96700 180125 a
127368 165961 b
64857 173799 b
...

correct output
140998 25928

user output
125942 25928

Test 42

Verdict: ACCEPTED

input
200000 37
84272 141200 a
115974 60401 b
164159 9233 b
95061 109295 a
...

correct output
-1 -1

user output
-1 -1

Test 43

Verdict: ACCEPTED

input
200000 9
64062 160380 r
125886 155571 c
171774 169538 y
98278 87688 l
...

correct output
178549 166726

user output
178549 34044

Test 44

Verdict: ACCEPTED

input
200000 10
180259 144845 g
116606 49642 c
17065 195194 h
19110 15737 w
...

correct output
-1 -1

user output
-1 -1

Test 45

Verdict: ACCEPTED

input
200000 28
69795 159223 b
120740 49968 a
175591 112576 b
13475 97987 b
...

correct output
191304 139389

user output
182861 60392

Test 46

Verdict: ACCEPTED

input
200000 29
196920 93284 b
10733 37080 b
60158 81484 a
182923 50537 a
...

correct output
118826 166004

user output
146699 50672

Test 47

Verdict: ACCEPTED

input
200000 29
139335 36430 a
19077 167148 a
14826 196018 a
24497 91018 b
...

correct output
176667 184941

user output
7196 111243

Test 48

Verdict: ACCEPTED

input
200000 38
198494 65812 a
154023 48530 a
161230 125892 a
163972 48765 b
...

correct output
-1 -1

user output
-1 -1

Test 49

Verdict: ACCEPTED

input
200000 100000
138756 91790 c
50567 42961 d
199463 59094 d
158218 93083 e
...

correct output
120276 158931

user output
120276 158931

Test 50

Verdict: ACCEPTED

input
200000 100000
93749 174202 a
182960 2304 n
23460 111271 d
142390 124490 h
...

correct output
197101 82602

user output
197101 82602

Test 51

Verdict: ACCEPTED

input
200000 50000
26351 4065 d
190076 154406 b
47708 176166 c
56748 94747 d
...

correct output
102345 166560

user output
80847 166560

Test 52

Verdict: ACCEPTED

input
200000 50000
25937 89996 f
147254 157058 r
72097 126197 k
143403 116253 i
...

correct output
175469 125703

user output
73950 125703

Test 53

Verdict: ACCEPTED

input
200000 1000
138872 149623 e
161260 117770 a
151105 54329 a
120464 187446 d
...

correct output
188942 1

user output
15560 57113

Test 54

Verdict: ACCEPTED

input
200000 1000
53986 146114 r
139076 142403 c
86688 88562 g
40058 165888 g
...

correct output
140202 1

user output
65384 90992