답안 #643372

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
643372 2022-09-21T22:33:22 Z Pajaraja Jail (JOI22_jail) C++17
100 / 100
1590 ms 359420 KB
#include <bits/stdc++.h>
#define MAXN 120007
#define MAXL 19
using namespace std;
vector<int> g[MAXN],v[3*MAXL*MAXN];
int p[MAXL][MAXN],ind[2][MAXL][MAXN],in[MAXN],out[MAXN],t,deg[MAXN];
void dfs(int s,int f)
{
    p[0][s]=f;
    in[s]=++t;
    for(int i=0;i<g[s].size();i++) if(g[s][i]!=f) dfs(g[s][i],s);
    out[s]=t;
}
bool insub(int uu,int vv) {return in[uu]<=in[vv] && out[uu]>=out[vv];} //Jel v insub u
int oneunder(int uu,int vv)
{
    for(int i=MAXL-1;i>=0;i--) if(!insub(p[i][uu],vv)) uu=p[i][uu];
    return uu;
}
int lca(int uu,int vv)
{
    if(insub(uu,vv)) return uu;
    if(insub(vv,uu)) return vv;
    return p[0][oneunder(uu,vv)];
}
void dostuff(int uu,int vv,int type,int br)
{
    for(int i=MAXL-1;i>=0;i--) if(!insub(p[i][uu],vv))
    {
        if(type==0) v[br].push_back(ind[0][i][uu]);
        else v[ind[1][i][uu]].push_back(br);
        uu=p[i][uu];
    }
    if(type==0) v[br].push_back(ind[0][0][uu]);
    else v[ind[1][0][uu]].push_back(br);
    if(type==0) v[br].push_back(ind[0][0][vv]);
    else v[ind[1][0][vv]].push_back(br);
}
int main()
{
    int q;
    scanf("%d",&q);
    while(q--)
    {
        int n,m;
        scanf("%d",&n);
        for(int i=1;i<n;i++)
        {
            int t1,t2;
            scanf("%d %d",&t1,&t2);
            g[t1].push_back(t2);
            g[t2].push_back(t1);
        }
        dfs(1,1); t=0;
        int c=2*n,br=0;
        for(int i=1;i<=n;i++) ind[0][0][i]=i;
        for(int i=1;i<=n;i++) ind[1][0][i]=n+i;
        for(int j=1;j<MAXL;j++) for(int i=1;i<=n;i++)
        {
            p[j][i]=p[j-1][p[j-1][i]];
            c++;
            ind[0][j][i]=c;
            v[c].push_back(ind[0][j-1][i]);
            v[c].push_back(ind[0][j-1][p[j-1][i]]);
            c++;
            ind[1][j][i]=c;
            v[ind[1][j-1][i]].push_back(c);
            v[ind[1][j-1][p[j-1][i]]].push_back(c);
        }
        scanf("%d",&m);
        for(int i=1;i<=m;i++)
        {
            c++;
            int s,e;
            scanf("%d%d",&s,&e);
            v[s].push_back(c);
            v[c].push_back(n+e);
            int lc=lca(s,e);
            if(lc!=s && lc!=e)
            {
                dostuff(p[0][s],lc,0,c);
                dostuff(e,oneunder(e,lc),0,c);
                dostuff(p[0][e],lc,1,c);
                dostuff(s,oneunder(s,lc),1,c);
            }
            if(lc==s)
            {
                dostuff(e,oneunder(e,s),0,c);
                dostuff(p[0][e],s,1,c);
            }
            if(lc==e)
            {
                dostuff(s,oneunder(s,e),1,c);
                dostuff(p[0][s],e,0,c);
            }
        }
        queue<int> q;
        for(int i=1;i<=c;i++) deg[i]=0;
        for(int i=1;i<=c;i++) for(int j=0;j<v[i].size();j++) deg[v[i][j]]++;
        for(int i=1;i<=c;i++) if(deg[i]==0) q.push(i);
        //for(int i=1;i<=c;i++) for(int j=0;j<v[i].size();j++) printf("%d %d\n",i,v[i][j]);
        while(!q.empty())
        {
            br++;
            int s=q.front(); q.pop();
            for(int i=0;i<v[s].size();i++) {deg[v[s][i]]--; if(deg[v[s][i]]==0) q.push(v[s][i]);}
        }
        //printf("%d %d\n",c,br);
        if(br==c) printf("Yes\n");
        else printf("No\n");
        for(int i=1;i<=c;i++) v[i].clear();
        for(int i=1;i<=n;i++) g[i].clear();
    }
}

