Submission #962466

# Submission time Handle Problem Language Result Execution time Memory
962466 2024-04-13T15:33:45 Z huutuan Jail (JOI22_jail) C++14
77 / 100
5000 ms 227020 KB
#include<bits/stdc++.h>

using namespace std;

// #define int long long
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define isz(x) ((int)x.size())
#define sumof(x) accumulate(all(x), 0ll)

const int N=2e5+10, LG=18;
vector<int> gg[N*6];
int idx[2][N*6], pos[2][N];
int deg[N*6];

struct SegmentTree{
   int n, m;
   void init(int _n){
      n=_n;
      m=n*2;
   }
   void build(int k, int l, int r, int ps[N], int pt[N]){
      if (l==r){
         idx[0][k]=pt[l];
         idx[1][k]=ps[l]?ps[l]+n:0;
         if (pt[l]) pos[0][pt[l]]=l;
         if (ps[l]) pos[1][ps[l]]=l;
         return;
      }
      idx[0][k]=++m;
      idx[1][k]=++m;
      int mid=(l+r)>>1;
      build(k<<1, l, mid, ps, pt);
      build(k<<1|1, mid+1, r, ps, pt);
      if (idx[0][k<<1]) gg[idx[0][k]].push_back(idx[0][k<<1]), ++deg[idx[0][k<<1]];
      if (idx[0][k<<1|1]) gg[idx[0][k]].push_back(idx[0][k<<1|1]), ++deg[idx[0][k<<1|1]];
      if (idx[1][k<<1]) gg[idx[1][k<<1]].push_back(idx[1][k]), ++deg[idx[1][k]];
      if (idx[1][k<<1|1]) gg[idx[1][k<<1|1]].push_back(idx[1][k]), ++deg[idx[1][k]];
   }
   void update(int k, int l, int r, int L, int R, int i, bool type){
      if (r<L || R<l) return;
      if (L<=l && r<=R){
         if (!type){
            if (idx[0][k]) gg[i+n].push_back(idx[0][k]), ++deg[idx[0][k]];
         }else{
            if (idx[1][k]) gg[idx[1][k]].push_back(i), ++deg[i];
         }
         return;
      }
      int mid=(l+r)>>1;
      update(k<<1, l, mid, L, R, i, type);
      update(k<<1|1, mid+1, r, L, R, i, type);
   }
   void _update(int k, int l, int r, int L, int R, int i, bool type){
      if (L<=pos[type][i] && pos[type][i]<=R){
         update(k, l, r, L, pos[type][i]-1, i, type);
         update(k, l, r, pos[type][i]+1, R, i, type);
      }else update(k, l, r, L, R, i, type);
   }
} st;

int n, m, tin[N], tout[N], tdfs, dep[N], vis[N*6], par[N], head[N], sz[N];
pair<int, int> a[N];
int check;
vector<int> g[N];

void dfs_sz(int u, int p){
   if (p) g[u].erase(find(all(g[u]), p));
   dep[u]=dep[p]+1;
   par[u]=p;
   sz[u]=1;
   for (int &v:g[u]){
      dfs_sz(v, u);
      if (sz[v]>sz[g[u][0]]) swap(v, g[u][0]);
   }
}

void dfs_hld(int u, int h){
   tin[u]=++tdfs;
   head[u]=h;
   for (int v:g[u]) dfs_hld(v, v==g[u][0]?h:v);
   tout[u]=tdfs;
}

void update(int u, int v, int i){
   while (head[u]!=head[v]){
      if (dep[head[u]]<dep[head[v]]) swap(u, v);
      st._update(1, 1, n, tin[head[u]], tin[u], i, 0);
      st._update(1, 1, n, tin[head[u]], tin[u], i, 1);
      u=par[head[u]];
   }
   if (dep[u]>dep[v]) swap(u, v);
   st._update(1, 1, n, tin[u], tin[v], i, 0);
   st._update(1, 1, n, tin[u], tin[v], i, 1);
}

void dfs2(int u){
   vis[u]=1;
   for (int v:gg[u]){
      if (!vis[v]) dfs2(v);
      else if (vis[v]==1) check=0;
   }
   vis[u]=2;
}

int ps[N], pt[N];

