답안 #587523

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
587523 2022-07-02T04:36:52 Z penguinhacker Jail (JOI22_jail) C++17
100 / 100
1573 ms 498848 KB
#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define ar array

const int mxN=120000, INF=69696969, B=2000;
int n, m, s[mxN], t[mxN], t2[mxN], lc[mxN], cnt[mxN];
int p[mxN], d[mxN], hd[mxN], sz[mxN], tin[mxN], timer, tin2[mxN];
vector<int> adj[mxN];

void dfs1(int u=0) {
	sz[u]=1;
	for (int& v : adj[u]) {
		adj[v].erase(find(adj[v].begin(), adj[v].end(), u));
		p[v]=u, d[v]=d[u]+1;
		dfs1(v);
		sz[u]+=sz[v];
		if (sz[v]>sz[adj[u][0]])
			swap(adj[u][0], v);
	}
}

void dfs2(int u=0, int r=0) {
	hd[u]=r, tin[u]=timer++;
	if (adj[u].empty())
		return;
	dfs2(adj[u][0], r);
	for (int i=1; i<adj[u].size(); ++i)
		dfs2(adj[u][i], adj[u][i]);
}

int lca(int u, int v) {
	for (; hd[u]!=hd[v]; v=p[hd[v]])
		if (d[hd[u]]>d[hd[v]])
			swap(u, v);
	return d[u]<d[v]?u:v;
}

void dfs3(int u=0) {
	for (int v : adj[u])
		dfs3(v), cnt[u]+=cnt[v];
}

int init[mxN], st[1<<18], lz[1<<18];

void bld(int u=1, int l=0, int r=n-1) {
	lz[u]=0;
	if (l==r) {
		st[u]=init[l];
		return;
	}
	int mid=(l+r)/2;
	bld(2*u, l, mid);
	bld(2*u+1, mid+1, r);
	st[u]=min(st[2*u], st[2*u+1]);
}

void psh(int u, int l, int r) {
	if (lz[u]) {
		st[u]+=lz[u];
		if (l!=r)
			lz[2*u]+=lz[u], lz[2*u+1]+=lz[u];
		lz[u]=0;
	}
}

void upd(int ql, int qr, int x, int u=1, int l=0, int r=n-1) {
	psh(u, l, r);
	if (l>qr||r<ql)
		return;
	if (ql<=l&&r<=qr) {
		lz[u]=x;
		psh(u, l, r);
		return;
	}
	int mid=(l+r)/2;
	upd(ql, qr, x, 2*u, l, mid);
	upd(ql, qr, x, 2*u+1, mid+1, r);
	st[u]=min(st[2*u], st[2*u+1]);
}

int qry(int u=1, int l=0, int r=n-1) {
	psh(u, l, r);
	if (st[u]>1)
		return -1;
	if (l==r)
		return l;
	int mid=(l+r)/2;
	int x=qry(2*u, l, mid);
	return x!=-1?x:qry(2*u+1, mid+1, r);
}

void upd_path(int u, int v) {
	for (; hd[u]!=hd[v]; v=p[hd[v]]) {
		if (d[hd[u]]>d[hd[v]])
			swap(u, v);
		upd(tin[hd[v]], tin[v], -1);
	}
	if (tin[u]>tin[v])
		swap(u, v);
	upd(tin[u], tin[v], -1);
}

queue<int> q;
bool good[mxN][2];

void check(int i, int j) {
	assert(!good[i][j]);
	good[i][j]=1;
	if (good[i][0]&&good[i][1])
		q.push(i);
}

int ord[mxN], dp[mxN], highest[mxN], need[mxN];
bool active[mxN], small[mxN];
vector<int> todo[mxN];

void upd_active() {
	for (int i=0; i<n; ++i) {
		int u=ord[i];
		dp[u]=active[u];
		highest[u]=active[u]?u:(u?highest[p[u]]:-1);
		if (u)
			dp[u]+=dp[p[u]];
	}
}

int blocking(int i) {
	return dp[s[i]]+dp[t[i]]-dp[lc[i]]-(lc[i]?dp[p[lc[i]]]:0)-1;
}

