#include <bits/stdc++.h>
#define DIMN 120010
using namespace std;
int elem;
int rmq_lca[20][2*DIMN] , eul[2*DIMN] , first[DIMN] , nv[2*DIMN] , logg[2*DIMN] , niv[DIMN];
int rmqc[20][DIMN] , rmqd[20][DIMN] , rmqt[20][DIMN] , maxi[20][DIMN];
int help[20][DIMN];
vector <pair <int , int> > v[DIMN];
pair <int , int> tt[DIMN];
struct queryuri{
int x , y , mch;
} qry[DIMN];
void dfs (int nod , int fth , int val , int lvl){
int i , vecin , cost;
eul[++elem] = nod;
first[nod] = elem;
nv[elem] = lvl;
niv[nod] = lvl;
for (i = 0 ; i < v[nod].size() ; i++){
vecin = v[nod][i].first;
cost = v[nod][i].second;
if (vecin == fth){
tt[nod] = make_pair(vecin , val);
continue;
}
dfs(vecin , nod , cost , lvl + 1);
eul[++elem] = nod;
nv[elem] = lvl;
}
}
int query (int x, int y){
int px , py , len;
px = first[x];
py = first[y];
if (px > py)
swap(px , py);
len = py - px + 1;
len = logg[len]; /// cea mai mare p2 <= len
if (nv[ rmq_lca[len][px] ] < nv[ rmq_lca[len][py - (1 << len) + 1] ])
return eul[rmq_lca[len][px]];
else return eul[rmq_lca[len][py - (1 << len) + 1]];
}
int qmaxi (int x , int y){
int p2 , val = 0;
for (p2 = 19 ; p2 >= 0 ; p2--){
if (niv[x] - (1 << p2) >= niv[y]){
val = max(val , maxi[p2][x]);
x = rmqt[p2][x];
}
}
return val;
}
int ok (int x , int y , int fth){
int p2 , val = 1 , y2 , x2 , j , vecin;
y2 = y;
for (p2 = 19 ; p2 >= 0 ; p2--){
if (niv[y] - (1 << p2) >= niv[fth]){
val = (val & rmqc[p2][y]);
y = rmqt[p2][y];
/// este cazul sa fac verificarea?
if (y != fth){
for (j = 0 ; j < v[y].size() ; j++){
vecin = v[y][j].first;
if (vecin != tt[y].first && query(vecin , y2) == vecin){
if (v[y][j].second < tt[y].second)
break;
else return 0;
}
}
}
}
}
if (!val)
return 0;
val = 1;
x2 = x;
for (p2 = 19 ; p2 >= 0 ; p2--){
if (niv[x] - (1 << p2) >= niv[fth]){
val = (val & rmqd[p2][x]);
x = rmqt[p2][x];
if (x != fth){
for (j = 0 ; j < v[x].size() ; j++){
vecin = v[x][j].first;
if (vecin != tt[x].first && query(vecin , x2) == vecin){
if (v[x][j].second > tt[x].second)
break;
else return 0;
}
}
}
}
}
if (!val)
return 0;
if (x2 == fth || y2 == fth)
return 1;
for (p2 = 19 ; p2 >= 0 ; p2--){
if (niv[x2] - (1 << p2) > niv[fth])
x2 = rmqt[p2][x2];
if (niv[y2] - (1 << p2) > niv[fth])
y2 = rmqt[p2][y2];
}
return tt[y2].second < tt[x2].second;
}
int main()
{
FILE *fin = stdin;
FILE *fout = stdout;
//FILE *in = fopen ("b.in", "r");
int n , q , cnt , i , x , y , mch = 0 , powe , fth , j , vecin;
char c;
fscanf (fin,"%d%d\n",&n,&q);
cnt = 0;
for (i = 1 ; i <= n + q - 1 ; i++){
c = fgetc(fin);
if ( c == 'S' ){
fscanf (fin,"%d%d\n",&x,&y);
mch++;
v[x].push_back(make_pair(y , mch));
v[y].push_back(make_pair(x , mch));
}
else if ( c == 'Q' ){
fscanf (fin,"%d%d\n",&x,&y);
qry[++cnt] = {x , y , mch};
}
else { /// momentan nimic
}
}
/// vrei sa ai lca disponibil
dfs (1 , 0 , 0 , 1);
for (i = 2 ; i <= 2 * n ; i++)
logg[i] = 1 + logg[i / 2];
for (i = 1 ; i <= elem ; i++)
rmq_lca[0][i] = i;
for (powe = 1 ; (1 << powe) <= elem ; powe++){
for (i = 1 ; i + (1 << powe) - 1 <= elem ; i++){
if (nv[ rmq_lca[powe-1][i] ] < nv[ rmq_lca[powe-1][i + (1 << (powe - 1) )] ])
rmq_lca[powe][i] = rmq_lca[powe-1][i];
else rmq_lca[powe][i] = rmq_lca[powe-1][i + (1 << (powe - 1) )];
}
}
for (i = 1 ; i <= n ; i++){
rmqt[0][i] = tt[i].first;
}
for (powe = 1 ; (1 << powe) <= n ; powe++){
for (i = 1 ; i <= n ; i++){
rmqt[powe][i] = rmqt[powe - 1][rmqt[powe - 1][i]];
}
}
/// imi va mai trb un rmqc si rmqd
for (i = 1 ; i <= n ; i++){
rmqc[0][i] = rmqd[0][i] = 1;
maxi[0][i] = tt[i].second;
help[0][i] = i;
}
for (powe = 1 ; (1 << powe) <= n ; powe++){
for (i = 1 ; i <= n ; i++){
maxi[powe][i] = max(maxi[powe - 1][i] , maxi[powe - 1][ rmqt[powe - 1][i] ]);
if (rmqc[powe-1][i] && rmqc[powe-1][ rmqt[powe - 1][i] ] && tt[help[powe - 1][i]].second < tt[rmqt[powe-1][i]].second)
rmqc[powe][i] = 1;
if (rmqd[powe-1][i] && rmqd[powe-1][ rmqt[powe - 1][i] ] && tt[help[powe - 1][i]].second > tt[rmqt[powe-1][i]].second)
rmqd[powe][i] = 1;
help[powe][i] = rmqt[powe - 1][ help[powe - 1][i] ];
}
}
/// momentul sa rasp la queryuri?
char ans[10];
for (i = 1 ; i <= cnt ; i++){
x = qry[i].x;
y = qry[i].y;
mch = qry[i].mch;
fth = query(x , y);
if (qmaxi(x , fth) > mch || qmaxi(y , fth) > mch || !mch){
//fscanf (in,"%s\n",ans);
//if (ans[0] != 'n')
//printf ("%d\n", i);
fprintf (fout,"no\n"); /// maximul prea mare
continue;
}
//fscanf (in,"%s\n",ans);
if (ok(x , y , fth)){
//if (ans[0] != 'y')
//printf ("%d\n", i);
fprintf (fout,"yes\n");
}
else {
//if (ans[0] != 'n')
// printf ("%d\n", i);
fprintf (fout,"no\n");
}
}
return 0;
}
Compilation message
servers.cpp: In function 'void dfs(int, int, int, int)':
servers.cpp:25:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
25 | for (i = 0 ; i < v[nod].size() ; i++){
| ~~^~~~~~~~~~~~~~~
servers.cpp: In function 'int ok(int, int, int)':
servers.cpp:89:32: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
89 | for (j = 0 ; j < v[y].size() ; j++){
| ~~^~~~~~~~~~~~~
servers.cpp:121:32: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
121 | for (j = 0 ; j < v[x].size() ; j++){
| ~~^~~~~~~~~~~~~
servers.cpp: In function 'int main()':
servers.cpp:164:58: warning: unused variable 'j' [-Wunused-variable]
164 | int n , q , cnt , i , x , y , mch = 0 , powe , fth , j , vecin;
| ^
servers.cpp:164:62: warning: unused variable 'vecin' [-Wunused-variable]
164 | int n , q , cnt , i , x , y , mch = 0 , powe , fth , j , vecin;
| ^~~~~
servers.cpp:252:10: warning: unused variable 'ans' [-Wunused-variable]
252 | char ans[10];
| ^~~
servers.cpp:166:12: warning: ignoring return value of 'int fscanf(FILE*, const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
166 | fscanf (fin,"%d%d\n",&n,&q);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~
servers.cpp:171:20: warning: ignoring return value of 'int fscanf(FILE*, const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
171 | fscanf (fin,"%d%d\n",&x,&y);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~
servers.cpp:179:20: warning: ignoring return value of 'int fscanf(FILE*, const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
179 | fscanf (fin,"%d%d\n",&x,&y);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
41 ms |
5196 KB |
Output is correct |
2 |
Correct |
50 ms |
6472 KB |
Output is correct |
3 |
Correct |
83 ms |
7924 KB |
Output is correct |
4 |
Correct |
62 ms |
8028 KB |
Output is correct |
5 |
Correct |
98 ms |
8132 KB |
Output is correct |
6 |
Correct |
61 ms |
7916 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
41 ms |
5196 KB |
Output is correct |
2 |
Correct |
50 ms |
6472 KB |
Output is correct |
3 |
Correct |
83 ms |
7924 KB |
Output is correct |
4 |
Correct |
62 ms |
8028 KB |
Output is correct |
5 |
Correct |
98 ms |
8132 KB |
Output is correct |
6 |
Correct |
61 ms |
7916 KB |
Output is correct |
7 |
Incorrect |
37 ms |
4928 KB |
Extra information in the output file |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
43 ms |
5024 KB |
Output is correct |
2 |
Correct |
183 ms |
55656 KB |
Output is correct |
3 |
Correct |
198 ms |
55588 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
43 ms |
5024 KB |
Output is correct |
2 |
Correct |
183 ms |
55656 KB |
Output is correct |
3 |
Correct |
198 ms |
55588 KB |
Output is correct |
4 |
Incorrect |
37 ms |
4836 KB |
Extra information in the output file |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
43 ms |
5132 KB |
Output is correct |
2 |
Correct |
199 ms |
65460 KB |
Output is correct |
3 |
Correct |
209 ms |
68788 KB |
Output is correct |
4 |
Correct |
298 ms |
73924 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
43 ms |
5132 KB |
Output is correct |
2 |
Correct |
199 ms |
65460 KB |
Output is correct |
3 |
Correct |
209 ms |
68788 KB |
Output is correct |
4 |
Correct |
298 ms |
73924 KB |
Output is correct |
5 |
Incorrect |
38 ms |
4916 KB |
Extra information in the output file |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
40 ms |
4984 KB |
Output is correct |
2 |
Correct |
206 ms |
57136 KB |
Output is correct |
3 |
Correct |
210 ms |
60804 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
40 ms |
4984 KB |
Output is correct |
2 |
Correct |
206 ms |
57136 KB |
Output is correct |
3 |
Correct |
210 ms |
60804 KB |
Output is correct |
4 |
Incorrect |
36 ms |
4956 KB |
Extra information in the output file |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
41 ms |
5052 KB |
Output is correct |
2 |
Correct |
222 ms |
65436 KB |
Output is correct |
3 |
Correct |
212 ms |
68872 KB |
Output is correct |
4 |
Correct |
298 ms |
73932 KB |
Output is correct |
5 |
Correct |
40 ms |
5928 KB |
Output is correct |
6 |
Correct |
214 ms |
60292 KB |
Output is correct |
7 |
Correct |
200 ms |
60836 KB |
Output is correct |
8 |
Correct |
280 ms |
61172 KB |
Output is correct |
9 |
Correct |
225 ms |
61252 KB |
Output is correct |
10 |
Correct |
270 ms |
76992 KB |
Output is correct |
11 |
Correct |
482 ms |
75500 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
41 ms |
5052 KB |
Output is correct |
2 |
Correct |
222 ms |
65436 KB |
Output is correct |
3 |
Correct |
212 ms |
68872 KB |
Output is correct |
4 |
Correct |
298 ms |
73932 KB |
Output is correct |
5 |
Correct |
40 ms |
5928 KB |
Output is correct |
6 |
Correct |
214 ms |
60292 KB |
Output is correct |
7 |
Correct |
200 ms |
60836 KB |
Output is correct |
8 |
Correct |
280 ms |
61172 KB |
Output is correct |
9 |
Correct |
225 ms |
61252 KB |
Output is correct |
10 |
Correct |
270 ms |
76992 KB |
Output is correct |
11 |
Correct |
482 ms |
75500 KB |
Output is correct |
12 |
Incorrect |
37 ms |
4952 KB |
Extra information in the output file |
13 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
40 ms |
5060 KB |
Output is correct |
2 |
Correct |
54 ms |
6500 KB |
Output is correct |
3 |
Correct |
94 ms |
8092 KB |
Output is correct |
4 |
Correct |
61 ms |
8164 KB |
Output is correct |
5 |
Correct |
80 ms |
8184 KB |
Output is correct |
6 |
Correct |
64 ms |
7900 KB |
Output is correct |
7 |
Correct |
50 ms |
5880 KB |
Output is correct |
8 |
Correct |
196 ms |
58424 KB |
Output is correct |
9 |
Correct |
186 ms |
58516 KB |
Output is correct |
10 |
Correct |
42 ms |
5956 KB |
Output is correct |
11 |
Correct |
198 ms |
68880 KB |
Output is correct |
12 |
Correct |
201 ms |
68924 KB |
Output is correct |
13 |
Correct |
348 ms |
73860 KB |
Output is correct |
14 |
Correct |
39 ms |
5944 KB |
Output is correct |
15 |
Correct |
224 ms |
60240 KB |
Output is correct |
16 |
Correct |
220 ms |
60788 KB |
Output is correct |
17 |
Correct |
296 ms |
61328 KB |
Output is correct |
18 |
Correct |
239 ms |
61212 KB |
Output is correct |
19 |
Correct |
281 ms |
76960 KB |
Output is correct |
20 |
Correct |
438 ms |
75296 KB |
Output is correct |
21 |
Correct |
220 ms |
59556 KB |
Output is correct |
22 |
Correct |
214 ms |
59936 KB |
Output is correct |
23 |
Correct |
272 ms |
60720 KB |
Output is correct |
24 |
Correct |
285 ms |
61160 KB |
Output is correct |
25 |
Correct |
261 ms |
62500 KB |
Output is correct |
26 |
Correct |
212 ms |
60880 KB |
Output is correct |
27 |
Correct |
178 ms |
60880 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
40 ms |
5060 KB |
Output is correct |
2 |
Correct |
54 ms |
6500 KB |
Output is correct |
3 |
Correct |
94 ms |
8092 KB |
Output is correct |
4 |
Correct |
61 ms |
8164 KB |
Output is correct |
5 |
Correct |
80 ms |
8184 KB |
Output is correct |
6 |
Correct |
64 ms |
7900 KB |
Output is correct |
7 |
Correct |
50 ms |
5880 KB |
Output is correct |
8 |
Correct |
196 ms |
58424 KB |
Output is correct |
9 |
Correct |
186 ms |
58516 KB |
Output is correct |
10 |
Correct |
42 ms |
5956 KB |
Output is correct |
11 |
Correct |
198 ms |
68880 KB |
Output is correct |
12 |
Correct |
201 ms |
68924 KB |
Output is correct |
13 |
Correct |
348 ms |
73860 KB |
Output is correct |
14 |
Correct |
39 ms |
5944 KB |
Output is correct |
15 |
Correct |
224 ms |
60240 KB |
Output is correct |
16 |
Correct |
220 ms |
60788 KB |
Output is correct |
17 |
Correct |
296 ms |
61328 KB |
Output is correct |
18 |
Correct |
239 ms |
61212 KB |
Output is correct |
19 |
Correct |
281 ms |
76960 KB |
Output is correct |
20 |
Correct |
438 ms |
75296 KB |
Output is correct |
21 |
Correct |
220 ms |
59556 KB |
Output is correct |
22 |
Correct |
214 ms |
59936 KB |
Output is correct |
23 |
Correct |
272 ms |
60720 KB |
Output is correct |
24 |
Correct |
285 ms |
61160 KB |
Output is correct |
25 |
Correct |
261 ms |
62500 KB |
Output is correct |
26 |
Correct |
212 ms |
60880 KB |
Output is correct |
27 |
Correct |
178 ms |
60880 KB |
Output is correct |
28 |
Incorrect |
38 ms |
4920 KB |
Extra information in the output file |
29 |
Halted |
0 ms |
0 KB |
- |