Submission #300383

# Submission time Handle Problem Language Result Execution time Memory
300383 2020-09-17T06:29:19 Z bacegen4o Joker (BOI20_joker) C
71 / 100
2000 ms 18780 KB
#pragma GCC optimize "-O3"
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<assert.h>
#include<stdbool.h>
#include<limits.h>
#define swap(a,b) do{ __typeof(a) tp; tp=a; a=b; b=tp; }while(0)
typedef struct{
  int first, second;
}pair;
pair newpair(int a, int b){
  return(pair){a,b};
}
int cmpP(const void*pa, const void*pb){
  pair*a = (pair*)pa;
  pair*b = (pair*)pb;
  if(a->first  != b->first) return(a->first  < b->first )?-1:1;
  if(a->second != b->second)return(a->second < b->second)?-1:1;
  return 0;
}

#define pb(zpv, zvv) zpv.ptr = pushback(zpv.ptr, &zpv.sz, zvv)
#define resizeArray(ptr, type, size) ((type*)realloc(ptr, (size) * sizeof(type)))
int *pushback(int *array, int *size, int value) {
    int *output = resizeArray(array, int, *size + 1);
    output[(*size)++] = value;
    return output;
}
typedef struct{
	int*ptr;
	int sz;
}vec;
vec newVec(){
	vec rez;
	rez.ptr = NULL;
	rez.sz  = 0;
	return rez;
}
inline double sqrt(double x){
  __asm__ ("fsqrt" : "+t" (x));
  return x;
}
inline int max(int a,int b){return a>b?a:b;}
///////////////////////////////////
enum{MAXN = 200005};
enum{MAXM = 200005};
enum{MAXQ = 200005};
int comp[MAXN], h[MAXN];
bool col_switch[MAXN];
int n_mem, mem_cv[MAXM], mem_cu[MAXM], mem_hcu[MAXM];
bool mem_swcv[MAXM];
int N, M, Q;
int E[2][MAXM], queries[2][MAXQ], res[MAXQ];
vec inds;