void make_small(int i) {
	small[i]=1;
	need[i]=0;
	if (s[i])
		for (int u=highest[p[s[i]]]; u!=-1&&d[u]>d[lc[i]]; u=highest[p[u]])
			todo[u].push_back(i), ++need[i];
	for (int u=highest[t[i]]; u!=-1&&d[u]>d[lc[i]]; u=highest[p[u]])
		todo[u].push_back(i), ++need[i];
	if (active[lc[i]]&&lc[i]!=s[i])
		todo[lc[i]].push_back(i), ++need[i];
}

void solve() {
	cin >> n;
	for (int i=0; i<n; ++i) {
		adj[i].clear();
		active[i]=0;
		cnt[i]=0, t2[i]=-1;
		todo[i].clear();
	}
	for (int i=1; i<n; ++i) {
		int u, v;
		cin >> u >> v, --u, --v;
		adj[u].push_back(v);
		adj[v].push_back(u);
	}
	timer=0;
	dfs1();
	dfs2();
	cin >> m;
	memset(good, 0, 2*m);
	for (int i=0; i<m; ++i) {
		cin >> s[i] >> t[i], --s[i], --t[i];
		t2[t[i]]=i;
		lc[i]=lca(s[i], t[i]);
		active[s[i]]=1;
		++cnt[s[i]], ++cnt[t[i]], --cnt[lc[i]];
		if (lc[i])
			--cnt[p[lc[i]]];
	}
	dfs3();

	queue<int>().swap(q);
	for (int i=0; i<n; ++i) {
		if (cnt[i]==1&&t2[i]!=-1)
			check(t2[i], 0);
		tin2[tin[i]]=i;
		init[tin[i]]=cnt[i]<=1?INF:cnt[i];
	}
	bld();

	iota(ord, ord+n, 0);
	sort(ord, ord+n, [](int a, int b) { return d[a]<d[b]; });
	upd_active();
	for (int i=0; i<m; ++i) {
		int x=blocking(i);
		if (x==0) {
			small[i]=1;
			check(i, 1);
		} else if (x<=B+5)
			make_small(i);
	}

	for (int rep=0; q.size(); ++rep, q.pop()) {
		int i=q.front();
		//cout << i << endl;
		upd_path(s[i], t[i]);
		while(st[1]<=1) {
			assert(st[1]==1);
			int pos=qry();
			assert(pos!=-1);
			int u=tin2[pos];
			if (t2[u]!=-1)
				check(t2[u], 0);
			upd(pos, pos, INF);
		}
		active[s[i]]=0;
		for (int j : todo[s[i]])
			if ((--need[j])==0)
				check(j, 1);
		if (rep%B==B-1) {
			upd_active();
			for (int j=0; j<m; ++j) {
				if (small[j])
					continue;
				int x=blocking(j);
				if (x<=B+5)
					make_small(j);
			}
		}
	}
	for (int i=0; i<m; ++i)
		if (!good[i][0]||!good[i][1]) {
			cout << "No\n";
			return;
		}
	cout << "Yes\n";
}

int main() {
	ios::sync_with_stdio(0);
	cin.tie(0);
	int t;
	cin >> t;
	while(t--)
		solve();
	return 0;
}

Compilation message

jail.cpp: In function 'void dfs2(int, int)':
jail.cpp:29:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   29 |  for (int i=1; i<adj[u].size(); ++i)
      |                ~^~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 5972 KB Output is correct
