답안 #544590

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
544590 2022-04-02T13:15:56 Z pavement Jail (JOI22_jail) C++17
100 / 100
3299 ms 192988 KB
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#ifdef _WIN32
#define getchar_unlocked _getchar_nolock
#endif
//#define int long long
#define mp make_pair
#define mt make_tuple
#define pb push_back
#define ppb pop_back
#define eb emplace_back
#define g0(a) get<0>(a)
#define g1(a) get<1>(a)
#define g2(a) get<2>(a)
#define g3(a) get<3>(a)
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
using db = double;
using ll = long long;
using ld = long double;
using ii = pair<int, int>;
using iii = tuple<int, int, int>;
using iiii = tuple<int, int, int, int>;
template<class key, class value = null_type, class cmp = less<key> >
using ordered_set = tree<key, value, cmp, rb_tree_tag, tree_order_statistics_node_update>;

int Q, N, M, _lab, cur_scc, idx, leaf[2][700005], dep[700005], hv[700005], par[700005], pos[700005], scc_num[700005];
bool vis[700005], vis_t[700005];
vector<int> tp, adj[700005], adj2_t[700005], adj2[700005];

struct node {
	node *left, *right;
	int S, E, lab;
	node(int _s, int _e, bool _f = 0) : S(_s), E(_e) {
		lab = ++_lab;
		if (S == E) {
			leaf[_f][S] = lab;
			return;
		}
		int M = (S + E) >> 1;
		left = new node(S, M, _f);
		right = new node(M + 1, E, _f);
		if (!_f) {
			adj2[left->lab].pb(lab);
			adj2[right->lab].pb(lab);
		} else {
			adj2[lab].pb(left->lab);
			adj2[lab].pb(right->lab);
		}
	}
	void add(int l, int r, int x, bool dir) {
		if (l > E || r < S) return;
		if (l <= S && E <= r) {
			if (dir) adj2[lab].pb(x);
			else adj2[x].pb(lab);
			return;
		}
		left->add(l, r, x, dir);
		right->add(l, r, x, dir);
	}
} *root[2];

int init(int n, int e = -1) {
    par[n] = e;
    int sz = 1, mc = 0;
    for (auto u : adj[n]) {
        if (u == e) continue;
        dep[u] = dep[n] + 1;
        int c = init(u, n);
        if (c > mc) mc = c, hv[n] = u;
        sz += c;
    }
    return sz;
}

void decomp(int n, int h) {
    pos[n] = ++idx;
    if (hv[n] != -1) decomp(hv[n], h);
    for (auto u : adj[n]) {
        if (u == par[n] || u == hv[n]) continue;
        decomp(u, u);
    }
    hv[n] = h;
}

void add_edge(int u, int v, int x) {
    if (dep[u] > dep[v]) swap(u, v);
    for (; hv[u] != hv[v]; v = par[hv[v]]) {
		if (dep[hv[u]] > dep[hv[v]]) swap(u, v);
        root[0]->add(pos[hv[v]], pos[v], x, 0);
        root[1]->add(pos[hv[v]], pos[v], x, 1);
    }
    if (dep[u] > dep[v]) swap(u, v);
    root[0]->add(pos[u], pos[v], x, 0);
    root[1]->add(pos[u], pos[v], x, 1);
}

void dfs(int n) {
	vis[n] = 1;
	for (auto u : adj2[n])
		if (!vis[u]) dfs(u);
	tp.push_back(n);
}

void dfs_t(int n) {
	vis_t[n] = 1;
	scc_num[n] = cur_scc;
	for (auto u : adj2_t[n])
		if (!vis_t[u]) dfs_t(u);
}

main() {
	memset(hv, -1, sizeof hv);
	ios::sync_with_stdio(0);
	cin.tie(0);
	cin >> Q;
	while (Q--) {
		cin >> N;
		for (int i = 1, u, v; i < N; i++) {
			cin >> u >> v;
			adj[u].pb(v);
			adj[v].pb(u);
		}
		cin >> M;
		_lab = M;
		init(1);
		decomp(1, 1);
		root[0] = new node(1, N, 1);
		root[1] = new node(1, N, 0);
		for (int i = 1, S, T; i <= M; i++) {
			cin >> S >> T;
			add_edge(S, T, i);
			adj2[leaf[1][pos[S]]].pb(i);
			adj2[i].pb(leaf[0][pos[T]]);
		}
		for (int i = 1; i <= _lab; i++)
			for (int j : adj2[i]) adj2_t[j].pb(i);
		for (int i = 1; i <= N; i++)
			if (!vis[i]) dfs(i);
		reverse(tp.begin(), tp.end());
		for (int i : tp)
			if (!vis_t[i]) {
				cur_scc++;
				dfs_t(i);
			}
		set<int> ss;
		for (int i = 1; i <= M; i++) ss.insert(scc_num[i]);
		if (ss.size() == M) cout << "Yes\n";
		else cout << "No\n";
		for (int i = 1; i <= _lab; i++) {
			adj[i].clear();
			adj2[i].clear();
			adj2_t[i].clear();
			hv[i] = -1;
			dep[i] = par[i] = pos[i] = leaf[0][i] = leaf[1][i] = scc_num[i] = 0;
			vis[i] = vis_t[i] = 0;
		}
		tp.clear();
		_lab = cur_scc = idx = 0;
	}
}