void init_dsu(int n){
    for(int i = 0; i < n; i++) {
        comp[i] = i;
        h[i] = 0;
        col_switch[i] = 0;
    }
    n_mem = 0;
}
pair find(int v){
    bool col = col_switch[v];
    while (comp[v] != v) {
        v = comp[v];
        col ^= 1;
        col ^= col_switch[v];
    }
    return newpair(v, col);
}
void merge(int v, int u){
    pair ansv = find(v), ansu = find(u);
    int cv = ansv.first, cu = ansu.first;
    bool colv = ansv.second, colu = ansu.second;
    if (cv != cu) {
        if (h[cv] > h[cu]) {
            swap(cv, cu);
            swap(v, u);
        }
        mem_cv[n_mem] = cv;
        mem_cu[n_mem] = cu;
        mem_hcu[n_mem] = h[cu];
        mem_swcv[n_mem] = col_switch[cv];
        n_mem++;
        comp[cv] = cu;
        if (h[cv] == h[cu]) h[cu]++;
        col_switch[cv] = (colv != colu);
    }
    else {
        mem_cv[n_mem] = -1;
        n_mem++;
    }
}
int check(int v, int u){
    pair ansv = find(v), ansu = find(u);
    int cv = ansv.first, cu = ansu.first;
    bool colv = ansv.second, colu = ansu.second;
    if (cv != cu)
        return 2;
    if (colv == colu)
        return -1;
    return 1;
}
void rollback(int k){
    while (k--) {
        n_mem--;
        int cv = mem_cv[n_mem], cu = mem_cu[n_mem];
        bool swcv = mem_swcv[n_mem];
        if (cv == -1)
            continue;
        comp[cv] = cv;
        h[cu] = cu;
        col_switch[cv] = swcv;
    }
}
void solve_for_inds(int e_left_next){
    int k = inds.sz;
    pair v[k];
    for(int i=0; i<k; i++){
        int ind = inds.ptr[i];
        v[i] = newpair(-queries[1][ind], ind);
    }
    qsort(v, k, sizeof(pair), cmpP);
    int e_right_next = M-1, steps = 0;
    bool got_cycle = false;
    for (int i = 0; i < k; i++) {
        int ind = v[i].second;
        int L = queries[0][ind], R = queries[1][ind];
        if (! got_cycle) {
            while (e_right_next > R) {
                int status = check(E[0][e_right_next], E[1][e_right_next]);
                if (status == -1) {
                    got_cycle = true;
                    break;
                }
                merge(E[0][e_right_next], E[1][e_right_next]);
                e_right_next--;
                steps++;
            }
        }
        bool got_cycle_inner = got_cycle;
        int steps_inner = 0;
        if (! got_cycle_inner) {
            for (int e = e_left_next; e < L; e++) {
                int status = check(E[0][e], E[1][e]);
                if (status == -1) {
                    got_cycle_inner = true;
                    break;
                }
                merge(E[0][e], E[1][e]);
                steps_inner++;
            }
        }
        res[ind] = (int)(got_cycle || got_cycle_inner);
        rollback(steps_inner);
    }
    rollback(steps);
}
void solve(){
    int B = max((int)(M / sqrt(Q)), 1);
    pair v[Q];
    for(int i = 0; i < Q; i++){
        v[i] = newpair(queries[0][i] / B, i);
    }
    qsort(v, Q, sizeof(pair), cmpP);
    init_dsu(N);
    int e_left_next = 0;
    bool got_cycle = false;
    inds = newVec();
    for (int i = 0; i < Q; i++) {
        pb(inds, v[i].second);
        if (i + 1 == Q || v[i].first != v[i+1].first) {
            while (! got_cycle && e_left_next < v[i].first * B) {
                int status = check(E[0][e_left_next], E[1][e_left_next]);
                if (status == -1) {
                    got_cycle = true;
                    break;
                }
                merge(E[0][e_left_next], E[1][e_left_next]);
                e_left_next++;
            }
            if (got_cycle) {
                for (int j = 0; j < inds.sz; j++) {
                    int ind = inds.ptr[j];
                    res[ind] = 1;
                }
            }
            else {
                solve_for_inds(e_left_next);
            }
            inds = newVec();
        }
    }
}
int main(){
    while(scanf("%d%d%d", &N, &M, &Q)==3){
        for(int i = 0; i < M; i++){
            scanf("%d %d", &E[0][i], &E[1][i]);
            E[0][i]--;
            E[1][i]--;
        }
        for(int i = 0; i < Q; i++){
            scanf("%d %d", &queries[0][i], &queries[1][i]);
            queries[0][i]--;
            queries[1][i]--;
        }
        solve();
        for(int i = 0; i < Q; i++){
            if (res[i])
                puts("YES");
            else
                puts("NO");
        }
    }
    return 0;
}

Compilation message

Joker.c: In function 'main':
Joker.c:203:13: warning: ignoring return value of 'scanf', declared with attribute warn_unused_result [-Wunused-result]
  203 |             scanf("%d %d", &E[0][i], &E[1][i]);
      |             ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Joker.c:208:13: warning: ignoring return value of 'scanf', declared with attribute warn_unused_result [-Wunused-result]
  208 |             scanf("%d %d", &queries[0][i], &queries[1][i]);
      |             ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 1 ms 288 KB Output is correct
