Submission #815156

# Submission time Handle Problem Language Result Execution time Memory
815156 2023-08-08T12:47:34 Z jajco Jail (JOI22_jail) C++17
72 / 100
5000 ms 962908 KB
#include <ios>
#include <vector>
#include <algorithm>
#include <functional>
#define REP(i, n) for (int i=0; i<n; ++i)
typedef std::vector <int> vi;
int main(){
    int q;
    scanf("%d", &q);
    while (q--){
        int n;
        scanf("%d", &n);
        std::vector <vi> pol(n+1, vi());
        bool liniow=1;
        REP(i, n-1){ // wczytanie grafu
            int p,k;
            scanf("%d%d", &p, &k);
            if (p!=k-1)
                liniow=0;
            pol[p].emplace_back(k);
            pol[k].emplace_back(p);
        }
        vi gl(n+1),ojc(n+1);
        { // glebokosci
            std::function <void(int, int, int)> DFS=[&](
                    int i, int o, int g){
                ojc[i]=o;
                gl[i]=g;
                for (int j : pol[i])
                    if (j!=o)
                        DFS(j, i, g+1);
            };
            DFS(1, 0, 1);
        }
        int m;
        scanf("%d", &m);
        vi pocz(m),kon(m),perm,pn(n+1, -1),kn(n+1, -1);
        REP(i, m){ // wczytanie ziomow
            scanf("%d%d", &pocz[i], &kon[i]);
            pn[pocz[i]]=i,kn[kon[i]]=i;
        }
        std::vector <vi> dag(m, vi());
        vi ile(m);
        bool czy;
        /*if (liniow){
            int cp=-1,ck=-1;
            czy=1;
            for (int i=1; i<=n; ++i){
                if (pn[i]>=0){
                    if (ck==pn[i])
                        ck=-1;
                    if (cp>=0||ck>=0){
                        czy=0;
                        goto fin;}
                }
                if (kn[i]>=0){
                    if (cp==kn[i])
                        cp=-1;
                    if (cp>=0||ck>=0){
                        czy=0;
                        goto fin;
                    }
                }
            }
            goto fin;
        }*/
        auto mkkraw=[&](int i, int id){
            if (pn[i]>=0&&pn[i]!=id) // musimy byc po nim
                dag[pn[i]].emplace_back(id),++ile[id];
            if (kn[i]>=0&&kn[i]!=id) // musimy byc przed nim
                dag[id].emplace_back(kn[i]),++ile[kn[i]];
        };
        if (m<=500)
            REP(k, m){ // generacja daga dfsami
                static std::function <
                        bool(int, int, int, int)> DFS=[&](
                        int i, int ojc, int d, int id){
                    bool r=i==d;
                    if (!r)
                        for (int j : pol[i])
                            if (j!=ojc)
                                if (r|=DFS(j, i, d, id))
                                    break;
                    if (r)
                        mkkraw(i, id);
                    return r;
                };
                DFS(pocz[k], pocz[k], kon[k], k);
            }
        else{
            REP(i, m){ // generacja daga idac w gore do LCA
                int p=pocz[i],k=kon[i];
                if (gl[p]>gl[k])
                    std::swap(p, k);
                while (p!=k){ // p ma mniejsze gl
                    if (gl[p]==gl[k])
                        mkkraw(p, i),p=ojc[p];
                    else
                        mkkraw(k, i),k=ojc[k];
                }
                mkkraw(p, i);
            }
        }
        { // check daga
            vi zer;
            REP(i, m)
                if (!ile[i])
                    zer.emplace_back(i);
            int w=0;
            while (zer.size()){
                ++w;
                int p=zer.back();
                zer.pop_back();
                for (int i : dag[p])
                    if (!--ile[i])
                        zer.emplace_back(i);
            }
            czy=w==m;
        }
        fin:
        printf(czy ? "Yes\n" : "No\n");
    }
}

Compilation message

jail.cpp: In function 'int main()':
jail.cpp:14:14: warning: variable 'liniow' set but not used [-Wunused-but-set-variable]
   14 |         bool liniow=1;
      |              ^~~~~~
