Submission #567199

# Submission time Handle Problem Language Result Execution time Memory
567199 2022-05-23T09:12:54 Z tqbfjotld Jail (JOI22_jail) C++14
100 / 100
1066 ms 129984 KB
#include <bits/stdc++.h>
using namespace std;

vector<int> adjl[120005];
int vis[600005];
int cur;
int fs[120005];
int ls[120005];

int A[120005];
int B[120005];

vector<int> adjl2[600005];
int curid = 0;

struct node{
    int s,e,inid,outid;
    node *l,*r;
    vector<int> stuff;
    node (int _s, int _e){
        s = _s; e = _e;
        inid = curid++;
        outid = curid++;
        //printf("inid outid %d %d %d\n",inid,outid,curid);
        if (s!=e){
            l = new node(s,(s+e)/2);
            r = new node((s+e)/2+1,e);
        }
    }
    void add(int a, int b, int val){
        if (a<=s && e<=b){
            stuff.push_back(val);
            return;
        }
        if (b<=(s+e)/2) l->add(a,b,val);
        else if (a>(s+e)/2) r->add(a,b,val);
        else {
            l->add(a,b,val);
            r->add(a,b,val);
        }
    }
    void constructstuff(){
        if (s!=e){
            l->constructstuff();
            r->constructstuff();
        }
        for (auto y : stuff){
            adjl2[inid].push_back(y);
            adjl2[y].push_back(outid);
        }
    }
    void constructbef(int pos, int val){
        adjl2[val].push_back(inid);
        if (s==e) return;
        if (pos<=(s+e)/2) l->constructbef(pos,val);
        else r->constructbef(pos,val);
    }
    void constructaft(int pos, int val){
        adjl2[outid].push_back(val);
        if (s==e) return;
        if (pos<=(s+e)/2) l->constructaft(pos,val);
        else r->constructaft(pos,val);
    }

}*root;


int p[120005];
int hd[120005];
int dep[120005];
int sz[120005];
int pre[120005];
int ct = 0;

void dfs(int nd){
    sz[nd] = 1;
    for (auto x : adjl[nd]){
        if (x==p[nd]){
            continue;
        }
        dep[x] = dep[nd]+1;
        p[x] = nd;
        dfs(x);
        sz[nd] += sz[x];
    }
}

void decomp(int nd, int head){
    hd[nd] = head;
    pre[nd] = ct++;
    int mx = 0;
    int mnd = -1;
    for (auto x : adjl[nd]){
        if (x==p[nd]) continue;
        if (sz[x]>mx){
            mx = sz[x];
            mnd = x;
        }
    }
    if (mnd==-1) return;
    decomp(mnd,head);
    for (auto x : adjl[nd]){
        if (x==p[nd]||x==mnd) continue;
        decomp(x,x);
    }
}


bool checkcycle(int nd){
    if (vis[nd]==2) return false;
    vis[nd] = 1;
    for (auto x : adjl2[nd]){
        if (vis[x]==1) return true;
        if (checkcycle(x)) return true;
    }
    vis[nd] = 2;
    return false;
}

int p2[120005][20];

int anc(int a, int amt){
    for (int x = 0; x<20; x++){
        if ((1<<x)&amt){
            a = p2[a][x];
        }
    }
    return a;
}