3 Correct 0 ms 384 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
5 Correct 1 ms 384 KB Output is correct
6 Correct 1 ms 384 KB Output is correct
7 Correct 1 ms 384 KB Output is correct
8 Correct 1 ms 384 KB Output is correct
9 Correct 1 ms 384 KB Output is correct
10 Correct 1 ms 384 KB Output is correct
11 Correct 1 ms 384 KB Output is correct
12 Correct 1 ms 384 KB Output is correct
13 Correct 1 ms 384 KB Output is correct
14 Correct 1 ms 384 KB Output is correct
15 Correct 1 ms 384 KB Output is correct
16 Correct 1 ms 384 KB Output is correct
17 Correct 1 ms 384 KB Output is correct
18 Correct 1 ms 384 KB Output is correct
19 Correct 1 ms 384 KB Output is correct
20 Correct 1 ms 384 KB Output is correct
21 Correct 1 ms 384 KB Output is correct
22 Correct 1 ms 512 KB Output is correct
23 Correct 1 ms 384 KB Output is correct
24 Correct 1 ms 384 KB Output is correct
25 Correct 1 ms 384 KB Output is correct
26 Correct 1 ms 384 KB Output is correct
27 Correct 1 ms 384 KB Output is correct
28 Correct 1 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 1 ms 288 KB Output is correct
3 Correct 0 ms 384 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
5 Correct 1 ms 384 KB Output is correct
6 Correct 1 ms 384 KB Output is correct
7 Correct 1 ms 384 KB Output is correct
8 Correct 1 ms 384 KB Output is correct
9 Correct 1 ms 384 KB Output is correct
10 Correct 1 ms 384 KB Output is correct
11 Correct 1 ms 384 KB Output is correct
12 Correct 1 ms 384 KB Output is correct
13 Correct 1 ms 384 KB Output is correct
14 Correct 1 ms 384 KB Output is correct
15 Correct 1 ms 384 KB Output is correct
16 Correct 1 ms 384 KB Output is correct
17 Correct 1 ms 384 KB Output is correct
18 Correct 1 ms 384 KB Output is correct
19 Correct 1 ms 384 KB Output is correct
20 Correct 1 ms 384 KB Output is correct
21 Correct 1 ms 384 KB Output is correct
22 Correct 1 ms 512 KB Output is correct
23 Correct 1 ms 384 KB Output is correct
24 Correct 1 ms 384 KB Output is correct
25 Correct 1 ms 384 KB Output is correct
26 Correct 1 ms 384 KB Output is correct
27 Correct 1 ms 384 KB Output is correct
28 Correct 1 ms 384 KB Output is correct
29 Correct 3 ms 512 KB Output is correct
30 Correct 3 ms 512 KB Output is correct
31 Correct 3 ms 512 KB Output is correct
32 Correct 2 ms 512 KB Output is correct
33 Correct 2 ms 512 KB Output is correct
34 Correct 2 ms 512 KB Output is correct
35 Correct 3 ms 512 KB Output is correct
36 Correct 3 ms 512 KB Output is correct
37 Correct 3 ms 640 KB Output is correct
38 Correct 4 ms 512 KB Output is correct
39 Correct 3 ms 512 KB Output is correct
40 Correct 2 ms 512 KB Output is correct
41 Correct 3 ms 512 KB Output is correct
42 Correct 3 ms 512 KB Output is correct
43 Correct 2 ms 512 KB Output is correct
44 Correct 2 ms 512 KB Output is correct
45 Correct 2 ms 512 KB Output is correct
46 Correct 3 ms 512 KB Output is correct
47 Correct 3 ms 512 KB Output is correct
48 Correct 3 ms 640 KB Output is correct
49 Correct 3 ms 512 KB Output is correct
50 Correct 3 ms 512 KB Output is correct
51 Correct 3 ms 512 KB Output is correct
52 Correct 3 ms 512 KB Output is correct
53 Correct 3 ms 512 KB Output is correct
54 Correct 3 ms 512 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 1 ms 288 KB Output is correct
3 Correct 162 ms 16504 KB Output is correct
4 Correct 172 ms 18152 KB Output is correct
5 Correct 162 ms 16732 KB Output is correct
6 Correct 166 ms 16472 KB Output is correct
7 Correct 159 ms 16472 KB Output is correct
8 Correct 156 ms 15452 KB Output is correct
9 Correct 156 ms 15836 KB Output is correct
10 Correct 162 ms 17128 KB Output is correct
11 Correct 162 ms 16476 KB Output is correct
12 Correct 169 ms 17368 KB Output is correct
13 Correct 159 ms 15580 KB Output is correct
14 Correct 163 ms 16092 KB Output is correct
15 Correct 167 ms 16860 KB Output is correct
16 Correct 165 ms 17628 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 1 ms 288 KB Output is correct
3 Correct 0 ms 384 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
5 Correct 1 ms 384 KB Output is correct
6 Correct 1 ms 384 KB Output is correct
7 Correct 1 ms 384 KB Output is correct
8 Correct 1 ms 384 KB Output is correct
9 Correct 1 ms 384 KB Output is correct
10 Correct 1 ms 384 KB Output is correct
11 Correct 1 ms 384 KB Output is correct
12 Correct 1 ms 384 KB Output is correct
13 Correct 1 ms 384 KB Output is correct
14 Correct 1 ms 384 KB Output is correct
15 Correct 1 ms 384 KB Output is correct
16 Correct 1 ms 384 KB Output is correct
17 Correct 1 ms 384 KB Output is correct
18 Correct 1 ms 384 KB Output is correct
19 Correct 1 ms 384 KB Output is correct
20 Correct 1 ms 384 KB Output is correct
21 Correct 1 ms 384 KB Output is correct
22 Correct 1 ms 512 KB Output is correct
23 Correct 1 ms 384 KB Output is correct
24 Correct 1 ms 384 KB Output is correct
25 Correct 1 ms 384 KB Output is correct
26 Correct 1 ms 384 KB Output is correct
27 Correct 1 ms 384 KB Output is correct
28 Correct 1 ms 384 KB Output is correct
29 Correct 162 ms 16504 KB Output is correct
30 Correct 172 ms 18152 KB Output is correct
31 Correct 162 ms 16732 KB Output is correct
32 Correct 166 ms 16472 KB Output is correct
33 Correct 159 ms 16472 KB Output is correct
34 Correct 156 ms 15452 KB Output is correct
35 Correct 156 ms 15836 KB Output is correct
36 Correct 162 ms 17128 KB Output is correct
37 Correct 162 ms 16476 KB Output is correct
38 Correct 169 ms 17368 KB Output is correct
39 Correct 159 ms 15580 KB Output is correct
40 Correct 163 ms 16092 KB Output is correct
41 Correct 167 ms 16860 KB Output is correct
42 Correct 165 ms 17628 KB Output is correct
43 Correct 493 ms 16860 KB Output is correct
44 Correct 536 ms 18648 KB Output is correct
45 Correct 497 ms 18780 KB Output is correct
46 Correct 400 ms 16860 KB Output is correct
47 Correct 418 ms 16860 KB Output is correct
48 Correct 207 ms 16580 KB Output is correct
49 Correct 249 ms 17692 KB Output is correct
50 Correct 470 ms 16720 KB Output is correct
51 Correct 463 ms 17360 KB Output is correct
52 Correct 483 ms 17884 KB Output is correct
53 Correct 529 ms 15964 KB Output is correct
54 Correct 558 ms 16600 KB Output is correct
55 Correct 620 ms 17356 KB Output is correct
56 Correct 580 ms 17992 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 1 ms 288 KB Output is correct
3 Correct 0 ms 384 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
5 Correct 1 ms 384 KB Output is correct
6 Correct 1 ms 384 KB Output is correct
7 Correct 1 ms 384 KB Output is correct
8 Correct 1 ms 384 KB Output is correct
9 Correct 1 ms 384 KB Output is correct
10 Correct 1 ms 384 KB Output is correct
11 Correct 1 ms 384 KB Output is correct
12 Correct 1 ms 384 KB Output is correct
13 Correct 1 ms 384 KB Output is correct
14 Correct 1 ms 384 KB Output is correct
15 Correct 1 ms 384 KB Output is correct
16 Correct 1 ms 384 KB Output is correct
17 Correct 1 ms 384 KB Output is correct
18 Correct 1 ms 384 KB Output is correct
19 Correct 1 ms 384 KB Output is correct
20 Correct 1 ms 384 KB Output is correct
21 Correct 1 ms 384 KB Output is correct
22 Correct 1 ms 512 KB Output is correct
23 Correct 1 ms 384 KB Output is correct
24 Correct 1 ms 384 KB Output is correct
25 Correct 1 ms 384 KB Output is correct
26 Correct 1 ms 384 KB Output is correct
27 Correct 1 ms 384 KB Output is correct
28 Correct 1 ms 384 KB Output is correct
29 Correct 3 ms 512 KB Output is correct
30 Correct 3 ms 512 KB Output is correct
31 Correct 3 ms 512 KB Output is correct
32 Correct 2 ms 512 KB Output is correct
33 Correct 2 ms 512 KB Output is correct
34 Correct 2 ms 512 KB Output is correct
35 Correct 3 ms 512 KB Output is correct
36 Correct 3 ms 512 KB Output is correct
37 Correct 3 ms 640 KB Output is correct
38 Correct 4 ms 512 KB Output is correct
39 Correct 3 ms 512 KB Output is correct
40 Correct 2 ms 512 KB Output is correct
41 Correct 3 ms 512 KB Output is correct
42 Correct 3 ms 512 KB Output is correct
43 Correct 2 ms 512 KB Output is correct
44 Correct 2 ms 512 KB Output is correct
45 Correct 2 ms 512 KB Output is correct
46 Correct 3 ms 512 KB Output is correct
47 Correct 3 ms 512 KB Output is correct
48 Correct 3 ms 640 KB Output is correct
49 Correct 3 ms 512 KB Output is correct
50 Correct 3 ms 512 KB Output is correct
51 Correct 3 ms 512 KB Output is correct
52 Correct 3 ms 512 KB Output is correct
53 Correct 3 ms 512 KB Output is correct
54 Correct 3 ms 512 KB Output is correct
55 Correct 146 ms 6488 KB Output is correct
56 Correct 366 ms 8848 KB Output is correct
57 Correct 154 ms 8396 KB Output is correct
58 Correct 125 ms 6540 KB Output is correct
59 Correct 87 ms 6392 KB Output is correct
60 Correct 115 ms 7160 KB Output is correct
61 Correct 141 ms 6520 KB Output is correct
62 Correct 177 ms 7400 KB Output is correct
63 Correct 158 ms 5880 KB Output is correct
64 Correct 203 ms 6648 KB Output is correct
65 Correct 216 ms 7288 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 1 ms 288 KB Output is correct
3 Correct 0 ms 384 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
5 Correct 1 ms 384 KB Output is correct
6 Correct 1 ms 384 KB Output is correct
7 Correct 1 ms 384 KB Output is correct
8 Correct 1 ms 384 KB Output is correct
9 Correct 1 ms 384 KB Output is correct
10 Correct 1 ms 384 KB Output is correct
11 Correct 1 ms 384 KB Output is correct
12 Correct 1 ms 384 KB Output is correct
13 Correct 1 ms 384 KB Output is correct
14 Correct 1 ms 384 KB Output is correct
15 Correct 1 ms 384 KB Output is correct
16 Correct 1 ms 384 KB Output is correct
17 Correct 1 ms 384 KB Output is correct
18 Correct 1 ms 384 KB Output is correct
19 Correct 1 ms 384 KB Output is correct
20 Correct 1 ms 384 KB Output is correct
21 Correct 1 ms 384 KB Output is correct
22 Correct 1 ms 512 KB Output is correct
23 Correct 1 ms 384 KB Output is correct
24 Correct 1 ms 384 KB Output is correct
25 Correct 1 ms 384 KB Output is correct
26 Correct 1 ms 384 KB Output is correct
27 Correct 1 ms 384 KB Output is correct
28 Correct 1 ms 384 KB Output is correct
29 Correct 3 ms 512 KB Output is correct
30 Correct 3 ms 512 KB Output is correct
31 Correct 3 ms 512 KB Output is correct
32 Correct 2 ms 512 KB Output is correct
33 Correct 2 ms 512 KB Output is correct
34 Correct 2 ms 512 KB Output is correct
35 Correct 3 ms 512 KB Output is correct
36 Correct 3 ms 512 KB Output is correct
37 Correct 3 ms 640 KB Output is correct
38 Correct 4 ms 512 KB Output is correct
39 Correct 3 ms 512 KB Output is correct
40 Correct 2 ms 512 KB Output is correct
41 Correct 3 ms 512 KB Output is correct
42 Correct 3 ms 512 KB Output is correct
43 Correct 2 ms 512 KB Output is correct
44 Correct 2 ms 512 KB Output is correct
45 Correct 2 ms 512 KB Output is correct
46 Correct 3 ms 512 KB Output is correct
47 Correct 3 ms 512 KB Output is correct
48 Correct 3 ms 640 KB Output is correct
49 Correct 3 ms 512 KB Output is correct
50 Correct 3 ms 512 KB Output is correct
51 Correct 3 ms 512 KB Output is correct
52 Correct 3 ms 512 KB Output is correct
53 Correct 3 ms 512 KB Output is correct
54 Correct 3 ms 512 KB Output is correct
55 Correct 162 ms 16504 KB Output is correct
56 Correct 172 ms 18152 KB Output is correct
57 Correct 162 ms 16732 KB Output is correct
58 Correct 166 ms 16472 KB Output is correct
59 Correct 159 ms 16472 KB Output is correct
60 Correct 156 ms 15452 KB Output is correct
61 Correct 156 ms 15836 KB Output is correct
62 Correct 162 ms 17128 KB Output is correct
63 Correct 162 ms 16476 KB Output is correct
64 Correct 169 ms 17368 KB Output is correct
65 Correct 159 ms 15580 KB Output is correct
66 Correct 163 ms 16092 KB Output is correct
67 Correct 167 ms 16860 KB Output is correct
68 Correct 165 ms 17628 KB Output is correct
69 Correct 493 ms 16860 KB Output is correct
70 Correct 536 ms 18648 KB Output is correct
71 Correct 497 ms 18780 KB Output is correct
72 Correct 400 ms 16860 KB Output is correct
73 Correct 418 ms 16860 KB Output is correct
74 Correct 207 ms 16580 KB Output is correct
75 Correct 249 ms 17692 KB Output is correct
76 Correct 470 ms 16720 KB Output is correct
77 Correct 463 ms 17360 KB Output is correct
78 Correct 483 ms 17884 KB Output is correct
79 Correct 529 ms 15964 KB Output is correct
80 Correct 558 ms 16600 KB Output is correct
81 Correct 620 ms 17356 KB Output is correct
82 Correct 580 ms 17992 KB Output is correct
83 Correct 146 ms 6488 KB Output is correct
84 Correct 366 ms 8848 KB Output is correct
85 Correct 154 ms 8396 KB Output is correct
86 Correct 125 ms 6540 KB Output is correct
87 Correct 87 ms 6392 KB Output is correct
88 Correct 115 ms 7160 KB Output is correct
89 Correct 141 ms 6520 KB Output is correct
90 Correct 177 ms 7400 KB Output is correct
91 Correct 158 ms 5880 KB Output is correct
92 Correct 203 ms 6648 KB Output is correct
93 Correct 216 ms 7288 KB Output is correct
94 Correct 880 ms 14460 KB Output is correct
95 Execution timed out 2032 ms 16096 KB Time limit exceeded
96 Halted 0 ms 0 KB -