Compilation message

jail.cpp: In function 'void dfs(int, int)':
jail.cpp:11:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   11 |     for(int i=0;i<g[s].size();i++) if(g[s][i]!=f) dfs(g[s][i],s);
      |                 ~^~~~~~~~~~~~
jail.cpp: In function 'int main()':
jail.cpp:99:44: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   99 |         for(int i=1;i<=c;i++) for(int j=0;j<v[i].size();j++) deg[v[i][j]]++;
      |                                           ~^~~~~~~~~~~~
jail.cpp:106:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  106 |             for(int i=0;i<v[s].size();i++) {deg[v[s][i]]--; if(deg[v[s][i]]==0) q.push(v[s][i]);}
      |                         ~^~~~~~~~~~~~
jail.cpp:42:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   42 |     scanf("%d",&q);
      |     ~~~~~^~~~~~~~~
jail.cpp:46:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   46 |         scanf("%d",&n);
      |         ~~~~~^~~~~~~~~
jail.cpp:50:18: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   50 |             scanf("%d %d",&t1,&t2);
      |             ~~~~~^~~~~~~~~~~~~~~~~
jail.cpp:70:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   70 |         scanf("%d",&m);
      |         ~~~~~^~~~~~~~~
jail.cpp:75:18: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   75 |             scanf("%d%d",&s,&e);
      |             ~~~~~^~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 74 ms 163940 KB Output is correct