2 Correct 3 ms 5972 KB Output is correct
3 Correct 3 ms 5972 KB Output is correct
4 Correct 12 ms 5972 KB Output is correct
5 Correct 24 ms 5972 KB Output is correct
6 Correct 4 ms 6100 KB Output is correct
7 Correct 4 ms 6100 KB Output is correct
8 Correct 5 ms 6100 KB Output is correct
9 Correct 55 ms 10172 KB Output is correct
10 Correct 97 ms 31040 KB Output is correct
11 Correct 8 ms 6100 KB Output is correct
12 Correct 40 ms 6996 KB Output is correct
13 Correct 180 ms 47016 KB Output is correct
14 Correct 113 ms 52712 KB Output is correct
15 Correct 137 ms 57468 KB Output is correct
16 Correct 157 ms 59148 KB Output is correct
17 Correct 961 ms 351560 KB Output is correct
18 Correct 170 ms 37612 KB Output is correct
19 Correct 1542 ms 498848 KB Output is correct
20 Correct 969 ms 352080 KB Output is correct
21 Correct 79 ms 42444 KB Output is correct
22 Correct 60 ms 31776 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 5972 KB Output is correct
2 Correct 4 ms 5972 KB Output is correct
3 Correct 4 ms 6100 KB Output is correct
4 Correct 5 ms 5972 KB Output is correct
5 Correct 5 ms 5972 KB Output is correct
6 Correct 5 ms 6100 KB Output is correct
7 Correct 5 ms 6068 KB Output is correct
8 Correct 4 ms 5972 KB Output is correct
9 Correct 5 ms 5972 KB Output is correct
10 Correct 4 ms 5972 KB Output is correct
11 Correct 4 ms 5972 KB Output is correct
12 Correct 3 ms 5972 KB Output is correct
13 Correct 4 ms 5972 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 5972 KB Output is correct
2 Correct 4 ms 5972 KB Output is correct
3 Correct 4 ms 6100 KB Output is correct
4 Correct 5 ms 5972 KB Output is correct
5 Correct 5 ms 5972 KB Output is correct
6 Correct 5 ms 6100 KB Output is correct
7 Correct 5 ms 6068 KB Output is correct
8 Correct 4 ms 5972 KB Output is correct
9 Correct 5 ms 5972 KB Output is correct
10 Correct 4 ms 5972 KB Output is correct
11 Correct 4 ms 5972 KB Output is correct
12 Correct 3 ms 5972 KB Output is correct
13 Correct 4 ms 5972 KB Output is correct
14 Correct 3 ms 5972 KB Output is correct
15 Correct 3 ms 5972 KB Output is correct
16 Correct 4 ms 6100 KB Output is correct
17 Correct 4 ms 5972 KB Output is correct
18 Correct 4 ms 5972 KB Output is correct
19 Correct 3 ms 5972 KB Output is correct
20 Correct 5 ms 5972 KB Output is correct
21 Correct 5 ms 5972 KB Output is correct
22 Correct 5 ms 6100 KB Output is correct
23 Correct 4 ms 5972 KB Output is correct
24 Correct 3 ms 5972 KB Output is correct
25 Correct 6 ms 5972 KB Output is correct
26 Correct 3 ms 5972 KB Output is correct
27 Correct 4 ms 5972 KB Output is correct
28 Correct 3 ms 5972 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 5972 KB Output is correct
2 Correct 4 ms 5972 KB Output is correct
3 Correct 4 ms 6100 KB Output is correct
4 Correct 5 ms 5972 KB Output is correct
5 Correct 5 ms 5972 KB Output is correct
6 Correct 5 ms 6100 KB Output is correct
7 Correct 5 ms 6068 KB Output is correct
8 Correct 4 ms 5972 KB Output is correct
9 Correct 5 ms 5972 KB Output is correct
10 Correct 4 ms 5972 KB Output is correct
11 Correct 4 ms 5972 KB Output is correct
12 Correct 3 ms 5972 KB Output is correct
13 Correct 4 ms 5972 KB Output is correct
14 Correct 3 ms 5972 KB Output is correct
15 Correct 3 ms 5972 KB Output is correct
16 Correct 4 ms 6100 KB Output is correct
17 Correct 4 ms 5972 KB Output is correct
18 Correct 4 ms 5972 KB Output is correct
19 Correct 3 ms 5972 KB Output is correct
20 Correct 5 ms 5972 KB Output is correct
21 Correct 5 ms 5972 KB Output is correct
22 Correct 5 ms 6100 KB Output is correct
23 Correct 4 ms 5972 KB Output is correct
24 Correct 3 ms 5972 KB Output is correct
25 Correct 6 ms 5972 KB Output is correct
26 Correct 3 ms 5972 KB Output is correct
27 Correct 4 ms 5972 KB Output is correct
28 Correct 3 ms 5972 KB Output is correct
29 Correct 5 ms 6100 KB Output is correct
30 Correct 5 ms 6100 KB Output is correct
31 Correct 4 ms 6100 KB Output is correct
32 Correct 5 ms 6100 KB Output is correct
33 Correct 5 ms 5972 KB Output is correct
34 Correct 5 ms 5972 KB Output is correct
35 Correct 5 ms 6100 KB Output is correct
36 Correct 6 ms 6100 KB Output is correct
37 Correct 6 ms 5972 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 5972 KB Output is correct
2 Correct 4 ms 5972 KB Output is correct
3 Correct 4 ms 6100 KB Output is correct
4 Correct 5 ms 5972 KB Output is correct
5 Correct 5 ms 5972 KB Output is correct
6 Correct 5 ms 6100 KB Output is correct
7 Correct 5 ms 6068 KB Output is correct
8 Correct 4 ms 5972 KB Output is correct
9 Correct 5 ms 5972 KB Output is correct
10 Correct 4 ms 5972 KB Output is correct
11 Correct 4 ms 5972 KB Output is correct
12 Correct 3 ms 5972 KB Output is correct
13 Correct 4 ms 5972 KB Output is correct
14 Correct 3 ms 5972 KB Output is correct
15 Correct 3 ms 5972 KB Output is correct
16 Correct 4 ms 6100 KB Output is correct
17 Correct 4 ms 5972 KB Output is correct
18 Correct 4 ms 5972 KB Output is correct
19 Correct 3 ms 5972 KB Output is correct
20 Correct 5 ms 5972 KB Output is correct
21 Correct 5 ms 5972 KB Output is correct
22 Correct 5 ms 6100 KB Output is correct
23 Correct 4 ms 5972 KB Output is correct
24 Correct 3 ms 5972 KB Output is correct
25 Correct 6 ms 5972 KB Output is correct
26 Correct 3 ms 5972 KB Output is correct
27 Correct 4 ms 5972 KB Output is correct
28 Correct 3 ms 5972 KB Output is correct
29 Correct 5 ms 6100 KB Output is correct
30 Correct 5 ms 6100 KB Output is correct
31 Correct 4 ms 6100 KB Output is correct
32 Correct 5 ms 6100 KB Output is correct
33 Correct 5 ms 5972 KB Output is correct
34 Correct 5 ms 5972 KB Output is correct
35 Correct 5 ms 6100 KB Output is correct
36 Correct 6 ms 6100 KB Output is correct
37 Correct 6 ms 5972 KB Output is correct
38 Correct 77 ms 10188 KB Output is correct
39 Correct 96 ms 30796 KB Output is correct
40 Correct 48 ms 9596 KB Output is correct
41 Correct 56 ms 7712 KB Output is correct
42 Correct 44 ms 9516 KB Output is correct
43 Correct 37 ms 7908 KB Output is correct
44 Correct 20 ms 6356 KB Output is correct
45 Correct 77 ms 19020 KB Output is correct
46 Correct 70 ms 19024 KB Output is correct
47 Correct 69 ms 24524 KB Output is correct
48 Correct 69 ms 24540 KB Output is correct
49 Correct 64 ms 19264 KB Output is correct
50 Correct 72 ms 19216 KB Output is correct
51 Correct 52 ms 20512 KB Output is correct
52 Correct 49 ms 20816 KB Output is correct
53 Correct 16 ms 7140 KB Output is correct
54 Correct 91 ms 18904 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 5972 KB Output is correct
2 Correct 4 ms 5972 KB Output is correct
3 Correct 4 ms 5972 KB Output is correct
4 Correct 4 ms 5972 KB Output is correct
5 Correct 11 ms 5972 KB Output is correct
6 Correct 4 ms 5972 KB Output is correct
7 Correct 3 ms 5972 KB Output is correct
8 Correct 5 ms 5972 KB Output is correct
9 Correct 3 ms 5948 KB Output is correct
10 Correct 3 ms 5972 KB Output is correct
11 Correct 3 ms 6064 KB Output is correct
12 Correct 4 ms 6100 KB Output is correct
13 Correct 24 ms 6080 KB Output is correct
14 Correct 55 ms 6076 KB Output is correct
15 Correct 37 ms 6096 KB Output is correct
16 Correct 105 ms 18012 KB Output is correct
17 Correct 114 ms 21832 KB Output is correct
18 Correct 209 ms 32352 KB Output is correct
19 Correct 151 ms 18344 KB Output is correct
20 Correct 129 ms 18332 KB Output is correct
21 Correct 186 ms 18308 KB Output is correct
22 Correct 103 ms 21576 KB Output is correct
23 Correct 86 ms 21516 KB Output is correct
24 Correct 312 ms 21680 KB Output is correct
25 Correct 313 ms 21764 KB Output is correct
26 Correct 293 ms 21704 KB Output is correct
27 Correct 409 ms 23880 KB Output is correct
28 Correct 81 ms 23900 KB Output is correct
29 Correct 79 ms 23872 KB Output is correct
30 Correct 212 ms 18564 KB Output is correct
31 Correct 84 ms 18596 KB Output is correct
32 Correct 236 ms 20712 KB Output is correct
33 Correct 95 ms 20708 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 5972 KB Output is correct
2 Correct 3 ms 5972 KB Output is correct
3 Correct 3 ms 5972 KB Output is correct
4 Correct 12 ms 5972 KB Output is correct
5 Correct 24 ms 5972 KB Output is correct
6 Correct 4 ms 6100 KB Output is correct
7 Correct 4 ms 6100 KB Output is correct
8 Correct 5 ms 6100 KB Output is correct
9 Correct 55 ms 10172 KB Output is correct
10 Correct 97 ms 31040 KB Output is correct
11 Correct 8 ms 6100 KB Output is correct
12 Correct 40 ms 6996 KB Output is correct
13 Correct 180 ms 47016 KB Output is correct
14 Correct 113 ms 52712 KB Output is correct
15 Correct 137 ms 57468 KB Output is correct
16 Correct 157 ms 59148 KB Output is correct
17 Correct 961 ms 351560 KB Output is correct
18 Correct 170 ms 37612 KB Output is correct
19 Correct 1542 ms 498848 KB Output is correct
20 Correct 969 ms 352080 KB Output is correct
21 Correct 79 ms 42444 KB Output is correct
22 Correct 60 ms 31776 KB Output is correct
23 Correct 4 ms 5972 KB Output is correct
24 Correct 4 ms 5972 KB Output is correct
25 Correct 4 ms 6100 KB Output is correct
26 Correct 5 ms 5972 KB Output is correct
27 Correct 5 ms 5972 KB Output is correct
28 Correct 5 ms 6100 KB Output is correct
29 Correct 5 ms 6068 KB Output is correct
30 Correct 4 ms 5972 KB Output is correct
31 Correct 5 ms 5972 KB Output is correct
32 Correct 4 ms 5972 KB Output is correct
33 Correct 4 ms 5972 KB Output is correct
34 Correct 3 ms 5972 KB Output is correct
35 Correct 4 ms 5972 KB Output is correct
36 Correct 3 ms 5972 KB Output is correct
37 Correct 3 ms 5972 KB Output is correct
38 Correct 4 ms 6100 KB Output is correct
39 Correct 4 ms 5972 KB Output is correct
40 Correct 4 ms 5972 KB Output is correct
41 Correct 3 ms 5972 KB Output is correct
42 Correct 5 ms 5972 KB Output is correct
43 Correct 5 ms 5972 KB Output is correct
44 Correct 5 ms 6100 KB Output is correct
45 Correct 4 ms 5972 KB Output is correct
46 Correct 3 ms 5972 KB Output is correct
47 Correct 6 ms 5972 KB Output is correct
48 Correct 3 ms 5972 KB Output is correct
49 Correct 4 ms 5972 KB Output is correct
50 Correct 3 ms 5972 KB Output is correct
51 Correct 5 ms 6100 KB Output is correct
52 Correct 5 ms 6100 KB Output is correct
53 Correct 4 ms 6100 KB Output is correct
54 Correct 5 ms 6100 KB Output is correct
55 Correct 5 ms 5972 KB Output is correct
56 Correct 5 ms 5972 KB Output is correct
57 Correct 5 ms 6100 KB Output is correct
58 Correct 6 ms 6100 KB Output is correct
59 Correct 6 ms 5972 KB Output is correct
60 Correct 77 ms 10188 KB Output is correct
61 Correct 96 ms 30796 KB Output is correct
62 Correct 48 ms 9596 KB Output is correct
63 Correct 56 ms 7712 KB Output is correct
64 Correct 44 ms 9516 KB Output is correct
65 Correct 37 ms 7908 KB Output is correct
66 Correct 20 ms 6356 KB Output is correct
67 Correct 77 ms 19020 KB Output is correct
68 Correct 70 ms 19024 KB Output is correct
69 Correct 69 ms 24524 KB Output is correct
70 Correct 69 ms 24540 KB Output is correct
71 Correct 64 ms 19264 KB Output is correct
72 Correct 72 ms 19216 KB Output is correct
73 Correct 52 ms 20512 KB Output is correct
74 Correct 49 ms 20816 KB Output is correct
75 Correct 16 ms 7140 KB Output is correct
76 Correct 91 ms 18904 KB Output is correct
77 Correct 3 ms 5972 KB Output is correct
78 Correct 4 ms 5972 KB Output is correct
79 Correct 4 ms 5972 KB Output is correct
80 Correct 4 ms 5972 KB Output is correct
81 Correct 11 ms 5972 KB Output is correct
82 Correct 4 ms 5972 KB Output is correct
83 Correct 3 ms 5972 KB Output is correct
84 Correct 5 ms 5972 KB Output is correct
85 Correct 3 ms 5948 KB Output is correct
86 Correct 3 ms 5972 KB Output is correct
87 Correct 3 ms 6064 KB Output is correct
88 Correct 4 ms 6100 KB Output is correct
89 Correct 24 ms 6080 KB Output is correct
90 Correct 55 ms 6076 KB Output is correct
91 Correct 37 ms 6096 KB Output is correct
92 Correct 105 ms 18012 KB Output is correct
93 Correct 114 ms 21832 KB Output is correct
94 Correct 209 ms 32352 KB Output is correct
95 Correct 151 ms 18344 KB Output is correct
96 Correct 129 ms 18332 KB Output is correct
97 Correct 186 ms 18308 KB Output is correct
98 Correct 103 ms 21576 KB Output is correct
99 Correct 86 ms 21516 KB Output is correct
100 Correct 312 ms 21680 KB Output is correct
101 Correct 313 ms 21764 KB Output is correct
102 Correct 293 ms 21704 KB Output is correct
103 Correct 409 ms 23880 KB Output is correct
104 Correct 81 ms 23900 KB Output is correct
105 Correct 79 ms 23872 KB Output is correct
106 Correct 212 ms 18564 KB Output is correct
107 Correct 84 ms 18596 KB Output is correct
108 Correct 236 ms 20712 KB Output is correct
109 Correct 95 ms 20708 KB Output is correct
110 Correct 47 ms 6928 KB Output is correct
111 Correct 36 ms 6604 KB Output is correct
112 Correct 213 ms 77252 KB Output is correct
113 Correct 143 ms 25576 KB Output is correct
114 Correct 161 ms 49804 KB Output is correct
115 Correct 41 ms 19032 KB Output is correct
116 Correct 186 ms 21024 KB Output is correct
117 Correct 400 ms 40036 KB Output is correct
118 Correct 91 ms 18916 KB Output is correct
119 Correct 112 ms 18904 KB Output is correct
120 Correct 19 ms 7276 KB Output is correct
121 Correct 264 ms 20812 KB Output is correct
122 Correct 223 ms 20668 KB Output is correct
123 Correct 231 ms 27836 KB Output is correct
124 Correct 93 ms 35032 KB Output is correct
125 Correct 143 ms 28176 KB Output is correct
126 Correct 212 ms 71052 KB Output is correct
127 Correct 106 ms 40640 KB Output is correct
128 Correct 107 ms 40780 KB Output is correct
129 Correct 1573 ms 452480 KB Output is correct
130 Correct 120 ms 40576 KB Output is correct