Submission #631605

# Submission time Handle Problem Language Result Execution time Memory
631605 2022-08-18T09:54:35 Z LittleCube Jail (JOI22_jail) C++14
77 / 100
5000 ms 729868 KB
#pragma GCC optimize("Ofast,unroll-loops")
#include <bits/stdc++.h>
#define ll long long
#define pii pair<int, int>
#define pll pair<ll, ll>
#define F first
#define S second
using namespace std;

int T, N, M, pre[1200005], dis[1200005], s[1200005], e[1200005], deg[6000005], fr[1200005], to[1200005], Hhead[1200005], Hsz[1200005], Hpos[1200005], idx, ts;
pii io[1200005];
vector<int> E[1200005], topo[6000005];

struct HLD
{
	// 0: start, 1: end
	int root[1600005][2] = {};
	int l[6000005] = {};
	int r[6000005] = {};
	
	inline void init()
	{
		for(int i = 1; i <= 6 * N; i++)
			l[i] = r[i] = 0;
		for(int i = 1; i <= N; i++)
			root[i][0] = root[i][1] = 0;
		for(int i = 1; i <= N; i++)
			if(Hsz[i])
			{
				// cout << "HLD: " << i << '\n';
				root[i][0] = ++idx;
				segInit(idx, 1, Hsz[i], 0);
				root[i][1] = ++idx;
				segInit(idx, 1, Hsz[i], 1);
			}
	}

	inline void segInit(int i, int L, int R, int type)
	{
		// cout << i << ' ' << L << ' ' << R << ' ' << type << '\n';
		if(L == R)
			return;
		int mid = (L + R) / 2;
		l[i] = ++idx;
		segInit(l[i], L, mid, type);
		r[i] = ++idx;
		segInit(r[i], mid + 1, R, type);

		if(type == 0)
		{
			topo[l[i]].emplace_back(i);
			topo[r[i]].emplace_back(i);
		}
		else
		{
			topo[i].emplace_back(l[i]);
			topo[i].emplace_back(r[i]);
		}
	}

	inline void segInsert(int &i, int L, int &R, int &pos, int &j, int type)
	{
		if(L == R)
		{
			// cout << i << ' ' << j << ' ' << type << '\n';
			if(type == 0)
				topo[j].emplace_back(i);
			else 
				topo[i].emplace_back(j);
		}
		else
		{
			int mid = (L + R) / 2;
			if(pos <= mid)
				segInsert(l[i], L, mid, pos, j, type);
			else
				segInsert(r[i], mid + 1, R, pos, j, type);
		}
	}

	inline void segEdge(int &r1, int &r2, int L, int &R, int &j, int mL, int &mR)
	{
		if(mL <= L && R <= mR)
		{
			topo[r1].emplace_back(j);
			topo[j].emplace_back(r2);
		}
		else if(R < mL || mR < L)
			return;
		else
		{
			int mid = (L + R) / 2;
			segEdge(l[r1], l[r2], L, mid, j, mL, mR);
			segEdge(r[r1], r[r2], mid + 1, R, j, mL, mR);
		}
	}
}hld;

inline void dfs(int u)
{
	io[u].F = ++ts;
	for(int v : E[u])
		if(pre[u] != v)
		{
			pre[v] = u;
			dis[v] = dis[u] + 1;
			dfs(v);
		}
	io[u].S = ts;
}

inline void decompose(int u, int h)
{
	int deep = 0;
	Hhead[u] = h;
	for(int v : E[u])
		if(pre[u] != v)
		{
			if(dis[v] > dis[deep])
				deep = v;
		}
	for(int v : E[u])
		if(pre[u] != v)
		{
			Hpos[v] = (v == deep ? Hpos[u] + 1 : 1);
			decompose(v, (v == deep ? h : v));
		}
}

inline void addedge(int pos, int i)
{
	if(s[pos] && s[pos] != i)
		topo[s[pos]].emplace_back(i);
	if(e[pos] && e[pos] != i)
		topo[i].emplace_back(e[pos]);
}