int main(){
    int Q;
    scanf("%d",&Q);
    while (Q--){
        int n;
        scanf("%d",&n);
        ct = 0;
        for (int x = 1; x<=n; x++){
            adjl[x].clear();
        }
        for (int x = 0; x<n-1; x++){
            int a,b;
            scanf("%d%d",&a,&b);
            adjl[a].push_back(b);
            adjl[b].push_back(a);
        }
        p[1] = 0;
        dfs(1);
        decomp(1,1);

        for (int x = 0; x<=n; x++){
            p2[x][0] = p[x];
        }
        for (int x = 1; x<20; x++){
            for (int y = 0; y<=n; y++){
                p2[y][x] = p2[p2[y][x-1]][x-1];
            }
        }

        int m;
        scanf("%d",&m);
        curid = m;
        for (int x = 1; x<=n; x++){
            fs[x] = -1;
            ls[x] = -1;
        }
        for (int x = 0; x<m; x++){
            scanf("%d%d",&A[x],&B[x]);
            fs[A[x]] = x;
            ls[B[x]] = x;
        }
        root = new node(0,n-1);
        for (int x = 0; x<m; x++){
            int a = A[x];
            int b = B[x];
            if (dep[a]>dep[b]) swap(a,b);
            if (p[b]==a) continue;
            if (anc(b,dep[b]-dep[a])==a){
                a = anc(b,dep[b]-dep[a]-1);
                b = p[b];
                //printf("hi\n");
            }
            else{
                a = p[a];
                b = p[b];
            }
            while (hd[a]!=hd[b]){
                if (dep[hd[a]]<dep[hd[b]]) swap(a,b);
                root->add(pre[hd[a]],pre[a],x);
                a = p[hd[a]];
            }
            if (pre[a]>pre[b])swap(a,b);
            root->add(pre[a],pre[b],x);
        }
        for (int x = 0; x<5*n+5; x++){
            adjl2[x].clear();
            vis[x] = 0;
        }
        root->constructstuff();
        for (int x = 1; x<=n; x++){
            if (fs[x]!=-1 && ls[x]!=-1) adjl2[fs[x]].push_back(ls[x]);
            if (fs[x]!=-1){
                root->constructbef(pre[x],fs[x]);
            }
            if (ls[x]!=-1){
                root->constructaft(pre[x],ls[x]);
            }
        }
        bool ans = true;
        for (int x = 0; x<curid; x++){
            if (checkcycle(x)){
                ans = false;
            }
        }
        printf(ans?"Yes\n":"No\n");
    }
}

Compilation message

jail.cpp: In function 'int main()':
jail.cpp:133:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  133 |     scanf("%d",&Q);
      |     ~~~~~^~~~~~~~~
jail.cpp:136:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  136 |         scanf("%d",&n);
      |         ~~~~~^~~~~~~~~
jail.cpp:143:18: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  143 |             scanf("%d%d",&a,&b);
      |             ~~~~~^~~~~~~~~~~~~~
jail.cpp:161:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  161 |         scanf("%d",&m);
      |         ~~~~~^~~~~~~~~
jail.cpp:168:18: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  168 |             scanf("%d%d",&A[x],&B[x]);
      |             ~~~~~^~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 9 ms 17236 KB Output is correct
