Submission #961204

# Submission time Handle Problem Language Result Execution time Memory
961204 2024-04-11T16:52:11 Z huutuan Jail (JOI22_jail) C++14
61 / 100
5000 ms 54004 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;
int n, m, up[LG][N], tin[N], tout[N], tdfs, dep[N], deg[N], vis[N];
pair<int, int> a[N];
int b[N], check;
vector<int> g[N], g2[N];

void dfs(int u, int p){
   tin[u]=++tdfs;
   up[0][u]=p;
   dep[u]=dep[p]+1;
   for (int k=1; k<LG; ++k) up[k][u]=up[k-1][up[k-1][u]];
   for (int v:g[u]) if (v!=p) dfs(v, u);
   tout[u]=tdfs;
}

bool is_ancestor(int u, int v){
   return tin[u]<=tin[v] && tin[v]<=tout[u];
}

int lca(int u, int v){
   if (dep[u]!=dep[v]){
      if (dep[u]<dep[v]) swap(u, v);
      int d=dep[u]-dep[v];
      for (int k=0; k<LG; ++k) if (d>>k&1) u=up[k][u];
   }
   if (u==v) return u;
   for (int k=LG-1; k>=0; --k) if (up[k][u]!=up[k][v]) u=up[k][u], v=up[k][v];
   return up[0][u];
}

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

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(1, 0);
   cin >> m;
   for (int i=1; i<=m; ++i){
      cin >> a[i].first >> a[i].second;
      b[i]=lca(a[i].first, a[i].second);
   }
   for (int i=1; i<=m; ++i) for (int j=1; j<=m; ++j) if (i!=j){
      if (is_ancestor(b[i], a[j].first) && (is_ancestor(a[j].first, a[i].first) || is_ancestor(a[j].first, a[i].second))) g2[j].push_back(i), ++deg[i];
      if (is_ancestor(b[i], a[j].second) && (is_ancestor(a[j].second, a[i].first) || is_ancestor(a[j].second, a[i].second))) g2[i].push_back(j), ++deg[j];
   }
   for (int i=1; i<=m; ++i) if (!deg[i]) dfs2(i);
   check&=accumulate(vis+1, vis+m+1, 0ll)==2*m;
   cout << (check?"Yes\n":"No\n");
   for (int i=1; i<=n; ++i) g[i].clear(), g2[i].clear(), deg[i]=vis[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 12 ms 27228 KB Output is correct
2 Correct 7 ms 27228 KB Output is correct
3 Correct 5 ms 25180 KB Output is correct
4 Correct 13 ms 25436 KB Output is correct
5 Correct 20 ms 26148 KB Output is correct
6 Correct 7 ms 27228 KB Output is correct
7 Correct 6 ms 27224 KB Output is correct
8 Correct 7 ms 27480 KB Output is correct
9 Correct 59 ms 30936 KB Output is correct
10 Correct 37 ms 41152 KB Output is correct
11 Correct 9 ms 27736 KB Output is correct
12 Correct 47 ms 28344 KB Output is correct
13 Execution timed out 5042 ms 54004 KB Time limit exceeded
14 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 8 ms 27228 KB Output is correct
2 Correct 5 ms 25180 KB Output is correct
3 Correct 6 ms 27252 KB Output is correct
4 Correct 5 ms 25180 KB Output is correct
5 Correct 5 ms 27228 KB Output is correct
6 Correct 5 ms 27228 KB Output is correct
7 Correct 5 ms 25432 KB Output is correct
8 Correct 5 ms 27228 KB Output is correct
9 Correct 7 ms 27480 KB Output is correct
10 Correct 5 ms 27228 KB Output is correct
11 Correct 6 ms 27228 KB Output is correct
12 Correct 5 ms 25176 KB Output is correct
13 Correct 5 ms 25180 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 27228 KB Output is correct
2 Correct 5 ms 25180 KB Output is correct
3 Correct 6 ms 27252 KB Output is correct
4 Correct 5 ms 25180 KB Output is correct
5 Correct 5 ms 27228 KB Output is correct
6 Correct 5 ms 27228 KB Output is correct
7 Correct 5 ms 25432 KB Output is correct
8 Correct 5 ms 27228 KB Output is correct
9 Correct 7 ms 27480 KB Output is correct
10 Correct 5 ms 27228 KB Output is correct
11 Correct 6 ms 27228 KB Output is correct
12 Correct 5 ms 25176 KB Output is correct
13 Correct 5 ms 25180 KB Output is correct
14 Correct 6 ms 27340 KB Output is correct
15 Correct 5 ms 27280 KB Output is correct
16 Correct 6 ms 27228 KB Output is correct
17 Correct 6 ms 27228 KB Output is correct
18 Correct 6 ms 27228 KB Output is correct
19 Correct 5 ms 27228 KB Output is correct
20 Correct 6 ms 27224 KB Output is correct
21 Correct 5 ms 27388 KB Output is correct
22 Correct 6 ms 27552 KB Output is correct
23 Correct 5 ms 27236 KB Output is correct
24 Correct 5 ms 27540 KB Output is correct
25 Correct 6 ms 27384 KB Output is correct
26 Correct 5 ms 27228 KB Output is correct
27 Correct 6 ms 27292 KB Output is correct
28 Correct 5 ms 27228 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 27228 KB Output is correct
2 Correct 5 ms 25180 KB Output is correct
3 Correct 6 ms 27252 KB Output is correct
4 Correct 5 ms 25180 KB Output is correct
5 Correct 5 ms 27228 KB Output is correct
6 Correct 5 ms 27228 KB Output is correct
7 Correct 5 ms 25432 KB Output is correct
8 Correct 5 ms 27228 KB Output is correct
9 Correct 7 ms 27480 KB Output is correct
10 Correct 5 ms 27228 KB Output is correct
11 Correct 6 ms 27228 KB Output is correct
12 Correct 5 ms 25176 KB Output is correct
13 Correct 5 ms 25180 KB Output is correct
14 Correct 6 ms 27340 KB Output is correct
15 Correct 5 ms 27280 KB Output is correct
16 Correct 6 ms 27228 KB Output is correct
17 Correct 6 ms 27228 KB Output is correct
18 Correct 6 ms 27228 KB Output is correct
19 Correct 5 ms 27228 KB Output is correct
20 Correct 6 ms 27224 KB Output is correct
21 Correct 5 ms 27388 KB Output is correct
22 Correct 6 ms 27552 KB Output is correct
23 Correct 5 ms 27236 KB Output is correct
24 Correct 5 ms 27540 KB Output is correct
25 Correct 6 ms 27384 KB Output is correct
26 Correct 5 ms 27228 KB Output is correct
27 Correct 6 ms 27292 KB Output is correct
28 Correct 5 ms 27228 KB Output is correct
29 Correct 7 ms 27480 KB Output is correct
30 Correct 7 ms 27228 KB Output is correct
31 Correct 7 ms 25180 KB Output is correct
32 Correct 6 ms 27228 KB Output is correct
33 Correct 6 ms 27228 KB Output is correct
34 Correct 7 ms 25180 KB Output is correct
35 Correct 7 ms 25180 KB Output is correct
36 Correct 8 ms 27296 KB Output is correct
37 Correct 7 ms 27388 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 27228 KB Output is correct
2 Correct 5 ms 25180 KB Output is correct
3 Correct 6 ms 27252 KB Output is correct
4 Correct 5 ms 25180 KB Output is correct
5 Correct 5 ms 27228 KB Output is correct
6 Correct 5 ms 27228 KB Output is correct
7 Correct 5 ms 25432 KB Output is correct
8 Correct 5 ms 27228 KB Output is correct
9 Correct 7 ms 27480 KB Output is correct
10 Correct 5 ms 27228 KB Output is correct
11 Correct 6 ms 27228 KB Output is correct
12 Correct 5 ms 25176 KB Output is correct
13 Correct 5 ms 25180 KB Output is correct
14 Correct 6 ms 27340 KB Output is correct
15 Correct 5 ms 27280 KB Output is correct
16 Correct 6 ms 27228 KB Output is correct
17 Correct 6 ms 27228 KB Output is correct
18 Correct 6 ms 27228 KB Output is correct
19 Correct 5 ms 27228 KB Output is correct
20 Correct 6 ms 27224 KB Output is correct
21 Correct 5 ms 27388 KB Output is correct
22 Correct 6 ms 27552 KB Output is correct
23 Correct 5 ms 27236 KB Output is correct
24 Correct 5 ms 27540 KB Output is correct
25 Correct 6 ms 27384 KB Output is correct
26 Correct 5 ms 27228 KB Output is correct
27 Correct 6 ms 27292 KB Output is correct
28 Correct 5 ms 27228 KB Output is correct
29 Correct 7 ms 27480 KB Output is correct
30 Correct 7 ms 27228 KB Output is correct
31 Correct 7 ms 25180 KB Output is correct
32 Correct 6 ms 27228 KB Output is correct
33 Correct 6 ms 27228 KB Output is correct
34 Correct 7 ms 25180 KB Output is correct
35 Correct 7 ms 25180 KB Output is correct
36 Correct 8 ms 27296 KB Output is correct
37 Correct 7 ms 27388 KB Output is correct
38 Correct 61 ms 31052 KB Output is correct
39 Correct 41 ms 41300 KB Output is correct
40 Correct 117 ms 30040 KB Output is correct
41 Correct 110 ms 28952 KB Output is correct
42 Correct 106 ms 30016 KB Output is correct
43 Correct 25 ms 28612 KB Output is correct
44 Correct 40 ms 25500 KB Output is correct
45 Correct 57 ms 34196 KB Output is correct
46 Correct 63 ms 34184 KB Output is correct
47 Correct 62 ms 36956 KB Output is correct
48 Correct 41 ms 36948 KB Output is correct
49 Correct 63 ms 34492 KB Output is correct
50 Correct 53 ms 34304 KB Output is correct
51 Correct 34 ms 35156 KB Output is correct
52 Correct 44 ms 35528 KB Output is correct
53 Correct 59 ms 28020 KB Output is correct
54 Correct 67 ms 34412 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 27228 KB Output is correct
2 Correct 5 ms 27228 KB Output is correct
3 Correct 4 ms 27228 KB Output is correct
4 Correct 5 ms 25180 KB Output is correct
5 Correct 10 ms 27480 KB Output is correct
6 Correct 5 ms 25180 KB Output is correct
7 Correct 6 ms 25180 KB Output is correct
8 Correct 5 ms 27228 KB Output is correct
9 Correct 4 ms 27228 KB Output is correct
10 Correct 5 ms 27228 KB Output is correct
11 Correct 4 ms 27228 KB Output is correct
12 Correct 8 ms 27228 KB Output is correct
13 Correct 49 ms 27884 KB Output is correct
14 Correct 37 ms 28308 KB Output is correct
15 Correct 34 ms 27840 KB Output is correct
16 Correct 1751 ms 34808 KB Output is correct
17 Execution timed out 5044 ms 35908 KB Time limit exceeded
18 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 12 ms 27228 KB Output is correct
2 Correct 7 ms 27228 KB Output is correct
3 Correct 5 ms 25180 KB Output is correct
4 Correct 13 ms 25436 KB Output is correct
5 Correct 20 ms 26148 KB Output is correct
6 Correct 7 ms 27228 KB Output is correct
7 Correct 6 ms 27224 KB Output is correct
8 Correct 7 ms 27480 KB Output is correct
9 Correct 59 ms 30936 KB Output is correct
10 Correct 37 ms 41152 KB Output is correct
11 Correct 9 ms 27736 KB Output is correct
12 Correct 47 ms 28344 KB Output is correct
13 Execution timed out 5042 ms 54004 KB Time limit exceeded
14 Halted 0 ms 0 KB -