inline bool isSubtree(int r, int c)
{
	return io[r].F <= io[c].F && io[c].S <= io[r].S;
}

inline void solve()
{
	ts = 0, idx = 0;
	cin >> N;
	for(int i = 1; i <= N; i++)
	{
		pre[i] = dis[i] = s[i] = e[i] = Hhead[i] = Hsz[i] = Hpos[i] = 0;
		E[i].clear();
	}
	for(int i = 1; i < N; i++)
	{
		int u, v;
		cin >> u >> v;
		E[u].emplace_back(v);
		E[v].emplace_back(u);
	}
	
	dfs(1);
	Hpos[1] = 1;
	decompose(1, 1);

	cin >> M;
	idx = M;
	
	for(int i = 1; i <= 6 * N; i++)
	{
		deg[i] = 0;
		topo[i].clear();
	}
	
	for(int i = 1; i <= N; i++)
		Hsz[Hhead[i]] = max(Hsz[Hhead[i]], Hpos[i]);
	
	hld.init();

	for(int i = 1; i <= M; i++)
	{
		cin >> fr[i] >> to[i];
		s[fr[i]] = i;
		e[to[i]] = i;
		hld.segInsert(hld.root[Hhead[fr[i]]][0], 1, Hsz[Hhead[fr[i]]], Hpos[fr[i]], i, 0);
		hld.segInsert(hld.root[Hhead[to[i]]][1], 1, Hsz[Hhead[to[i]]], Hpos[to[i]], i, 1);
	}
	for(int i = 1; i <= M; i++)
	{
		int u = fr[i], v = to[i];
		addedge(u, i);
		addedge(v, i);

		if(pre[u] == v || pre[v] == u) continue;

		int cancelUp = 1;
		
		if(isSubtree(u, v))
			v = pre[v];
		else if(isSubtree(v, u))
			u = pre[u];
		else
			u = pre[u], v = pre[v], cancelUp = 0;

		while(Hhead[u] != Hhead[v])
		{
			if(dis[Hhead[u]] < dis[Hhead[v]])
				swap(u, v);
			hld.segEdge(hld.root[Hhead[u]][0], hld.root[Hhead[u]][1], 1, Hsz[Hhead[u]], i, 1, Hpos[u]);
			u = pre[Hhead[u]];
		}
		
		if(dis[u] > dis[v]) swap(u, v);

		hld.segEdge(hld.root[Hhead[u]][0], hld.root[Hhead[u]][1], 1, Hsz[Hhead[u]], i, Hpos[u] + cancelUp, Hpos[v]);
	}

	for(int i = 1; i <= idx; i++)
		for(int j : topo[i])
		{	
			// cout << i << "->" << j << '\n';
			deg[j]++;
		}
	queue<int> q;
	for(int i = 1; i <= idx; i++)
		if(deg[i] == 0)
			q.emplace(i);

	for(int t = 1; t <= idx; t++)
	{
		if(q.empty())
		{
			cout << "No\n";
			return;
		}
		int u = q.front();
		q.pop();
		for(int v : topo[u])
		{
			deg[v]--;
			if(deg[v] == 0)
				q.emplace(v);
		}
	}
	cout << "Yes\n";
}