2 Correct 9 ms 17272 KB Output is correct
3 Correct 8 ms 17200 KB Output is correct
4 Correct 27 ms 24268 KB Output is correct
5 Correct 49 ms 32516 KB Output is correct
6 Correct 11 ms 17920 KB Output is correct
7 Correct 13 ms 17876 KB Output is correct
8 Correct 13 ms 18004 KB Output is correct
9 Correct 67 ms 35940 KB Output is correct
10 Correct 86 ms 60696 KB Output is correct
11 Correct 20 ms 20004 KB Output is correct
12 Correct 77 ms 34724 KB Output is correct
13 Correct 204 ms 86892 KB Output is correct
14 Correct 193 ms 86820 KB Output is correct
15 Correct 339 ms 93556 KB Output is correct
16 Correct 731 ms 129984 KB Output is correct
17 Correct 218 ms 97028 KB Output is correct
18 Correct 217 ms 92756 KB Output is correct
19 Correct 224 ms 93264 KB Output is correct
20 Correct 222 ms 93108 KB Output is correct
21 Correct 242 ms 96836 KB Output is correct
22 Correct 145 ms 78192 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 17236 KB Output is correct
2 Correct 10 ms 17188 KB Output is correct
3 Correct 12 ms 17888 KB Output is correct
4 Correct 10 ms 17876 KB Output is correct
5 Correct 11 ms 17952 KB Output is correct
6 Correct 13 ms 17876 KB Output is correct
7 Correct 12 ms 17876 KB Output is correct
8 Correct 12 ms 17912 KB Output is correct
9 Correct 11 ms 17876 KB Output is correct
10 Correct 10 ms 17856 KB Output is correct
11 Correct 11 ms 17748 KB Output is correct
12 Correct 10 ms 17620 KB Output is correct
13 Correct 10 ms 17576 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 17236 KB Output is correct
2 Correct 10 ms 17188 KB Output is correct
3 Correct 12 ms 17888 KB Output is correct
4 Correct 10 ms 17876 KB Output is correct
5 Correct 11 ms 17952 KB Output is correct
6 Correct 13 ms 17876 KB Output is correct
7 Correct 12 ms 17876 KB Output is correct
8 Correct 12 ms 17912 KB Output is correct
9 Correct 11 ms 17876 KB Output is correct
10 Correct 10 ms 17856 KB Output is correct
11 Correct 11 ms 17748 KB Output is correct
12 Correct 10 ms 17620 KB Output is correct
13 Correct 10 ms 17576 KB Output is correct
14 Correct 8 ms 17236 KB Output is correct
15 Correct 11 ms 17236 KB Output is correct
16 Correct 11 ms 18004 KB Output is correct
17 Correct 11 ms 17876 KB Output is correct
18 Correct 10 ms 17876 KB Output is correct
19 Correct 9 ms 17236 KB Output is correct
20 Correct 11 ms 17876 KB Output is correct
21 Correct 10 ms 17908 KB Output is correct
22 Correct 10 ms 17876 KB Output is correct
23 Correct 9 ms 17236 KB Output is correct
24 Correct 9 ms 17296 KB Output is correct
25 Correct 11 ms 17932 KB Output is correct
26 Correct 9 ms 17372 KB Output is correct
27 Correct 11 ms 17876 KB Output is correct
28 Correct 9 ms 17236 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 17236 KB Output is correct
2 Correct 10 ms 17188 KB Output is correct
3 Correct 12 ms 17888 KB Output is correct
4 Correct 10 ms 17876 KB Output is correct
5 Correct 11 ms 17952 KB Output is correct
6 Correct 13 ms 17876 KB Output is correct
7 Correct 12 ms 17876 KB Output is correct
8 Correct 12 ms 17912 KB Output is correct
9 Correct 11 ms 17876 KB Output is correct
10 Correct 10 ms 17856 KB Output is correct
11 Correct 11 ms 17748 KB Output is correct
12 Correct 10 ms 17620 KB Output is correct
13 Correct 10 ms 17576 KB Output is correct
14 Correct 8 ms 17236 KB Output is correct
15 Correct 11 ms 17236 KB Output is correct
16 Correct 11 ms 18004 KB Output is correct
17 Correct 11 ms 17876 KB Output is correct
18 Correct 10 ms 17876 KB Output is correct
19 Correct 9 ms 17236 KB Output is correct
20 Correct 11 ms 17876 KB Output is correct
21 Correct 10 ms 17908 KB Output is correct
22 Correct 10 ms 17876 KB Output is correct
23 Correct 9 ms 17236 KB Output is correct
24 Correct 9 ms 17296 KB Output is correct
25 Correct 11 ms 17932 KB Output is correct
26 Correct 9 ms 17372 KB Output is correct
27 Correct 11 ms 17876 KB Output is correct
28 Correct 9 ms 17236 KB Output is correct
29 Correct 12 ms 18004 KB Output is correct
30 Correct 12 ms 18064 KB Output is correct
31 Correct 13 ms 18036 KB Output is correct
32 Correct 12 ms 18004 KB Output is correct
33 Correct 11 ms 17876 KB Output is correct
34 Correct 12 ms 17932 KB Output is correct
35 Correct 12 ms 17876 KB Output is correct
36 Correct 11 ms 17748 KB Output is correct
37 Correct 12 ms 17620 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 17236 KB Output is correct
2 Correct 10 ms 17188 KB Output is correct
3 Correct 12 ms 17888 KB Output is correct
4 Correct 10 ms 17876 KB Output is correct
5 Correct 11 ms 17952 KB Output is correct
6 Correct 13 ms 17876 KB Output is correct
7 Correct 12 ms 17876 KB Output is correct
8 Correct 12 ms 17912 KB Output is correct
9 Correct 11 ms 17876 KB Output is correct
10 Correct 10 ms 17856 KB Output is correct
11 Correct 11 ms 17748 KB Output is correct
12 Correct 10 ms 17620 KB Output is correct
13 Correct 10 ms 17576 KB Output is correct
14 Correct 8 ms 17236 KB Output is correct
15 Correct 11 ms 17236 KB Output is correct
16 Correct 11 ms 18004 KB Output is correct
17 Correct 11 ms 17876 KB Output is correct
18 Correct 10 ms 17876 KB Output is correct
19 Correct 9 ms 17236 KB Output is correct
20 Correct 11 ms 17876 KB Output is correct
21 Correct 10 ms 17908 KB Output is correct
22 Correct 10 ms 17876 KB Output is correct
23 Correct 9 ms 17236 KB Output is correct
24 Correct 9 ms 17296 KB Output is correct
25 Correct 11 ms 17932 KB Output is correct
26 Correct 9 ms 17372 KB Output is correct
27 Correct 11 ms 17876 KB Output is correct
28 Correct 9 ms 17236 KB Output is correct
29 Correct 12 ms 18004 KB Output is correct
30 Correct 12 ms 18064 KB Output is correct
31 Correct 13 ms 18036 KB Output is correct
32 Correct 12 ms 18004 KB Output is correct
33 Correct 11 ms 17876 KB Output is correct
34 Correct 12 ms 17932 KB Output is correct
35 Correct 12 ms 17876 KB Output is correct
36 Correct 11 ms 17748 KB Output is correct
37 Correct 12 ms 17620 KB Output is correct
38 Correct 64 ms 35852 KB Output is correct
39 Correct 84 ms 60696 KB Output is correct
40 Correct 75 ms 35556 KB Output is correct
41 Correct 79 ms 35292 KB Output is correct
42 Correct 62 ms 35020 KB Output is correct
43 Correct 51 ms 33476 KB Output is correct
44 Correct 28 ms 20820 KB Output is correct
45 Correct 103 ms 51688 KB Output is correct
46 Correct 102 ms 51772 KB Output is correct
47 Correct 83 ms 56204 KB Output is correct
48 Correct 86 ms 56268 KB Output is correct
49 Correct 96 ms 51828 KB Output is correct
50 Correct 93 ms 51864 KB Output is correct
51 Correct 82 ms 52688 KB Output is correct
52 Correct 99 ms 52756 KB Output is correct
53 Correct 28 ms 22548 KB Output is correct
54 Correct 125 ms 51684 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 17236 KB Output is correct
2 Correct 9 ms 17236 KB Output is correct
3 Correct 10 ms 17236 KB Output is correct
4 Correct 12 ms 17268 KB Output is correct
5 Correct 26 ms 20100 KB Output is correct
6 Correct 11 ms 17620 KB Output is correct
7 Correct 10 ms 17620 KB Output is correct
8 Correct 9 ms 17212 KB Output is correct
9 Correct 9 ms 17288 KB Output is correct
10 Correct 10 ms 17236 KB Output is correct
11 Correct 12 ms 17236 KB Output is correct
12 Correct 12 ms 17748 KB Output is correct
13 Correct 52 ms 26384 KB Output is correct
14 Correct 78 ms 33940 KB Output is correct
15 Correct 68 ms 29828 KB Output is correct
16 Correct 172 ms 57076 KB Output is correct
17 Correct 442 ms 78668 KB Output is correct
18 Correct 801 ms 103432 KB Output is correct
19 Correct 231 ms 62468 KB Output is correct
20 Correct 243 ms 62568 KB Output is correct
21 Correct 236 ms 62628 KB Output is correct
22 Correct 423 ms 76480 KB Output is correct
23 Correct 331 ms 76584 KB Output is correct
24 Correct 331 ms 76844 KB Output is correct
25 Correct 340 ms 76604 KB Output is correct
26 Correct 326 ms 76856 KB Output is correct
27 Correct 461 ms 86356 KB Output is correct
28 Correct 409 ms 90512 KB Output is correct
29 Correct 387 ms 87480 KB Output is correct
30 Correct 364 ms 77880 KB Output is correct
31 Correct 340 ms 78928 KB Output is correct
32 Correct 363 ms 76112 KB Output is correct
33 Correct 373 ms 78992 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 17236 KB Output is correct
2 Correct 9 ms 17272 KB Output is correct
3 Correct 8 ms 17200 KB Output is correct
4 Correct 27 ms 24268 KB Output is correct
5 Correct 49 ms 32516 KB Output is correct
6 Correct 11 ms 17920 KB Output is correct
7 Correct 13 ms 17876 KB Output is correct
8 Correct 13 ms 18004 KB Output is correct
9 Correct 67 ms 35940 KB Output is correct
10 Correct 86 ms 60696 KB Output is correct
11 Correct 20 ms 20004 KB Output is correct
12 Correct 77 ms 34724 KB Output is correct
13 Correct 204 ms 86892 KB Output is correct
14 Correct 193 ms 86820 KB Output is correct
15 Correct 339 ms 93556 KB Output is correct
16 Correct 731 ms 129984 KB Output is correct
17 Correct 218 ms 97028 KB Output is correct
18 Correct 217 ms 92756 KB Output is correct
19 Correct 224 ms 93264 KB Output is correct
20 Correct 222 ms 93108 KB Output is correct
21 Correct 242 ms 96836 KB Output is correct
22 Correct 145 ms 78192 KB Output is correct
23 Correct 10 ms 17236 KB Output is correct
24 Correct 10 ms 17188 KB Output is correct
25 Correct 12 ms 17888 KB Output is correct
26 Correct 10 ms 17876 KB Output is correct
27 Correct 11 ms 17952 KB Output is correct
28 Correct 13 ms 17876 KB Output is correct
29 Correct 12 ms 17876 KB Output is correct
30 Correct 12 ms 17912 KB Output is correct
31 Correct 11 ms 17876 KB Output is correct
32 Correct 10 ms 17856 KB Output is correct
33 Correct 11 ms 17748 KB Output is correct
34 Correct 10 ms 17620 KB Output is correct
35 Correct 10 ms 17576 KB Output is correct
36 Correct 8 ms 17236 KB Output is correct
37 Correct 11 ms 17236 KB Output is correct
38 Correct 11 ms 18004 KB Output is correct
39 Correct 11 ms 17876 KB Output is correct
40 Correct 10 ms 17876 KB Output is correct
41 Correct 9 ms 17236 KB Output is correct
42 Correct 11 ms 17876 KB Output is correct
43 Correct 10 ms 17908 KB Output is correct
44 Correct 10 ms 17876 KB Output is correct
45 Correct 9 ms 17236 KB Output is correct
46 Correct 9 ms 17296 KB Output is correct
47 Correct 11 ms 17932 KB Output is correct
48 Correct 9 ms 17372 KB Output is correct
49 Correct 11 ms 17876 KB Output is correct
50 Correct 9 ms 17236 KB Output is correct
51 Correct 12 ms 18004 KB Output is correct
52 Correct 12 ms 18064 KB Output is correct
53 Correct 13 ms 18036 KB Output is correct
54 Correct 12 ms 18004 KB Output is correct
55 Correct 11 ms 17876 KB Output is correct
56 Correct 12 ms 17932 KB Output is correct
57 Correct 12 ms 17876 KB Output is correct
58 Correct 11 ms 17748 KB Output is correct
59 Correct 12 ms 17620 KB Output is correct
60 Correct 64 ms 35852 KB Output is correct
61 Correct 84 ms 60696 KB Output is correct
62 Correct 75 ms 35556 KB Output is correct
63 Correct 79 ms 35292 KB Output is correct
64 Correct 62 ms 35020 KB Output is correct
65 Correct 51 ms 33476 KB Output is correct
66 Correct 28 ms 20820 KB Output is correct
67 Correct 103 ms 51688 KB Output is correct
68 Correct 102 ms 51772 KB Output is correct
69 Correct 83 ms 56204 KB Output is correct
70 Correct 86 ms 56268 KB Output is correct
71 Correct 96 ms 51828 KB Output is correct
72 Correct 93 ms 51864 KB Output is correct
73 Correct 82 ms 52688 KB Output is correct
74 Correct 99 ms 52756 KB Output is correct
75 Correct 28 ms 22548 KB Output is correct
76 Correct 125 ms 51684 KB Output is correct
77 Correct 9 ms 17236 KB Output is correct
78 Correct 9 ms 17236 KB Output is correct
79 Correct 10 ms 17236 KB Output is correct
80 Correct 12 ms 17268 KB Output is correct
81 Correct 26 ms 20100 KB Output is correct
82 Correct 11 ms 17620 KB Output is correct
83 Correct 10 ms 17620 KB Output is correct
84 Correct 9 ms 17212 KB Output is correct
85 Correct 9 ms 17288 KB Output is correct
86 Correct 10 ms 17236 KB Output is correct
87 Correct 12 ms 17236 KB Output is correct
88 Correct 12 ms 17748 KB Output is correct
89 Correct 52 ms 26384 KB Output is correct
90 Correct 78 ms 33940 KB Output is correct
91 Correct 68 ms 29828 KB Output is correct
92 Correct 172 ms 57076 KB Output is correct
93 Correct 442 ms 78668 KB Output is correct
94 Correct 801 ms 103432 KB Output is correct
95 Correct 231 ms 62468 KB Output is correct
96 Correct 243 ms 62568 KB Output is correct
97 Correct 236 ms 62628 KB Output is correct
98 Correct 423 ms 76480 KB Output is correct
99 Correct 331 ms 76584 KB Output is correct
100 Correct 331 ms 76844 KB Output is correct
101 Correct 340 ms 76604 KB Output is correct
102 Correct 326 ms 76856 KB Output is correct
103 Correct 461 ms 86356 KB Output is correct
104 Correct 409 ms 90512 KB Output is correct
105 Correct 387 ms 87480 KB Output is correct
106 Correct 364 ms 77880 KB Output is correct
107 Correct 340 ms 78928 KB Output is correct
108 Correct 363 ms 76112 KB Output is correct
109 Correct 373 ms 78992 KB Output is correct
110 Correct 74 ms 33740 KB Output is correct
111 Correct 50 ms 29700 KB Output is correct
112 Correct 401 ms 91536 KB Output is correct
113 Correct 200 ms 70148 KB Output is correct
114 Correct 256 ms 82552 KB Output is correct
115 Correct 79 ms 53472 KB Output is correct
116 Correct 330 ms 70720 KB Output is correct
117 Correct 1066 ms 125284 KB Output is correct
118 Correct 116 ms 52044 KB Output is correct
119 Correct 116 ms 52072 KB Output is correct
120 Correct 26 ms 21916 KB Output is correct
121 Correct 415 ms 73640 KB Output is correct
122 Correct 395 ms 73624 KB Output is correct
123 Correct 222 ms 73676 KB Output is correct
124 Correct 211 ms 73652 KB Output is correct
125 Correct 239 ms 75492 KB Output is correct
126 Correct 665 ms 123852 KB Output is correct
127 Correct 289 ms 96960 KB Output is correct
128 Correct 252 ms 97420 KB Output is correct
129 Correct 274 ms 97004 KB Output is correct
130 Correct 247 ms 97436 KB Output is correct