Compilation message

jail.cpp:114:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
  114 | main() {
      | ^~~~
jail.cpp: In function 'int main()':
jail.cpp:150:17: warning: comparison of integer expressions of different signedness: 'std::set<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
  150 |   if (ss.size() == M) cout << "Yes\n";
      |       ~~~~~~~~~~^~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 25 ms 52380 KB Output is correct
2 Correct 23 ms 52436 KB Output is correct
3 Correct 24 ms 52436 KB Output is correct
4 Correct 53 ms 62744 KB Output is correct
5 Correct 79 ms 74812 KB Output is correct
6 Correct 27 ms 53460 KB Output is correct
7 Correct 27 ms 53468 KB Output is correct
8 Correct 28 ms 53452 KB Output is correct
9 Correct 105 ms 77900 KB Output is correct
10 Correct 156 ms 124152 KB Output is correct
11 Correct 39 ms 56268 KB Output is correct
12 Correct 109 ms 74988 KB Output is correct
13 Correct 295 ms 138184 KB Output is correct
14 Correct 304 ms 136256 KB Output is correct
15 Correct 670 ms 145936 KB Output is correct
16 Correct 1307 ms 183168 KB Output is correct
17 Correct 377 ms 154292 KB Output is correct
18 Correct 305 ms 153308 KB Output is correct
19 Correct 366 ms 150396 KB Output is correct
20 Correct 360 ms 150364 KB Output is correct
21 Correct 397 ms 150204 KB Output is correct
22 Correct 244 ms 133888 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 24 ms 52436 KB Output is correct
2 Correct 25 ms 52348 KB Output is correct
3 Correct 28 ms 53460 KB Output is correct
4 Correct 42 ms 53416 KB Output is correct
5 Correct 33 ms 53456 KB Output is correct
6 Correct 29 ms 53420 KB Output is correct
7 Correct 35 ms 53392 KB Output is correct
8 Correct 29 ms 53324 KB Output is correct
9 Correct 28 ms 53368 KB Output is correct
10 Correct 27 ms 53408 KB Output is correct
11 Correct 27 ms 53308 KB Output is correct
12 Correct 26 ms 52880 KB Output is correct
13 Correct 27 ms 52912 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 24 ms 52436 KB Output is correct
2 Correct 25 ms 52348 KB Output is correct
3 Correct 28 ms 53460 KB Output is correct
4 Correct 42 ms 53416 KB Output is correct
5 Correct 33 ms 53456 KB Output is correct
6 Correct 29 ms 53420 KB Output is correct
7 Correct 35 ms 53392 KB Output is correct
8 Correct 29 ms 53324 KB Output is correct
9 Correct 28 ms 53368 KB Output is correct
10 Correct 27 ms 53408 KB Output is correct
11 Correct 27 ms 53308 KB Output is correct
12 Correct 26 ms 52880 KB Output is correct
13 Correct 27 ms 52912 KB Output is correct
14 Correct 25 ms 52436 KB Output is correct
15 Correct 25 ms 52428 KB Output is correct
16 Correct 27 ms 53388 KB Output is correct
17 Correct 27 ms 53416 KB Output is correct
18 Correct 26 ms 53372 KB Output is correct
19 Correct 30 ms 52456 KB Output is correct
20 Correct 31 ms 53368 KB Output is correct
21 Correct 29 ms 53428 KB Output is correct
22 Correct 28 ms 53364 KB Output is correct
23 Correct 27 ms 52416 KB Output is correct
24 Correct 27 ms 52440 KB Output is correct
25 Correct 32 ms 53376 KB Output is correct
26 Correct 35 ms 52684 KB Output is correct
27 Correct 26 ms 53464 KB Output is correct
28 Correct 23 ms 52436 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 24 ms 52436 KB Output is correct
2 Correct 25 ms 52348 KB Output is correct
3 Correct 28 ms 53460 KB Output is correct
4 Correct 42 ms 53416 KB Output is correct
5 Correct 33 ms 53456 KB Output is correct
6 Correct 29 ms 53420 KB Output is correct
7 Correct 35 ms 53392 KB Output is correct
8 Correct 29 ms 53324 KB Output is correct
9 Correct 28 ms 53368 KB Output is correct
10 Correct 27 ms 53408 KB Output is correct
11 Correct 27 ms 53308 KB Output is correct
12 Correct 26 ms 52880 KB Output is correct
13 Correct 27 ms 52912 KB Output is correct
14 Correct 25 ms 52436 KB Output is correct
15 Correct 25 ms 52428 KB Output is correct
16 Correct 27 ms 53388 KB Output is correct
17 Correct 27 ms 53416 KB Output is correct
18 Correct 26 ms 53372 KB Output is correct
19 Correct 30 ms 52456 KB Output is correct
20 Correct 31 ms 53368 KB Output is correct
21 Correct 29 ms 53428 KB Output is correct
22 Correct 28 ms 53364 KB Output is correct
23 Correct 27 ms 52416 KB Output is correct
24 Correct 27 ms 52440 KB Output is correct
25 Correct 32 ms 53376 KB Output is correct
26 Correct 35 ms 52684 KB Output is correct
27 Correct 26 ms 53464 KB Output is correct
28 Correct 23 ms 52436 KB Output is correct
29 Correct 30 ms 53416 KB Output is correct
30 Correct 30 ms 53460 KB Output is correct
31 Correct 30 ms 53460 KB Output is correct
32 Correct 28 ms 53460 KB Output is correct
33 Correct 26 ms 53452 KB Output is correct
34 Correct 28 ms 53208 KB Output is correct
35 Correct 28 ms 53212 KB Output is correct
36 Correct 34 ms 53068 KB Output is correct
37 Correct 29 ms 52932 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 24 ms 52436 KB Output is correct
2 Correct 25 ms 52348 KB Output is correct
3 Correct 28 ms 53460 KB Output is correct
4 Correct 42 ms 53416 KB Output is correct
5 Correct 33 ms 53456 KB Output is correct
6 Correct 29 ms 53420 KB Output is correct
7 Correct 35 ms 53392 KB Output is correct
8 Correct 29 ms 53324 KB Output is correct
9 Correct 28 ms 53368 KB Output is correct
10 Correct 27 ms 53408 KB Output is correct
11 Correct 27 ms 53308 KB Output is correct
12 Correct 26 ms 52880 KB Output is correct
13 Correct 27 ms 52912 KB Output is correct
14 Correct 25 ms 52436 KB Output is correct
15 Correct 25 ms 52428 KB Output is correct
16 Correct 27 ms 53388 KB Output is correct
17 Correct 27 ms 53416 KB Output is correct
18 Correct 26 ms 53372 KB Output is correct
19 Correct 30 ms 52456 KB Output is correct
20 Correct 31 ms 53368 KB Output is correct
21 Correct 29 ms 53428 KB Output is correct
22 Correct 28 ms 53364 KB Output is correct
23 Correct 27 ms 52416 KB Output is correct
24 Correct 27 ms 52440 KB Output is correct
25 Correct 32 ms 53376 KB Output is correct
26 Correct 35 ms 52684 KB Output is correct
27 Correct 26 ms 53464 KB Output is correct
28 Correct 23 ms 52436 KB Output is correct
29 Correct 30 ms 53416 KB Output is correct
30 Correct 30 ms 53460 KB Output is correct
31 Correct 30 ms 53460 KB Output is correct
32 Correct 28 ms 53460 KB Output is correct
33 Correct 26 ms 53452 KB Output is correct
34 Correct 28 ms 53208 KB Output is correct
35 Correct 28 ms 53212 KB Output is correct
36 Correct 34 ms 53068 KB Output is correct
37 Correct 29 ms 52932 KB Output is correct
38 Correct 138 ms 77804 KB Output is correct
39 Correct 177 ms 124200 KB Output is correct
40 Correct 181 ms 77776 KB Output is correct
41 Correct 191 ms 77964 KB Output is correct
42 Correct 145 ms 77944 KB Output is correct
43 Correct 93 ms 77132 KB Output is correct
44 Correct 62 ms 56908 KB Output is correct
45 Correct 206 ms 114732 KB Output is correct
46 Correct 210 ms 114804 KB Output is correct
47 Correct 185 ms 119376 KB Output is correct
48 Correct 222 ms 119420 KB Output is correct
49 Correct 190 ms 115020 KB Output is correct
50 Correct 229 ms 115004 KB Output is correct
51 Correct 234 ms 116064 KB Output is correct
52 Correct 187 ms 116160 KB Output is correct
53 Correct 64 ms 60804 KB Output is correct
54 Correct 231 ms 114780 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 26 ms 52368 KB Output is correct
2 Correct 28 ms 52332 KB Output is correct
3 Correct 28 ms 52396 KB Output is correct
4 Correct 36 ms 52436 KB Output is correct
5 Correct 40 ms 56296 KB Output is correct
6 Correct 30 ms 52940 KB Output is correct
7 Correct 29 ms 52944 KB Output is correct
8 Correct 27 ms 52384 KB Output is correct
9 Correct 26 ms 52368 KB Output is correct
10 Correct 25 ms 52436 KB Output is correct
11 Correct 27 ms 52540 KB Output is correct
12 Correct 34 ms 53076 KB Output is correct
13 Correct 90 ms 64604 KB Output is correct
14 Correct 124 ms 75088 KB Output is correct
15 Correct 113 ms 69312 KB Output is correct
16 Correct 390 ms 118732 KB Output is correct
17 Correct 1266 ms 139536 KB Output is correct
18 Correct 2249 ms 170244 KB Output is correct
19 Correct 650 ms 123360 KB Output is correct
20 Correct 626 ms 123540 KB Output is correct
21 Correct 643 ms 123800 KB Output is correct
22 Correct 1242 ms 138692 KB Output is correct
23 Correct 703 ms 137924 KB Output is correct
24 Correct 730 ms 140744 KB Output is correct
25 Correct 812 ms 140772 KB Output is correct
26 Correct 836 ms 140992 KB Output is correct
27 Correct 1393 ms 150480 KB Output is correct
28 Correct 1306 ms 147828 KB Output is correct
29 Correct 1255 ms 141932 KB Output is correct
30 Correct 850 ms 135824 KB Output is correct
31 Correct 793 ms 134552 KB Output is correct
32 Correct 795 ms 136252 KB Output is correct
33 Correct 846 ms 134272 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 25 ms 52380 KB Output is correct
2 Correct 23 ms 52436 KB Output is correct
3 Correct 24 ms 52436 KB Output is correct
4 Correct 53 ms 62744 KB Output is correct
5 Correct 79 ms 74812 KB Output is correct
6 Correct 27 ms 53460 KB Output is correct
7 Correct 27 ms 53468 KB Output is correct
8 Correct 28 ms 53452 KB Output is correct
9 Correct 105 ms 77900 KB Output is correct
10 Correct 156 ms 124152 KB Output is correct
11 Correct 39 ms 56268 KB Output is correct
12 Correct 109 ms 74988 KB Output is correct
13 Correct 295 ms 138184 KB Output is correct
14 Correct 304 ms 136256 KB Output is correct
15 Correct 670 ms 145936 KB Output is correct
16 Correct 1307 ms 183168 KB Output is correct
17 Correct 377 ms 154292 KB Output is correct
18 Correct 305 ms 153308 KB Output is correct
19 Correct 366 ms 150396 KB Output is correct
20 Correct 360 ms 150364 KB Output is correct
21 Correct 397 ms 150204 KB Output is correct
22 Correct 244 ms 133888 KB Output is correct
23 Correct 24 ms 52436 KB Output is correct
24 Correct 25 ms 52348 KB Output is correct
25 Correct 28 ms 53460 KB Output is correct
26 Correct 42 ms 53416 KB Output is correct
27 Correct 33 ms 53456 KB Output is correct
28 Correct 29 ms 53420 KB Output is correct
29 Correct 35 ms 53392 KB Output is correct
30 Correct 29 ms 53324 KB Output is correct
31 Correct 28 ms 53368 KB Output is correct
32 Correct 27 ms 53408 KB Output is correct
33 Correct 27 ms 53308 KB Output is correct
34 Correct 26 ms 52880 KB Output is correct
35 Correct 27 ms 52912 KB Output is correct
36 Correct 25 ms 52436 KB Output is correct
37 Correct 25 ms 52428 KB Output is correct
38 Correct 27 ms 53388 KB Output is correct
39 Correct 27 ms 53416 KB Output is correct
40 Correct 26 ms 53372 KB Output is correct
41 Correct 30 ms 52456 KB Output is correct
42 Correct 31 ms 53368 KB Output is correct
43 Correct 29 ms 53428 KB Output is correct
44 Correct 28 ms 53364 KB Output is correct
45 Correct 27 ms 52416 KB Output is correct
46 Correct 27 ms 52440 KB Output is correct
47 Correct 32 ms 53376 KB Output is correct
48 Correct 35 ms 52684 KB Output is correct
49 Correct 26 ms 53464 KB Output is correct
50 Correct 23 ms 52436 KB Output is correct
51 Correct 30 ms 53416 KB Output is correct
52 Correct 30 ms 53460 KB Output is correct
53 Correct 30 ms 53460 KB Output is correct
54 Correct 28 ms 53460 KB Output is correct
55 Correct 26 ms 53452 KB Output is correct
56 Correct 28 ms 53208 KB Output is correct
57 Correct 28 ms 53212 KB Output is correct
58 Correct 34 ms 53068 KB Output is correct
59 Correct 29 ms 52932 KB Output is correct
60 Correct 138 ms 77804 KB Output is correct
61 Correct 177 ms 124200 KB Output is correct
62 Correct 181 ms 77776 KB Output is correct
63 Correct 191 ms 77964 KB Output is correct
64 Correct 145 ms 77944 KB Output is correct
65 Correct 93 ms 77132 KB Output is correct
66 Correct 62 ms 56908 KB Output is correct
67 Correct 206 ms 114732 KB Output is correct
68 Correct 210 ms 114804 KB Output is correct
69 Correct 185 ms 119376 KB Output is correct
70 Correct 222 ms 119420 KB Output is correct
71 Correct 190 ms 115020 KB Output is correct
72 Correct 229 ms 115004 KB Output is correct
73 Correct 234 ms 116064 KB Output is correct
74 Correct 187 ms 116160 KB Output is correct
75 Correct 64 ms 60804 KB Output is correct
76 Correct 231 ms 114780 KB Output is correct
77 Correct 26 ms 52368 KB Output is correct
78 Correct 28 ms 52332 KB Output is correct
79 Correct 28 ms 52396 KB Output is correct
80 Correct 36 ms 52436 KB Output is correct
81 Correct 40 ms 56296 KB Output is correct
82 Correct 30 ms 52940 KB Output is correct
83 Correct 29 ms 52944 KB Output is correct
84 Correct 27 ms 52384 KB Output is correct
85 Correct 26 ms 52368 KB Output is correct
86 Correct 25 ms 52436 KB Output is correct
87 Correct 27 ms 52540 KB Output is correct
88 Correct 34 ms 53076 KB Output is correct
89 Correct 90 ms 64604 KB Output is correct
90 Correct 124 ms 75088 KB Output is correct
91 Correct 113 ms 69312 KB Output is correct
92 Correct 390 ms 118732 KB Output is correct
93 Correct 1266 ms 139536 KB Output is correct
94 Correct 2249 ms 170244 KB Output is correct
95 Correct 650 ms 123360 KB Output is correct
96 Correct 626 ms 123540 KB Output is correct
97 Correct 643 ms 123800 KB Output is correct
98 Correct 1242 ms 138692 KB Output is correct
99 Correct 703 ms 137924 KB Output is correct
100 Correct 730 ms 140744 KB Output is correct
101 Correct 812 ms 140772 KB Output is correct
102 Correct 836 ms 140992 KB Output is correct
103 Correct 1393 ms 150480 KB Output is correct
104 Correct 1306 ms 147828 KB Output is correct
105 Correct 1255 ms 141932 KB Output is correct
106 Correct 850 ms 135824 KB Output is correct
107 Correct 793 ms 134552 KB Output is correct
108 Correct 795 ms 136252 KB Output is correct
109 Correct 846 ms 134272 KB Output is correct
110 Correct 131 ms 76004 KB Output is correct
111 Correct 101 ms 71032 KB Output is correct
112 Correct 1233 ms 149008 KB Output is correct
113 Correct 552 ms 130376 KB Output is correct
114 Correct 896 ms 139152 KB Output is correct
115 Correct 238 ms 116972 KB Output is correct
116 Correct 734 ms 128832 KB Output is correct
117 Correct 3299 ms 192988 KB Output is correct
118 Correct 303 ms 116116 KB Output is correct
119 Correct 298 ms 116220 KB Output is correct
120 Correct 88 ms 59096 KB Output is correct
121 Correct 903 ms 132156 KB Output is correct
122 Correct 995 ms 132072 KB Output is correct
123 Correct 614 ms 133860 KB Output is correct
124 Correct 575 ms 132820 KB Output is correct
125 Correct 641 ms 135444 KB Output is correct
126 Correct 2091 ms 184404 KB Output is correct
127 Correct 1172 ms 156232 KB Output is correct
128 Correct 571 ms 158600 KB Output is correct
129 Correct 603 ms 158712 KB Output is correct
130 Correct 648 ms 157764 KB Output is correct