jail.cpp:120:9: warning: label 'fin' defined but not used [-Wunused-label]
  120 |         fin:
      |         ^~~
jail.cpp:9:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
    9 |     scanf("%d", &q);
      |     ~~~~~^~~~~~~~~~
jail.cpp:12:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   12 |         scanf("%d", &n);
      |         ~~~~~^~~~~~~~~~
jail.cpp:17:18: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   17 |             scanf("%d%d", &p, &k);
      |             ~~~~~^~~~~~~~~~~~~~~~
jail.cpp:36:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   36 |         scanf("%d", &m);
      |         ~~~~~^~~~~~~~~~
jail.cpp:39:18: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   39 |             scanf("%d%d", &pocz[i], &kon[i]);
      |             ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 12 ms 304 KB Output is correct
5 Correct 27 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
8 Correct 4 ms 340 KB Output is correct
9 Correct 332 ms 2740 KB Output is correct
10 Correct 1607 ms 25040 KB Output is correct
11 Correct 8 ms 212 KB Output is correct
12 Correct 70 ms 412 KB Output is correct
13 Correct 110 ms 51512 KB Output is correct
14 Correct 120 ms 65316 KB Output is correct
15 Execution timed out 5116 ms 962908 KB Time limit exceeded
16 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 280 KB Output is correct
3 Correct 2 ms 340 KB Output is correct
4 Correct 2 ms 280 KB Output is correct
5 Correct 2 ms 304 KB Output is correct
6 Correct 2 ms 340 KB Output is correct
7 Correct 2 ms 340 KB Output is correct
8 Correct 3 ms 340 KB Output is correct
9 Correct 2 ms 340 KB Output is correct
10 Correct 2 ms 212 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 280 KB Output is correct
3 Correct 2 ms 340 KB Output is correct
4 Correct 2 ms 280 KB Output is correct
5 Correct 2 ms 304 KB Output is correct
6 Correct 2 ms 340 KB Output is correct
7 Correct 2 ms 340 KB Output is correct
8 Correct 3 ms 340 KB Output is correct
9 Correct 2 ms 340 KB Output is correct
10 Correct 2 ms 212 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 2 ms 340 KB Output is correct
17 Correct 2 ms 340 KB Output is correct
18 Correct 2 ms 340 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 3 ms 340 KB Output is correct
21 Correct 2 ms 340 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 1 ms 212 KB Output is correct
24 Correct 1 ms 212 KB Output is correct
25 Correct 2 ms 340 KB Output is correct
26 Correct 1 ms 212 KB Output is correct
27 Correct 2 ms 344 KB Output is correct
28 Correct 1 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 280 KB Output is correct
3 Correct 2 ms 340 KB Output is correct
4 Correct 2 ms 280 KB Output is correct
5 Correct 2 ms 304 KB Output is correct
6 Correct 2 ms 340 KB Output is correct
7 Correct 2 ms 340 KB Output is correct
8 Correct 3 ms 340 KB Output is correct
9 Correct 2 ms 340 KB Output is correct
10 Correct 2 ms 212 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 2 ms 340 KB Output is correct
17 Correct 2 ms 340 KB Output is correct
18 Correct 2 ms 340 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 3 ms 340 KB Output is correct
21 Correct 2 ms 340 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 1 ms 212 KB Output is correct
24 Correct 1 ms 212 KB Output is correct
25 Correct 2 ms 340 KB Output is correct
26 Correct 1 ms 212 KB Output is correct
27 Correct 2 ms 344 KB Output is correct
28 Correct 1 ms 212 KB Output is correct
29 Correct 4 ms 424 KB Output is correct
30 Correct 3 ms 340 KB Output is correct
31 Correct 4 ms 340 KB Output is correct
32 Correct 4 ms 340 KB Output is correct
33 Correct 2 ms 340 KB Output is correct
34 Correct 5 ms 340 KB Output is correct
35 Correct 5 ms 340 KB Output is correct
36 Correct 3 ms 340 KB Output is correct
37 Correct 2 ms 308 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 280 KB Output is correct
3 Correct 2 ms 340 KB Output is correct
4 Correct 2 ms 280 KB Output is correct
5 Correct 2 ms 304 KB Output is correct
6 Correct 2 ms 340 KB Output is correct
7 Correct 2 ms 340 KB Output is correct
8 Correct 3 ms 340 KB Output is correct
9 Correct 2 ms 340 KB Output is correct
10 Correct 2 ms 212 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 2 ms 340 KB Output is correct
17 Correct 2 ms 340 KB Output is correct
18 Correct 2 ms 340 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 3 ms 340 KB Output is correct
21 Correct 2 ms 340 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 1 ms 212 KB Output is correct
24 Correct 1 ms 212 KB Output is correct
25 Correct 2 ms 340 KB Output is correct
26 Correct 1 ms 212 KB Output is correct
27 Correct 2 ms 344 KB Output is correct
28 Correct 1 ms 212 KB Output is correct
29 Correct 4 ms 424 KB Output is correct
30 Correct 3 ms 340 KB Output is correct
31 Correct 4 ms 340 KB Output is correct
32 Correct 4 ms 340 KB Output is correct
33 Correct 2 ms 340 KB Output is correct
34 Correct 5 ms 340 KB Output is correct
35 Correct 5 ms 340 KB Output is correct
36 Correct 3 ms 340 KB Output is correct
37 Correct 2 ms 308 KB Output is correct
38 Correct 329 ms 3268 KB Output is correct
39 Correct 1477 ms 25544 KB Output is correct
40 Correct 398 ms 2156 KB Output is correct
41 Correct 419 ms 1260 KB Output is correct
42 Correct 251 ms 2276 KB Output is correct
43 Correct 51 ms 1416 KB Output is correct
44 Correct 48 ms 644 KB Output is correct
45 Correct 1055 ms 9360 KB Output is correct
46 Correct 1085 ms 9352 KB Output is correct
47 Correct 609 ms 16768 KB Output is correct
48 Correct 629 ms 16844 KB Output is correct
49 Correct 710 ms 9660 KB Output is correct
50 Correct 684 ms 9548 KB Output is correct
51 Correct 227 ms 11828 KB Output is correct
52 Correct 226 ms 11684 KB Output is correct
53 Correct 44 ms 1156 KB Output is correct
54 Correct 992 ms 9276 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 7 ms 340 KB Output is correct
6 Correct 1 ms 300 KB Output is correct
7 Correct 1 ms 324 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 288 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 3 ms 340 KB Output is correct
13 Correct 36 ms 668 KB Output is correct
14 Correct 50 ms 720 KB Output is correct
15 Correct 46 ms 644 KB Output is correct
16 Correct 41 ms 10280 KB Output is correct
17 Correct 81 ms 17048 KB Output is correct
18 Correct 186 ms 38072 KB Output is correct
19 Correct 40 ms 10600 KB Output is correct
20 Correct 52 ms 10644 KB Output is correct
21 Correct 55 ms 10732 KB Output is correct
22 Correct 69 ms 17768 KB Output is correct
23 Correct 67 ms 18092 KB Output is correct
24 Correct 62 ms 17600 KB Output is correct
25 Correct 61 ms 17708 KB Output is correct
26 Correct 62 ms 17672 KB Output is correct
27 Correct 70 ms 17992 KB Output is correct
28 Correct 69 ms 17908 KB Output is correct
29 Correct 65 ms 17908 KB Output is correct
30 Correct 70 ms 13684 KB Output is correct
31 Correct 49 ms 13652 KB Output is correct
32 Correct 51 ms 13740 KB Output is correct
33 Correct 62 ms 13824 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 12 ms 304 KB Output is correct
5 Correct 27 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
8 Correct 4 ms 340 KB Output is correct
9 Correct 332 ms 2740 KB Output is correct
10 Correct 1607 ms 25040 KB Output is correct
11 Correct 8 ms 212 KB Output is correct
12 Correct 70 ms 412 KB Output is correct
13 Correct 110 ms 51512 KB Output is correct
14 Correct 120 ms 65316 KB Output is correct
15 Execution timed out 5116 ms 962908 KB Time limit exceeded
16 Halted 0 ms 0 KB -