2 Correct 78 ms 163988 KB Output is correct
3 Correct 77 ms 163956 KB Output is correct
4 Correct 160 ms 164568 KB Output is correct
5 Correct 246 ms 164980 KB Output is correct
6 Correct 82 ms 164468 KB Output is correct
7 Correct 83 ms 164416 KB Output is correct
8 Correct 83 ms 164496 KB Output is correct
9 Correct 388 ms 175328 KB Output is correct
10 Correct 639 ms 341872 KB Output is correct
11 Correct 112 ms 164300 KB Output is correct
12 Correct 268 ms 165112 KB Output is correct
13 Correct 923 ms 346416 KB Output is correct
14 Correct 661 ms 346432 KB Output is correct
15 Correct 946 ms 347780 KB Output is correct
16 Correct 1464 ms 357200 KB Output is correct
17 Correct 707 ms 348868 KB Output is correct
18 Correct 830 ms 350352 KB Output is correct
19 Correct 758 ms 348952 KB Output is correct
20 Correct 703 ms 348956 KB Output is correct
21 Correct 707 ms 349696 KB Output is correct
22 Correct 621 ms 346116 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 76 ms 164040 KB Output is correct
2 Correct 75 ms 164040 KB Output is correct
3 Correct 83 ms 164428 KB Output is correct
4 Correct 84 ms 164464 KB Output is correct
5 Correct 84 ms 164476 KB Output is correct
6 Correct 84 ms 164388 KB Output is correct
7 Correct 84 ms 164448 KB Output is correct
8 Correct 83 ms 164412 KB Output is correct
9 Correct 83 ms 164396 KB Output is correct
10 Correct 84 ms 164480 KB Output is correct
11 Correct 81 ms 164432 KB Output is correct
12 Correct 81 ms 164396 KB Output is correct
13 Correct 84 ms 164464 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 76 ms 164040 KB Output is correct
2 Correct 75 ms 164040 KB Output is correct
3 Correct 83 ms 164428 KB Output is correct
4 Correct 84 ms 164464 KB Output is correct
5 Correct 84 ms 164476 KB Output is correct
6 Correct 84 ms 164388 KB Output is correct
7 Correct 84 ms 164448 KB Output is correct
8 Correct 83 ms 164412 KB Output is correct
9 Correct 83 ms 164396 KB Output is correct
10 Correct 84 ms 164480 KB Output is correct
11 Correct 81 ms 164432 KB Output is correct
12 Correct 81 ms 164396 KB Output is correct
13 Correct 84 ms 164464 KB Output is correct
14 Correct 76 ms 164040 KB Output is correct
15 Correct 74 ms 164008 KB Output is correct
16 Correct 81 ms 164428 KB Output is correct
17 Correct 84 ms 164480 KB Output is correct
18 Correct 86 ms 164468 KB Output is correct
19 Correct 78 ms 164044 KB Output is correct
20 Correct 83 ms 164396 KB Output is correct
21 Correct 89 ms 164436 KB Output is correct
22 Correct 83 ms 164484 KB Output is correct
23 Correct 78 ms 163960 KB Output is correct
24 Correct 77 ms 164200 KB Output is correct
25 Correct 86 ms 164396 KB Output is correct
26 Correct 93 ms 164376 KB Output is correct
27 Correct 88 ms 164484 KB Output is correct
28 Correct 77 ms 164072 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 76 ms 164040 KB Output is correct
2 Correct 75 ms 164040 KB Output is correct
3 Correct 83 ms 164428 KB Output is correct
4 Correct 84 ms 164464 KB Output is correct
5 Correct 84 ms 164476 KB Output is correct
6 Correct 84 ms 164388 KB Output is correct
7 Correct 84 ms 164448 KB Output is correct
8 Correct 83 ms 164412 KB Output is correct
9 Correct 83 ms 164396 KB Output is correct
10 Correct 84 ms 164480 KB Output is correct
11 Correct 81 ms 164432 KB Output is correct
12 Correct 81 ms 164396 KB Output is correct
13 Correct 84 ms 164464 KB Output is correct
14 Correct 76 ms 164040 KB Output is correct
15 Correct 74 ms 164008 KB Output is correct
16 Correct 81 ms 164428 KB Output is correct
17 Correct 84 ms 164480 KB Output is correct
18 Correct 86 ms 164468 KB Output is correct
19 Correct 78 ms 164044 KB Output is correct
20 Correct 83 ms 164396 KB Output is correct
21 Correct 89 ms 164436 KB Output is correct
22 Correct 83 ms 164484 KB Output is correct
23 Correct 78 ms 163960 KB Output is correct
24 Correct 77 ms 164200 KB Output is correct
25 Correct 86 ms 164396 KB Output is correct
26 Correct 93 ms 164376 KB Output is correct
27 Correct 88 ms 164484 KB Output is correct
28 Correct 77 ms 164072 KB Output is correct
29 Correct 84 ms 164428 KB Output is correct
30 Correct 87 ms 164400 KB Output is correct
31 Correct 87 ms 164640 KB Output is correct
32 Correct 87 ms 164520 KB Output is correct
33 Correct 85 ms 164480 KB Output is correct
34 Correct 85 ms 164408 KB Output is correct
35 Correct 85 ms 164304 KB Output is correct
36 Correct 82 ms 164480 KB Output is correct
37 Correct 87 ms 164440 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 76 ms 164040 KB Output is correct
2 Correct 75 ms 164040 KB Output is correct
3 Correct 83 ms 164428 KB Output is correct
4 Correct 84 ms 164464 KB Output is correct
5 Correct 84 ms 164476 KB Output is correct
6 Correct 84 ms 164388 KB Output is correct
7 Correct 84 ms 164448 KB Output is correct
8 Correct 83 ms 164412 KB Output is correct
9 Correct 83 ms 164396 KB Output is correct
10 Correct 84 ms 164480 KB Output is correct
11 Correct 81 ms 164432 KB Output is correct
12 Correct 81 ms 164396 KB Output is correct
13 Correct 84 ms 164464 KB Output is correct
14 Correct 76 ms 164040 KB Output is correct
15 Correct 74 ms 164008 KB Output is correct
16 Correct 81 ms 164428 KB Output is correct
17 Correct 84 ms 164480 KB Output is correct
18 Correct 86 ms 164468 KB Output is correct
19 Correct 78 ms 164044 KB Output is correct
20 Correct 83 ms 164396 KB Output is correct
21 Correct 89 ms 164436 KB Output is correct
22 Correct 83 ms 164484 KB Output is correct
23 Correct 78 ms 163960 KB Output is correct
24 Correct 77 ms 164200 KB Output is correct
25 Correct 86 ms 164396 KB Output is correct
26 Correct 93 ms 164376 KB Output is correct
27 Correct 88 ms 164484 KB Output is correct
28 Correct 77 ms 164072 KB Output is correct
29 Correct 84 ms 164428 KB Output is correct
30 Correct 87 ms 164400 KB Output is correct
31 Correct 87 ms 164640 KB Output is correct
32 Correct 87 ms 164520 KB Output is correct
33 Correct 85 ms 164480 KB Output is correct
34 Correct 85 ms 164408 KB Output is correct
35 Correct 85 ms 164304 KB Output is correct
36 Correct 82 ms 164480 KB Output is correct
37 Correct 87 ms 164440 KB Output is correct
38 Correct 398 ms 174164 KB Output is correct
39 Correct 647 ms 340768 KB Output is correct
40 Correct 408 ms 176624 KB Output is correct
41 Correct 413 ms 175460 KB Output is correct
42 Correct 335 ms 176084 KB Output is correct
43 Correct 441 ms 176380 KB Output is correct
44 Correct 120 ms 165992 KB Output is correct
45 Correct 784 ms 341188 KB Output is correct
46 Correct 786 ms 341260 KB Output is correct
47 Correct 799 ms 338760 KB Output is correct
48 Correct 746 ms 338884 KB Output is correct
49 Correct 715 ms 341964 KB Output is correct
50 Correct 693 ms 341840 KB Output is correct
51 Correct 815 ms 341380 KB Output is correct
52 Correct 827 ms 341424 KB Output is correct
53 Correct 171 ms 178496 KB Output is correct
54 Correct 999 ms 342024 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 74 ms 163936 KB Output is correct
2 Correct 75 ms 163928 KB Output is correct
3 Correct 78 ms 164048 KB Output is correct
4 Correct 76 ms 163948 KB Output is correct
5 Correct 110 ms 164132 KB Output is correct
6 Correct 79 ms 164352 KB Output is correct
7 Correct 80 ms 164344 KB Output is correct
8 Correct 86 ms 163936 KB Output is correct
9 Correct 76 ms 164008 KB Output is correct
10 Correct 82 ms 164300 KB Output is correct
11 Correct 76 ms 164036 KB Output is correct
12 Correct 82 ms 164424 KB Output is correct
13 Correct 186 ms 164172 KB Output is correct
14 Correct 291 ms 164504 KB Output is correct
15 Correct 235 ms 164248 KB Output is correct
16 Correct 767 ms 341868 KB Output is correct
17 Correct 902 ms 350224 KB Output is correct
18 Correct 1195 ms 358048 KB Output is correct
19 Correct 865 ms 344828 KB Output is correct
20 Correct 867 ms 344452 KB Output is correct
21 Correct 884 ms 344420 KB Output is correct
22 Correct 815 ms 350364 KB Output is correct
23 Correct 853 ms 350704 KB Output is correct
24 Correct 1154 ms 350048 KB Output is correct
25 Correct 1149 ms 350160 KB Output is correct
26 Correct 1145 ms 350144 KB Output is correct
27 Correct 1570 ms 353564 KB Output is correct
28 Correct 937 ms 353576 KB Output is correct
29 Correct 952 ms 353628 KB Output is correct
30 Correct 1198 ms 347516 KB Output is correct
31 Correct 801 ms 347516 KB Output is correct
32 Correct 1153 ms 348732 KB Output is correct
33 Correct 817 ms 348632 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 74 ms 163940 KB Output is correct
2 Correct 78 ms 163988 KB Output is correct
3 Correct 77 ms 163956 KB Output is correct
4 Correct 160 ms 164568 KB Output is correct
5 Correct 246 ms 164980 KB Output is correct
6 Correct 82 ms 164468 KB Output is correct
7 Correct 83 ms 164416 KB Output is correct
8 Correct 83 ms 164496 KB Output is correct
9 Correct 388 ms 175328 KB Output is correct
10 Correct 639 ms 341872 KB Output is correct
11 Correct 112 ms 164300 KB Output is correct
12 Correct 268 ms 165112 KB Output is correct
13 Correct 923 ms 346416 KB Output is correct
14 Correct 661 ms 346432 KB Output is correct
15 Correct 946 ms 347780 KB Output is correct
16 Correct 1464 ms 357200 KB Output is correct
17 Correct 707 ms 348868 KB Output is correct
18 Correct 830 ms 350352 KB Output is correct
19 Correct 758 ms 348952 KB Output is correct
20 Correct 703 ms 348956 KB Output is correct
21 Correct 707 ms 349696 KB Output is correct
22 Correct 621 ms 346116 KB Output is correct
23 Correct 76 ms 164040 KB Output is correct
24 Correct 75 ms 164040 KB Output is correct
25 Correct 83 ms 164428 KB Output is correct
26 Correct 84 ms 164464 KB Output is correct
27 Correct 84 ms 164476 KB Output is correct
28 Correct 84 ms 164388 KB Output is correct
29 Correct 84 ms 164448 KB Output is correct
30 Correct 83 ms 164412 KB Output is correct
31 Correct 83 ms 164396 KB Output is correct
32 Correct 84 ms 164480 KB Output is correct
33 Correct 81 ms 164432 KB Output is correct
34 Correct 81 ms 164396 KB Output is correct
35 Correct 84 ms 164464 KB Output is correct
36 Correct 76 ms 164040 KB Output is correct
37 Correct 74 ms 164008 KB Output is correct
38 Correct 81 ms 164428 KB Output is correct
39 Correct 84 ms 164480 KB Output is correct
40 Correct 86 ms 164468 KB Output is correct
41 Correct 78 ms 164044 KB Output is correct
42 Correct 83 ms 164396 KB Output is correct
43 Correct 89 ms 164436 KB Output is correct
44 Correct 83 ms 164484 KB Output is correct
45 Correct 78 ms 163960 KB Output is correct
46 Correct 77 ms 164200 KB Output is correct
47 Correct 86 ms 164396 KB Output is correct
48 Correct 93 ms 164376 KB Output is correct
49 Correct 88 ms 164484 KB Output is correct
50 Correct 77 ms 164072 KB Output is correct
51 Correct 84 ms 164428 KB Output is correct
52 Correct 87 ms 164400 KB Output is correct
53 Correct 87 ms 164640 KB Output is correct
54 Correct 87 ms 164520 KB Output is correct
55 Correct 85 ms 164480 KB Output is correct
56 Correct 85 ms 164408 KB Output is correct
57 Correct 85 ms 164304 KB Output is correct
58 Correct 82 ms 164480 KB Output is correct
59 Correct 87 ms 164440 KB Output is correct
60 Correct 398 ms 174164 KB Output is correct
61 Correct 647 ms 340768 KB Output is correct
62 Correct 408 ms 176624 KB Output is correct
63 Correct 413 ms 175460 KB Output is correct
64 Correct 335 ms 176084 KB Output is correct
65 Correct 441 ms 176380 KB Output is correct
66 Correct 120 ms 165992 KB Output is correct
67 Correct 784 ms 341188 KB Output is correct
68 Correct 786 ms 341260 KB Output is correct
69 Correct 799 ms 338760 KB Output is correct
70 Correct 746 ms 338884 KB Output is correct
71 Correct 715 ms 341964 KB Output is correct
72 Correct 693 ms 341840 KB Output is correct
73 Correct 815 ms 341380 KB Output is correct
74 Correct 827 ms 341424 KB Output is correct
75 Correct 171 ms 178496 KB Output is correct
76 Correct 999 ms 342024 KB Output is correct
77 Correct 74 ms 163936 KB Output is correct
78 Correct 75 ms 163928 KB Output is correct
79 Correct 78 ms 164048 KB Output is correct
80 Correct 76 ms 163948 KB Output is correct
81 Correct 110 ms 164132 KB Output is correct
82 Correct 79 ms 164352 KB Output is correct
83 Correct 80 ms 164344 KB Output is correct
84 Correct 86 ms 163936 KB Output is correct
85 Correct 76 ms 164008 KB Output is correct
86 Correct 82 ms 164300 KB Output is correct
87 Correct 76 ms 164036 KB Output is correct
88 Correct 82 ms 164424 KB Output is correct
89 Correct 186 ms 164172 KB Output is correct
90 Correct 291 ms 164504 KB Output is correct
91 Correct 235 ms 164248 KB Output is correct
92 Correct 767 ms 341868 KB Output is correct
93 Correct 902 ms 350224 KB Output is correct
94 Correct 1195 ms 358048 KB Output is correct
95 Correct 865 ms 344828 KB Output is correct
96 Correct 867 ms 344452 KB Output is correct
97 Correct 884 ms 344420 KB Output is correct
98 Correct 815 ms 350364 KB Output is correct
99 Correct 853 ms 350704 KB Output is correct
100 Correct 1154 ms 350048 KB Output is correct
101 Correct 1149 ms 350160 KB Output is correct
102 Correct 1145 ms 350144 KB Output is correct
103 Correct 1570 ms 353564 KB Output is correct
104 Correct 937 ms 353576 KB Output is correct
105 Correct 952 ms 353628 KB Output is correct
106 Correct 1198 ms 347516 KB Output is correct
107 Correct 801 ms 347516 KB Output is correct
108 Correct 1153 ms 348732 KB Output is correct
109 Correct 817 ms 348632 KB Output is correct
110 Correct 297 ms 165324 KB Output is correct
111 Correct 233 ms 164940 KB Output is correct
112 Correct 1148 ms 350584 KB Output is correct
113 Correct 1178 ms 342724 KB Output is correct
114 Correct 917 ms 350000 KB Output is correct
115 Correct 549 ms 342344 KB Output is correct
116 Correct 1340 ms 348492 KB Output is correct
117 Correct 1278 ms 359420 KB Output is correct
118 Correct 1037 ms 342100 KB Output is correct
119 Correct 1010 ms 342088 KB Output is correct
120 Correct 167 ms 180848 KB Output is correct
121 Correct 1590 ms 348484 KB Output is correct
122 Correct 1587 ms 348384 KB Output is correct
123 Correct 1343 ms 343032 KB Output is correct
124 Correct 886 ms 343036 KB Output is correct
125 Correct 1335 ms 343488 KB Output is correct
126 Correct 1571 ms 359284 KB Output is correct
127 Correct 896 ms 352976 KB Output is correct
128 Correct 821 ms 352768 KB Output is correct
129 Correct 1143 ms 352748 KB Output is correct
130 Correct 813 ms 352784 KB Output is correct