void solve(){
   check=1;
   cin >> n;
   for (int i=1; i<n; ++i){
      int u, v; cin >> u >> v;
      g[u].push_back(v);
      g[v].push_back(u);
   }
   dfs_sz(1, 0);
   dfs_hld(1, 1);
   cin >> m;
   for (int i=1; i<=m; ++i){
      cin >> a[i].first >> a[i].second;
      ps[tin[a[i].first]]=i;
      pt[tin[a[i].second]]=i;
   }
   st.init(n);
   st.build(1, 1, n, ps, pt);
   for (int i=1; i<=m; ++i) gg[i].push_back(i+n), ++deg[i+n];
   // for (int i=1; i<=n*6; ++i) for (int j:gg[i]) cout << i << ' ' << j << endl;
   for (int i=1; i<=m; ++i) update(a[i].first, a[i].second, i);
   // for (int i=1; i<=n*6; ++i) for (int j:gg[i]) cout << i << ' ' << j << endl;
   for (int i=1; i<=n*6; ++i) if (!deg[i]) dfs2(i);
   check&=accumulate(vis+1, vis+n*6+1, 0ll)==2*n*6;
   cout << (check?"Yes\n":"No\n");
   for (int i=1; i<=n*6; ++i) g[i].clear(), gg[i].clear(), deg[i]=vis[i]=0, ps[i]=pt[i]=0;
   tdfs=0;
}

