Submission #815156

#TimeUsernameProblemLanguageResultExecution timeMemory
815156jajcoJail (JOI22_jail)C++17
72 / 100
5116 ms962908 KiB
#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 (stderr)

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 timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...