답안 #815181

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
815181 2023-08-08T12:58:48 Z jajco Jail (JOI22_jail) C++17
72 / 100
5000 ms 1011872 KB
#include <ios>
#include <set>
#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&&0){
            std::set <int> v,rv;
            czy=1;
            REP(i, m){
                if (pocz[i]<kon[i]){
                    v.insert(pocz[i]);
                    v.insert(kon[i]);
                }
                else{
                    rv.insert(pocz[i]);
                    rv.insert(kon[i]);
                }
            }
            REP(i, m){
                if (pocz[i]<kon[i]){
                    auto itr=rv.lower_bound(pocz[i]);
                    if (itr!=rv.end()&&*itr<=kon[i])
                        czy=0;
                }
                else{
                    std::swap(pocz[i], kon[i]);
                    auto itr=v.lower_bound(pocz[i]);
                    if (itr!=v.end()&&*itr<=kon[i])
                        czy=0;
                }
            }
            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:10:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   10 |     scanf("%d", &q);
      |     ~~~~~^~~~~~~~~~
jail.cpp:13:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   13 |         scanf("%d", &n);
      |         ~~~~~^~~~~~~~~~
jail.cpp:18:18: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   18 |             scanf("%d%d", &p, &k);
      |             ~~~~~^~~~~~~~~~~~~~~~
jail.cpp:37:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   37 |         scanf("%d", &m);
      |         ~~~~~^~~~~~~~~~
jail.cpp:40:18: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   40 |             scanf("%d%d", &pocz[i], &kon[i]);
      |             ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 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 15 ms 300 KB Output is correct
5 Correct 27 ms 304 KB Output is correct
6 Correct 2 ms 328 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 5 ms 400 KB Output is correct
9 Correct 331 ms 2800 KB Output is correct
10 Correct 1593 ms 25060 KB Output is correct
11 Correct 7 ms 212 KB Output is correct
12 Correct 68 ms 328 KB Output is correct
13 Correct 137 ms 51464 KB Output is correct
14 Correct 131 ms 67032 KB Output is correct
15 Execution timed out 5093 ms 1011872 KB Time limit exceeded
16 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 2 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 2 ms 312 KB Output is correct
9 Correct 2 ms 296 KB Output is correct
10 Correct 2 ms 340 KB Output is correct
11 Correct 2 ms 340 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 2 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 2 ms 312 KB Output is correct
9 Correct 2 ms 296 KB Output is correct
10 Correct 2 ms 340 KB Output is correct
11 Correct 2 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 1 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 2 ms 340 KB Output is correct
21 Correct 2 ms 300 KB Output is correct
22 Correct 2 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 340 KB Output is correct
28 Correct 1 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 2 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 2 ms 312 KB Output is correct
9 Correct 2 ms 296 KB Output is correct
10 Correct 2 ms 340 KB Output is correct
11 Correct 2 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 1 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 2 ms 340 KB Output is correct
21 Correct 2 ms 300 KB Output is correct
22 Correct 2 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 340 KB Output is correct
28 Correct 1 ms 212 KB Output is correct
29 Correct 5 ms 596 KB Output is correct
30 Correct 3 ms 300 KB Output is correct
31 Correct 4 ms 340 KB Output is correct
32 Correct 3 ms 364 KB Output is correct
33 Correct 2 ms 340 KB Output is correct
34 Correct 6 ms 300 KB Output is correct
35 Correct 4 ms 300 KB Output is correct
36 Correct 3 ms 340 KB Output is correct
37 Correct 2 ms 300 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 2 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 2 ms 312 KB Output is correct
9 Correct 2 ms 296 KB Output is correct
10 Correct 2 ms 340 KB Output is correct
11 Correct 2 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 1 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 2 ms 340 KB Output is correct
21 Correct 2 ms 300 KB Output is correct
22 Correct 2 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 340 KB Output is correct
28 Correct 1 ms 212 KB Output is correct
29 Correct 5 ms 596 KB Output is correct
30 Correct 3 ms 300 KB Output is correct
31 Correct 4 ms 340 KB Output is correct
32 Correct 3 ms 364 KB Output is correct
33 Correct 2 ms 340 KB Output is correct
34 Correct 6 ms 300 KB Output is correct
35 Correct 4 ms 300 KB Output is correct
36 Correct 3 ms 340 KB Output is correct
37 Correct 2 ms 300 KB Output is correct
38 Correct 304 ms 4060 KB Output is correct
39 Correct 1520 ms 26452 KB Output is correct
40 Correct 401 ms 2700 KB Output is correct
41 Correct 387 ms 1932 KB Output is correct
42 Correct 249 ms 2824 KB Output is correct
43 Correct 51 ms 2148 KB Output is correct
44 Correct 48 ms 640 KB Output is correct
45 Correct 1057 ms 10260 KB Output is correct
46 Correct 1139 ms 10264 KB Output is correct
47 Correct 607 ms 17684 KB Output is correct
48 Correct 665 ms 17712 KB Output is correct
49 Correct 870 ms 10572 KB Output is correct
50 Correct 725 ms 10576 KB Output is correct
51 Correct 202 ms 12620 KB Output is correct
52 Correct 211 ms 12620 KB Output is correct
53 Correct 43 ms 1104 KB Output is correct
54 Correct 977 ms 10200 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 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 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 296 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 292 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 3 ms 340 KB Output is correct
13 Correct 36 ms 724 KB Output is correct
14 Correct 49 ms 1148 KB Output is correct
15 Correct 45 ms 996 KB Output is correct
16 Correct 36 ms 11100 KB Output is correct
17 Correct 90 ms 17880 KB Output is correct
18 Correct 179 ms 38712 KB Output is correct
19 Correct 43 ms 11432 KB Output is correct
20 Correct 40 ms 11428 KB Output is correct
21 Correct 56 ms 11516 KB Output is correct
22 Correct 67 ms 18420 KB Output is correct
23 Correct 63 ms 18856 KB Output is correct
24 Correct 62 ms 18376 KB Output is correct
25 Correct 65 ms 18484 KB Output is correct
26 Correct 69 ms 18364 KB Output is correct
27 Correct 74 ms 18740 KB Output is correct
28 Correct 80 ms 18884 KB Output is correct
29 Correct 69 ms 18708 KB Output is correct
30 Correct 62 ms 14512 KB Output is correct
31 Correct 49 ms 14408 KB Output is correct
32 Correct 51 ms 14480 KB Output is correct
33 Correct 48 ms 14408 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 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 15 ms 300 KB Output is correct
5 Correct 27 ms 304 KB Output is correct
6 Correct 2 ms 328 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 5 ms 400 KB Output is correct
9 Correct 331 ms 2800 KB Output is correct
10 Correct 1593 ms 25060 KB Output is correct
11 Correct 7 ms 212 KB Output is correct
12 Correct 68 ms 328 KB Output is correct
13 Correct 137 ms 51464 KB Output is correct
14 Correct 131 ms 67032 KB Output is correct
15 Execution timed out 5093 ms 1011872 KB Time limit exceeded
16 Halted 0 ms 0 KB -