int32_t main(){
   ios_base::sync_with_stdio(false);
   cin.tie(nullptr);
   int ntests=1;
   cin >> ntests;
   for (int i=1; i<=ntests; ++i) solve();
   return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 11 ms 38748 KB Output is correct
2 Correct 10 ms 38748 KB Output is correct
3 Correct 8 ms 36728 KB Output is correct
4 Correct 18 ms 39212 KB Output is correct
5 Correct 28 ms 37568 KB Output is correct
6 Correct 11 ms 38748 KB Output is correct
7 Correct 10 ms 38744 KB Output is correct
8 Correct 12 ms 38748 KB Output is correct
9 Correct 42 ms 41652 KB Output is correct
10 Correct 59 ms 66384 KB Output is correct
11 Correct 14 ms 36700 KB Output is correct
12 Correct 48 ms 41780 KB Output is correct
13 Correct 139 ms 77396 KB Output is correct
14 Correct 146 ms 75536 KB Output is correct
15 Correct 317 ms 81016 KB Output is correct
16 Correct 638 ms 109040 KB Output is correct
17 Correct 169 ms 82536 KB Output is correct
18 Correct 163 ms 78532 KB Output is correct
19 Correct 155 ms 80196 KB Output is correct
20 Correct 156 ms 80492 KB Output is correct
21 Correct 184 ms 82200 KB Output is correct
22 Correct 103 ms 73296 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 38848 KB Output is correct
2 Correct 9 ms 38600 KB Output is correct
3 Correct 12 ms 36892 KB Output is correct
4 Correct 10 ms 38744 KB Output is correct
5 Correct 11 ms 39000 KB Output is correct
6 Correct 9 ms 36696 KB Output is correct
7 Correct 9 ms 36700 KB Output is correct
8 Correct 9 ms 36700 KB Output is correct
9 Correct 10 ms 38932 KB Output is correct
10 Correct 9 ms 36700 KB Output is correct
11 Correct 10 ms 36700 KB Output is correct
12 Correct 11 ms 38748 KB Output is correct
13 Correct 9 ms 36696 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 38848 KB Output is correct
2 Correct 9 ms 38600 KB Output is correct
3 Correct 12 ms 36892 KB Output is correct
4 Correct 10 ms 38744 KB Output is correct
5 Correct 11 ms 39000 KB Output is correct
6 Correct 9 ms 36696 KB Output is correct
7 Correct 9 ms 36700 KB Output is correct
8 Correct 9 ms 36700 KB Output is correct
9 Correct 10 ms 38932 KB Output is correct
10 Correct 9 ms 36700 KB Output is correct
11 Correct 10 ms 36700 KB Output is correct
12 Correct 11 ms 38748 KB Output is correct
13 Correct 9 ms 36696 KB Output is correct
14 Correct 9 ms 38748 KB Output is correct
15 Correct 9 ms 38748 KB Output is correct
16 Correct 10 ms 38744 KB Output is correct
17 Correct 10 ms 38928 KB Output is correct
18 Correct 10 ms 38748 KB Output is correct
19 Correct 9 ms 38748 KB Output is correct
20 Correct 10 ms 36700 KB Output is correct
21 Correct 9 ms 36696 KB Output is correct
22 Correct 9 ms 36696 KB Output is correct
23 Correct 9 ms 38744 KB Output is correct
24 Correct 9 ms 38748 KB Output is correct
25 Correct 9 ms 36956 KB Output is correct
26 Correct 9 ms 38744 KB Output is correct
27 Correct 10 ms 38748 KB Output is correct
28 Correct 9 ms 36700 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 38848 KB Output is correct
2 Correct 9 ms 38600 KB Output is correct
3 Correct 12 ms 36892 KB Output is correct
4 Correct 10 ms 38744 KB Output is correct
5 Correct 11 ms 39000 KB Output is correct
6 Correct 9 ms 36696 KB Output is correct
7 Correct 9 ms 36700 KB Output is correct
8 Correct 9 ms 36700 KB Output is correct
9 Correct 10 ms 38932 KB Output is correct
10 Correct 9 ms 36700 KB Output is correct
11 Correct 10 ms 36700 KB Output is correct
12 Correct 11 ms 38748 KB Output is correct
13 Correct 9 ms 36696 KB Output is correct
14 Correct 9 ms 38748 KB Output is correct
15 Correct 9 ms 38748 KB Output is correct
16 Correct 10 ms 38744 KB Output is correct
17 Correct 10 ms 38928 KB Output is correct
18 Correct 10 ms 38748 KB Output is correct
19 Correct 9 ms 38748 KB Output is correct
20 Correct 10 ms 36700 KB Output is correct
21 Correct 9 ms 36696 KB Output is correct
22 Correct 9 ms 36696 KB Output is correct
23 Correct 9 ms 38744 KB Output is correct
24 Correct 9 ms 38748 KB Output is correct
25 Correct 9 ms 36956 KB Output is correct
26 Correct 9 ms 38744 KB Output is correct
27 Correct 10 ms 38748 KB Output is correct
28 Correct 9 ms 36700 KB Output is correct
29 Correct 11 ms 38980 KB Output is correct
30 Correct 12 ms 39004 KB Output is correct
31 Correct 12 ms 36956 KB Output is correct
32 Correct 12 ms 38864 KB Output is correct
33 Correct 10 ms 38744 KB Output is correct
34 Correct 12 ms 39260 KB Output is correct
35 Correct 20 ms 38992 KB Output is correct
36 Correct 12 ms 38744 KB Output is correct
37 Correct 10 ms 36700 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 38848 KB Output is correct
2 Correct 9 ms 38600 KB Output is correct
3 Correct 12 ms 36892 KB Output is correct
4 Correct 10 ms 38744 KB Output is correct
5 Correct 11 ms 39000 KB Output is correct
6 Correct 9 ms 36696 KB Output is correct
7 Correct 9 ms 36700 KB Output is correct
8 Correct 9 ms 36700 KB Output is correct
9 Correct 10 ms 38932 KB Output is correct
10 Correct 9 ms 36700 KB Output is correct
11 Correct 10 ms 36700 KB Output is correct
12 Correct 11 ms 38748 KB Output is correct
13 Correct 9 ms 36696 KB Output is correct
14 Correct 9 ms 38748 KB Output is correct
15 Correct 9 ms 38748 KB Output is correct
16 Correct 10 ms 38744 KB Output is correct
17 Correct 10 ms 38928 KB Output is correct
18 Correct 10 ms 38748 KB Output is correct
19 Correct 9 ms 38748 KB Output is correct
20 Correct 10 ms 36700 KB Output is correct
21 Correct 9 ms 36696 KB Output is correct
22 Correct 9 ms 36696 KB Output is correct
23 Correct 9 ms 38744 KB Output is correct
24 Correct 9 ms 38748 KB Output is correct
25 Correct 9 ms 36956 KB Output is correct
26 Correct 9 ms 38744 KB Output is correct
27 Correct 10 ms 38748 KB Output is correct
28 Correct 9 ms 36700 KB Output is correct
29 Correct 11 ms 38980 KB Output is correct
30 Correct 12 ms 39004 KB Output is correct
31 Correct 12 ms 36956 KB Output is correct
32 Correct 12 ms 38864 KB Output is correct
33 Correct 10 ms 38744 KB Output is correct
34 Correct 12 ms 39260 KB Output is correct
35 Correct 20 ms 38992 KB Output is correct
36 Correct 12 ms 38744 KB Output is correct
37 Correct 10 ms 36700 KB Output is correct
38 Correct 41 ms 41504 KB Output is correct
39 Correct 71 ms 66428 KB Output is correct
40 Correct 400 ms 45356 KB Output is correct
41 Correct 65 ms 41084 KB Output is correct
42 Correct 47 ms 41308 KB Output is correct
43 Correct 34 ms 41508 KB Output is correct
44 Correct 32 ms 39260 KB Output is correct
45 Correct 73 ms 58464 KB Output is correct
46 Correct 82 ms 58980 KB Output is correct
47 Correct 1092 ms 84512 KB Output is correct
48 Correct 1061 ms 85252 KB Output is correct
49 Correct 140 ms 60252 KB Output is correct
50 Correct 127 ms 60128 KB Output is correct
51 Correct 57 ms 59856 KB Output is correct
52 Correct 59 ms 59728 KB Output is correct
53 Correct 24 ms 39004 KB Output is correct
54 Correct 75 ms 58700 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 38748 KB Output is correct
2 Correct 8 ms 36700 KB Output is correct
3 Correct 9 ms 38748 KB Output is correct
4 Correct 9 ms 38748 KB Output is correct
5 Correct 15 ms 36700 KB Output is correct
6 Correct 9 ms 38744 KB Output is correct
7 Correct 10 ms 38744 KB Output is correct
8 Correct 8 ms 36696 KB Output is correct
9 Correct 9 ms 38744 KB Output is correct
10 Correct 9 ms 38744 KB Output is correct
11 Correct 9 ms 36700 KB Output is correct
12 Correct 12 ms 36696 KB Output is correct
13 Correct 43 ms 39248 KB Output is correct
14 Correct 57 ms 39504 KB Output is correct
15 Correct 49 ms 37504 KB Output is correct
16 Correct 121 ms 59972 KB Output is correct
17 Correct 491 ms 72972 KB Output is correct
18 Correct 824 ms 99140 KB Output is correct
19 Correct 180 ms 61724 KB Output is correct
20 Correct 196 ms 61484 KB Output is correct
21 Correct 171 ms 61780 KB Output is correct
22 Correct 416 ms 70972 KB Output is correct
23 Correct 295 ms 70108 KB Output is correct
24 Correct 327 ms 69996 KB Output is correct
25 Correct 299 ms 69708 KB Output is correct
26 Correct 310 ms 70196 KB Output is correct
27 Correct 336 ms 76232 KB Output is correct
28 Correct 346 ms 82956 KB Output is correct
29 Correct 317 ms 76752 KB Output is correct
30 Correct 185 ms 68560 KB Output is correct
31 Correct 224 ms 72536 KB Output is correct
32 Correct 190 ms 69584 KB Output is correct
33 Correct 200 ms 72804 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 11 ms 38748 KB Output is correct
2 Correct 10 ms 38748 KB Output is correct
3 Correct 8 ms 36728 KB Output is correct
4 Correct 18 ms 39212 KB Output is correct
5 Correct 28 ms 37568 KB Output is correct
6 Correct 11 ms 38748 KB Output is correct
7 Correct 10 ms 38744 KB Output is correct
8 Correct 12 ms 38748 KB Output is correct
9 Correct 42 ms 41652 KB Output is correct
10 Correct 59 ms 66384 KB Output is correct
11 Correct 14 ms 36700 KB Output is correct
12 Correct 48 ms 41780 KB Output is correct
13 Correct 139 ms 77396 KB Output is correct
14 Correct 146 ms 75536 KB Output is correct
15 Correct 317 ms 81016 KB Output is correct
16 Correct 638 ms 109040 KB Output is correct
17 Correct 169 ms 82536 KB Output is correct
18 Correct 163 ms 78532 KB Output is correct
19 Correct 155 ms 80196 KB Output is correct
20 Correct 156 ms 80492 KB Output is correct
21 Correct 184 ms 82200 KB Output is correct
22 Correct 103 ms 73296 KB Output is correct
23 Correct 9 ms 38848 KB Output is correct
24 Correct 9 ms 38600 KB Output is correct
25 Correct 12 ms 36892 KB Output is correct
26 Correct 10 ms 38744 KB Output is correct
27 Correct 11 ms 39000 KB Output is correct
28 Correct 9 ms 36696 KB Output is correct
29 Correct 9 ms 36700 KB Output is correct
30 Correct 9 ms 36700 KB Output is correct
31 Correct 10 ms 38932 KB Output is correct
32 Correct 9 ms 36700 KB Output is correct
33 Correct 10 ms 36700 KB Output is correct
34 Correct 11 ms 38748 KB Output is correct
35 Correct 9 ms 36696 KB Output is correct
36 Correct 9 ms 38748 KB Output is correct
37 Correct 9 ms 38748 KB Output is correct
38 Correct 10 ms 38744 KB Output is correct
39 Correct 10 ms 38928 KB Output is correct
40 Correct 10 ms 38748 KB Output is correct
41 Correct 9 ms 38748 KB Output is correct
42 Correct 10 ms 36700 KB Output is correct
43 Correct 9 ms 36696 KB Output is correct
44 Correct 9 ms 36696 KB Output is correct
45 Correct 9 ms 38744 KB Output is correct
46 Correct 9 ms 38748 KB Output is correct
47 Correct 9 ms 36956 KB Output is correct
48 Correct 9 ms 38744 KB Output is correct
49 Correct 10 ms 38748 KB Output is correct
50 Correct 9 ms 36700 KB Output is correct
51 Correct 11 ms 38980 KB Output is correct
52 Correct 12 ms 39004 KB Output is correct
53 Correct 12 ms 36956 KB Output is correct
54 Correct 12 ms 38864 KB Output is correct
55 Correct 10 ms 38744 KB Output is correct
56 Correct 12 ms 39260 KB Output is correct
57 Correct 20 ms 38992 KB Output is correct
58 Correct 12 ms 38744 KB Output is correct
59 Correct 10 ms 36700 KB Output is correct
60 Correct 41 ms 41504 KB Output is correct
61 Correct 71 ms 66428 KB Output is correct
62 Correct 400 ms 45356 KB Output is correct
63 Correct 65 ms 41084 KB Output is correct
64 Correct 47 ms 41308 KB Output is correct
65 Correct 34 ms 41508 KB Output is correct
66 Correct 32 ms 39260 KB Output is correct
67 Correct 73 ms 58464 KB Output is correct
68 Correct 82 ms 58980 KB Output is correct
69 Correct 1092 ms 84512 KB Output is correct
70 Correct 1061 ms 85252 KB Output is correct
71 Correct 140 ms 60252 KB Output is correct
72 Correct 127 ms 60128 KB Output is correct
73 Correct 57 ms 59856 KB Output is correct
74 Correct 59 ms 59728 KB Output is correct
75 Correct 24 ms 39004 KB Output is correct
76 Correct 75 ms 58700 KB Output is correct
77 Correct 9 ms 38748 KB Output is correct
78 Correct 8 ms 36700 KB Output is correct
79 Correct 9 ms 38748 KB Output is correct
80 Correct 9 ms 38748 KB Output is correct
81 Correct 15 ms 36700 KB Output is correct
82 Correct 9 ms 38744 KB Output is correct
83 Correct 10 ms 38744 KB Output is correct
84 Correct 8 ms 36696 KB Output is correct
85 Correct 9 ms 38744 KB Output is correct
86 Correct 9 ms 38744 KB Output is correct
87 Correct 9 ms 36700 KB Output is correct
88 Correct 12 ms 36696 KB Output is correct
89 Correct 43 ms 39248 KB Output is correct
90 Correct 57 ms 39504 KB Output is correct
91 Correct 49 ms 37504 KB Output is correct
92 Correct 121 ms 59972 KB Output is correct
93 Correct 491 ms 72972 KB Output is correct
94 Correct 824 ms 99140 KB Output is correct
95 Correct 180 ms 61724 KB Output is correct
96 Correct 196 ms 61484 KB Output is correct
97 Correct 171 ms 61780 KB Output is correct
98 Correct 416 ms 70972 KB Output is correct
99 Correct 295 ms 70108 KB Output is correct
100 Correct 327 ms 69996 KB Output is correct
101 Correct 299 ms 69708 KB Output is correct
102 Correct 310 ms 70196 KB Output is correct
103 Correct 336 ms 76232 KB Output is correct
104 Correct 346 ms 82956 KB Output is correct
105 Correct 317 ms 76752 KB Output is correct
106 Correct 185 ms 68560 KB Output is correct
107 Correct 224 ms 72536 KB Output is correct
108 Correct 190 ms 69584 KB Output is correct
109 Correct 200 ms 72804 KB Output is correct
110 Correct 62 ms 37948 KB Output is correct
111 Correct 34 ms 37464 KB Output is correct
112 Execution timed out 5067 ms 227020 KB Time limit exceeded
113 Halted 0 ms 0 KB -