signed main()
{
	ios::sync_with_stdio(0);
	cin.tie(0), cout.tie(0);
	cin >> T;
	while(T--)
	{
		solve();
	}
}
# Verdict Execution time Memory Grader output
1 Correct 84 ms 169452 KB Output is correct
2 Correct 96 ms 169436 KB Output is correct
3 Correct 91 ms 169488 KB Output is correct
4 Correct 98 ms 169548 KB Output is correct
5 Correct 113 ms 169532 KB Output is correct
6 Correct 89 ms 169488 KB Output is correct
7 Correct 87 ms 169564 KB Output is correct
8 Correct 86 ms 169548 KB Output is correct
9 Correct 135 ms 171412 KB Output is correct
10 Correct 177 ms 203352 KB Output is correct
11 Correct 95 ms 169464 KB Output is correct
12 Correct 132 ms 169472 KB Output is correct
13 Correct 245 ms 210380 KB Output is correct
14 Correct 216 ms 210488 KB Output is correct
15 Correct 383 ms 215508 KB Output is correct
16 Correct 671 ms 233032 KB Output is correct
17 Correct 278 ms 219408 KB Output is correct
18 Correct 214 ms 211580 KB Output is correct
19 Correct 308 ms 217064 KB Output is correct
20 Correct 254 ms 216984 KB Output is correct
21 Correct 267 ms 218104 KB Output is correct
22 Correct 178 ms 207108 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 92 ms 169400 KB Output is correct
2 Correct 95 ms 169492 KB Output is correct
3 Correct 87 ms 169488 KB Output is correct
4 Correct 90 ms 169508 KB Output is correct
5 Correct 88 ms 169548 KB Output is correct
6 Correct 86 ms 169444 KB Output is correct
7 Correct 89 ms 169548 KB Output is correct
8 Correct 88 ms 169560 KB Output is correct
9 Correct 93 ms 169440 KB Output is correct
10 Correct 86 ms 169476 KB Output is correct
11 Correct 84 ms 169436 KB Output is correct
12 Correct 87 ms 169420 KB Output is correct
13 Correct 98 ms 169496 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 92 ms 169400 KB Output is correct
2 Correct 95 ms 169492 KB Output is correct
3 Correct 87 ms 169488 KB Output is correct
4 Correct 90 ms 169508 KB Output is correct
5 Correct 88 ms 169548 KB Output is correct
6 Correct 86 ms 169444 KB Output is correct
7 Correct 89 ms 169548 KB Output is correct
8 Correct 88 ms 169560 KB Output is correct
9 Correct 93 ms 169440 KB Output is correct
10 Correct 86 ms 169476 KB Output is correct
11 Correct 84 ms 169436 KB Output is correct
12 Correct 87 ms 169420 KB Output is correct
13 Correct 98 ms 169496 KB Output is correct
14 Correct 87 ms 169416 KB Output is correct
15 Correct 84 ms 169492 KB Output is correct
16 Correct 89 ms 169560 KB Output is correct
17 Correct 89 ms 169436 KB Output is correct
18 Correct 90 ms 169548 KB Output is correct
19 Correct 91 ms 169380 KB Output is correct
20 Correct 89 ms 169564 KB Output is correct
21 Correct 89 ms 169560 KB Output is correct
22 Correct 88 ms 169548 KB Output is correct
23 Correct 88 ms 169496 KB Output is correct
24 Correct 86 ms 169508 KB Output is correct
25 Correct 87 ms 169492 KB Output is correct
26 Correct 89 ms 169504 KB Output is correct
27 Correct 90 ms 169456 KB Output is correct
28 Correct 89 ms 169532 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 92 ms 169400 KB Output is correct
2 Correct 95 ms 169492 KB Output is correct
3 Correct 87 ms 169488 KB Output is correct
4 Correct 90 ms 169508 KB Output is correct
5 Correct 88 ms 169548 KB Output is correct
6 Correct 86 ms 169444 KB Output is correct
7 Correct 89 ms 169548 KB Output is correct
8 Correct 88 ms 169560 KB Output is correct
9 Correct 93 ms 169440 KB Output is correct
10 Correct 86 ms 169476 KB Output is correct
11 Correct 84 ms 169436 KB Output is correct
12 Correct 87 ms 169420 KB Output is correct
13 Correct 98 ms 169496 KB Output is correct
14 Correct 87 ms 169416 KB Output is correct
15 Correct 84 ms 169492 KB Output is correct
16 Correct 89 ms 169560 KB Output is correct
17 Correct 89 ms 169436 KB Output is correct
18 Correct 90 ms 169548 KB Output is correct
19 Correct 91 ms 169380 KB Output is correct
20 Correct 89 ms 169564 KB Output is correct
21 Correct 89 ms 169560 KB Output is correct
22 Correct 88 ms 169548 KB Output is correct
23 Correct 88 ms 169496 KB Output is correct
24 Correct 86 ms 169508 KB Output is correct
25 Correct 87 ms 169492 KB Output is correct
26 Correct 89 ms 169504 KB Output is correct
27 Correct 90 ms 169456 KB Output is correct
28 Correct 89 ms 169532 KB Output is correct
29 Correct 87 ms 169588 KB Output is correct
30 Correct 97 ms 169456 KB Output is correct
31 Correct 107 ms 169656 KB Output is correct
32 Correct 88 ms 169624 KB Output is correct
33 Correct 85 ms 169484 KB Output is correct
34 Correct 91 ms 169552 KB Output is correct
35 Correct 88 ms 169624 KB Output is correct
36 Correct 91 ms 169556 KB Output is correct
37 Correct 86 ms 169548 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 92 ms 169400 KB Output is correct
2 Correct 95 ms 169492 KB Output is correct
3 Correct 87 ms 169488 KB Output is correct
4 Correct 90 ms 169508 KB Output is correct
5 Correct 88 ms 169548 KB Output is correct
6 Correct 86 ms 169444 KB Output is correct
7 Correct 89 ms 169548 KB Output is correct
8 Correct 88 ms 169560 KB Output is correct
9 Correct 93 ms 169440 KB Output is correct
10 Correct 86 ms 169476 KB Output is correct
11 Correct 84 ms 169436 KB Output is correct
12 Correct 87 ms 169420 KB Output is correct
13 Correct 98 ms 169496 KB Output is correct
14 Correct 87 ms 169416 KB Output is correct
15 Correct 84 ms 169492 KB Output is correct
16 Correct 89 ms 169560 KB Output is correct
17 Correct 89 ms 169436 KB Output is correct
18 Correct 90 ms 169548 KB Output is correct
19 Correct 91 ms 169380 KB Output is correct
20 Correct 89 ms 169564 KB Output is correct
21 Correct 89 ms 169560 KB Output is correct
22 Correct 88 ms 169548 KB Output is correct
23 Correct 88 ms 169496 KB Output is correct
24 Correct 86 ms 169508 KB Output is correct
25 Correct 87 ms 169492 KB Output is correct
26 Correct 89 ms 169504 KB Output is correct
27 Correct 90 ms 169456 KB Output is correct
28 Correct 89 ms 169532 KB Output is correct
29 Correct 87 ms 169588 KB Output is correct
30 Correct 97 ms 169456 KB Output is correct
31 Correct 107 ms 169656 KB Output is correct
32 Correct 88 ms 169624 KB Output is correct
33 Correct 85 ms 169484 KB Output is correct
34 Correct 91 ms 169552 KB Output is correct
35 Correct 88 ms 169624 KB Output is correct
36 Correct 91 ms 169556 KB Output is correct
37 Correct 86 ms 169548 KB Output is correct
38 Correct 134 ms 171372 KB Output is correct
39 Correct 165 ms 203476 KB Output is correct
40 Correct 226 ms 180500 KB Output is correct
41 Correct 149 ms 171180 KB Output is correct
42 Correct 122 ms 171340 KB Output is correct
43 Correct 121 ms 171072 KB Output is correct
44 Correct 99 ms 169988 KB Output is correct
45 Correct 176 ms 195072 KB Output is correct
46 Correct 175 ms 195080 KB Output is correct
47 Correct 616 ms 237776 KB Output is correct
48 Correct 820 ms 238916 KB Output is correct
49 Correct 197 ms 197352 KB Output is correct
50 Correct 205 ms 197424 KB Output is correct
51 Correct 153 ms 195624 KB Output is correct
52 Correct 152 ms 195672 KB Output is correct
53 Correct 98 ms 171300 KB Output is correct
54 Correct 169 ms 193844 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 88 ms 169468 KB Output is correct
2 Correct 89 ms 169456 KB Output is correct
3 Correct 99 ms 169436 KB Output is correct
4 Correct 85 ms 169396 KB Output is correct
5 Correct 102 ms 169512 KB Output is correct
6 Correct 86 ms 169444 KB Output is correct
7 Correct 87 ms 169544 KB Output is correct
8 Correct 87 ms 169420 KB Output is correct
9 Correct 88 ms 169488 KB Output is correct
10 Correct 86 ms 169420 KB Output is correct
11 Correct 86 ms 169384 KB Output is correct
12 Correct 87 ms 169548 KB Output is correct
13 Correct 113 ms 169584 KB Output is correct
14 Correct 134 ms 169564 KB Output is correct
15 Correct 123 ms 169692 KB Output is correct
16 Correct 202 ms 194320 KB Output is correct
17 Correct 349 ms 203972 KB Output is correct
18 Correct 544 ms 216760 KB Output is correct
19 Correct 242 ms 196172 KB Output is correct
20 Correct 244 ms 196596 KB Output is correct
21 Correct 246 ms 196556 KB Output is correct
22 Correct 316 ms 204440 KB Output is correct
23 Correct 270 ms 204004 KB Output is correct
24 Correct 297 ms 204408 KB Output is correct
25 Correct 299 ms 203932 KB Output is correct
26 Correct 312 ms 204080 KB Output is correct
27 Correct 271 ms 196852 KB Output is correct
28 Correct 257 ms 196676 KB Output is correct
29 Correct 243 ms 196708 KB Output is correct
30 Correct 303 ms 197836 KB Output is correct
31 Correct 246 ms 197780 KB Output is correct
32 Correct 309 ms 197624 KB Output is correct
33 Correct 248 ms 197636 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 84 ms 169452 KB Output is correct
2 Correct 96 ms 169436 KB Output is correct
3 Correct 91 ms 169488 KB Output is correct
4 Correct 98 ms 169548 KB Output is correct
5 Correct 113 ms 169532 KB Output is correct
6 Correct 89 ms 169488 KB Output is correct
7 Correct 87 ms 169564 KB Output is correct
8 Correct 86 ms 169548 KB Output is correct
9 Correct 135 ms 171412 KB Output is correct
10 Correct 177 ms 203352 KB Output is correct
11 Correct 95 ms 169464 KB Output is correct
12 Correct 132 ms 169472 KB Output is correct
13 Correct 245 ms 210380 KB Output is correct
14 Correct 216 ms 210488 KB Output is correct
15 Correct 383 ms 215508 KB Output is correct
16 Correct 671 ms 233032 KB Output is correct
17 Correct 278 ms 219408 KB Output is correct
18 Correct 214 ms 211580 KB Output is correct
19 Correct 308 ms 217064 KB Output is correct
20 Correct 254 ms 216984 KB Output is correct
21 Correct 267 ms 218104 KB Output is correct
22 Correct 178 ms 207108 KB Output is correct
23 Correct 92 ms 169400 KB Output is correct
24 Correct 95 ms 169492 KB Output is correct
25 Correct 87 ms 169488 KB Output is correct
26 Correct 90 ms 169508 KB Output is correct
27 Correct 88 ms 169548 KB Output is correct
28 Correct 86 ms 169444 KB Output is correct
29 Correct 89 ms 169548 KB Output is correct
30 Correct 88 ms 169560 KB Output is correct
31 Correct 93 ms 169440 KB Output is correct
32 Correct 86 ms 169476 KB Output is correct
33 Correct 84 ms 169436 KB Output is correct
34 Correct 87 ms 169420 KB Output is correct
35 Correct 98 ms 169496 KB Output is correct
36 Correct 87 ms 169416 KB Output is correct
37 Correct 84 ms 169492 KB Output is correct
38 Correct 89 ms 169560 KB Output is correct
39 Correct 89 ms 169436 KB Output is correct
40 Correct 90 ms 169548 KB Output is correct
41 Correct 91 ms 169380 KB Output is correct
42 Correct 89 ms 169564 KB Output is correct
43 Correct 89 ms 169560 KB Output is correct
44 Correct 88 ms 169548 KB Output is correct
45 Correct 88 ms 169496 KB Output is correct
46 Correct 86 ms 169508 KB Output is correct
47 Correct 87 ms 169492 KB Output is correct
48 Correct 89 ms 169504 KB Output is correct
49 Correct 90 ms 169456 KB Output is correct
50 Correct 89 ms 169532 KB Output is correct
51 Correct 87 ms 169588 KB Output is correct
52 Correct 97 ms 169456 KB Output is correct
53 Correct 107 ms 169656 KB Output is correct
54 Correct 88 ms 169624 KB Output is correct
55 Correct 85 ms 169484 KB Output is correct
56 Correct 91 ms 169552 KB Output is correct
57 Correct 88 ms 169624 KB Output is correct
58 Correct 91 ms 169556 KB Output is correct
59 Correct 86 ms 169548 KB Output is correct
60 Correct 134 ms 171372 KB Output is correct
61 Correct 165 ms 203476 KB Output is correct
62 Correct 226 ms 180500 KB Output is correct
63 Correct 149 ms 171180 KB Output is correct
64 Correct 122 ms 171340 KB Output is correct
65 Correct 121 ms 171072 KB Output is correct
66 Correct 99 ms 169988 KB Output is correct
67 Correct 176 ms 195072 KB Output is correct
68 Correct 175 ms 195080 KB Output is correct
69 Correct 616 ms 237776 KB Output is correct
70 Correct 820 ms 238916 KB Output is correct
71 Correct 197 ms 197352 KB Output is correct
72 Correct 205 ms 197424 KB Output is correct
73 Correct 153 ms 195624 KB Output is correct
74 Correct 152 ms 195672 KB Output is correct
75 Correct 98 ms 171300 KB Output is correct
76 Correct 169 ms 193844 KB Output is correct
77 Correct 88 ms 169468 KB Output is correct
78 Correct 89 ms 169456 KB Output is correct
79 Correct 99 ms 169436 KB Output is correct
80 Correct 85 ms 169396 KB Output is correct
81 Correct 102 ms 169512 KB Output is correct
82 Correct 86 ms 169444 KB Output is correct
83 Correct 87 ms 169544 KB Output is correct
84 Correct 87 ms 169420 KB Output is correct
85 Correct 88 ms 169488 KB Output is correct
86 Correct 86 ms 169420 KB Output is correct
87 Correct 86 ms 169384 KB Output is correct
88 Correct 87 ms 169548 KB Output is correct
89 Correct 113 ms 169584 KB Output is correct
90 Correct 134 ms 169564 KB Output is correct
91 Correct 123 ms 169692 KB Output is correct
92 Correct 202 ms 194320 KB Output is correct
93 Correct 349 ms 203972 KB Output is correct
94 Correct 544 ms 216760 KB Output is correct
95 Correct 242 ms 196172 KB Output is correct
96 Correct 244 ms 196596 KB Output is correct
97 Correct 246 ms 196556 KB Output is correct
98 Correct 316 ms 204440 KB Output is correct
99 Correct 270 ms 204004 KB Output is correct
100 Correct 297 ms 204408 KB Output is correct
101 Correct 299 ms 203932 KB Output is correct
102 Correct 312 ms 204080 KB Output is correct
103 Correct 271 ms 196852 KB Output is correct
104 Correct 257 ms 196676 KB Output is correct
105 Correct 243 ms 196708 KB Output is correct
106 Correct 303 ms 197836 KB Output is correct
107 Correct 246 ms 197780 KB Output is correct
108 Correct 309 ms 197624 KB Output is correct
109 Correct 248 ms 197636 KB Output is correct
110 Correct 133 ms 169600 KB Output is correct
111 Correct 118 ms 169564 KB Output is correct
112 Execution timed out 5085 ms 729868 KB Time limit exceeded
113 Halted 0 ms 0 KB -