답안 #631580

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
631580 2022-08-18T09:20:00 Z LittleCube Jail (JOI22_jail) C++14
77 / 100
5000 ms 787844 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[8000005], fr[1200005], to[1200005], Hhead[1200005], Hsz[1200005], Hpos[1200005], idx, ts;
pii io[1200005];
vector<int> E[1200005], topo[8000005];

struct HLD
{
	// 0: start, 1: end
	int root[1600005][2] = {};
	int l[8000005] = {};
	int r[8000005] = {};
	
	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();
	}
}
# 결과 실행 시간 메모리 Grader output
1 Correct 115 ms 216328 KB Output is correct
2 Correct 109 ms 216552 KB Output is correct
3 Correct 114 ms 216416 KB Output is correct
4 Correct 124 ms 216472 KB Output is correct
5 Correct 151 ms 216488 KB Output is correct
6 Correct 118 ms 216412 KB Output is correct
7 Correct 118 ms 216520 KB Output is correct
8 Correct 112 ms 216536 KB Output is correct
9 Correct 160 ms 218400 KB Output is correct
10 Correct 191 ms 250312 KB Output is correct
11 Correct 120 ms 216444 KB Output is correct
12 Correct 155 ms 216544 KB Output is correct
13 Correct 260 ms 257228 KB Output is correct
14 Correct 248 ms 257452 KB Output is correct
15 Correct 400 ms 262532 KB Output is correct
16 Correct 751 ms 280064 KB Output is correct
17 Correct 316 ms 266356 KB Output is correct
18 Correct 263 ms 258616 KB Output is correct
19 Correct 297 ms 263944 KB Output is correct
20 Correct 283 ms 263992 KB Output is correct
21 Correct 285 ms 265080 KB Output is correct
22 Correct 227 ms 254152 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 109 ms 216452 KB Output is correct
2 Correct 107 ms 216372 KB Output is correct
3 Correct 114 ms 216472 KB Output is correct
4 Correct 111 ms 216440 KB Output is correct
5 Correct 117 ms 216624 KB Output is correct
6 Correct 111 ms 216408 KB Output is correct
7 Correct 134 ms 216456 KB Output is correct
8 Correct 113 ms 216448 KB Output is correct
9 Correct 116 ms 216524 KB Output is correct
10 Correct 111 ms 216516 KB Output is correct
11 Correct 116 ms 216408 KB Output is correct
12 Correct 111 ms 216472 KB Output is correct
13 Correct 113 ms 216464 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 109 ms 216452 KB Output is correct
2 Correct 107 ms 216372 KB Output is correct
3 Correct 114 ms 216472 KB Output is correct
4 Correct 111 ms 216440 KB Output is correct
5 Correct 117 ms 216624 KB Output is correct
6 Correct 111 ms 216408 KB Output is correct
7 Correct 134 ms 216456 KB Output is correct
8 Correct 113 ms 216448 KB Output is correct
9 Correct 116 ms 216524 KB Output is correct
10 Correct 111 ms 216516 KB Output is correct
11 Correct 116 ms 216408 KB Output is correct
12 Correct 111 ms 216472 KB Output is correct
13 Correct 113 ms 216464 KB Output is correct
14 Correct 127 ms 216416 KB Output is correct
15 Correct 118 ms 216384 KB Output is correct
16 Correct 113 ms 216524 KB Output is correct
17 Correct 114 ms 216480 KB Output is correct
18 Correct 122 ms 216536 KB Output is correct
19 Correct 117 ms 216456 KB Output is correct
20 Correct 118 ms 216540 KB Output is correct
21 Correct 115 ms 216536 KB Output is correct
22 Correct 117 ms 216464 KB Output is correct
23 Correct 117 ms 216388 KB Output is correct
24 Correct 115 ms 216420 KB Output is correct
25 Correct 111 ms 216496 KB Output is correct
26 Correct 110 ms 216520 KB Output is correct
27 Correct 121 ms 216424 KB Output is correct
28 Correct 112 ms 216432 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 109 ms 216452 KB Output is correct
2 Correct 107 ms 216372 KB Output is correct
3 Correct 114 ms 216472 KB Output is correct
4 Correct 111 ms 216440 KB Output is correct
5 Correct 117 ms 216624 KB Output is correct
6 Correct 111 ms 216408 KB Output is correct
7 Correct 134 ms 216456 KB Output is correct
8 Correct 113 ms 216448 KB Output is correct
9 Correct 116 ms 216524 KB Output is correct
10 Correct 111 ms 216516 KB Output is correct
11 Correct 116 ms 216408 KB Output is correct
12 Correct 111 ms 216472 KB Output is correct
13 Correct 113 ms 216464 KB Output is correct
14 Correct 127 ms 216416 KB Output is correct
15 Correct 118 ms 216384 KB Output is correct
16 Correct 113 ms 216524 KB Output is correct
17 Correct 114 ms 216480 KB Output is correct
18 Correct 122 ms 216536 KB Output is correct
19 Correct 117 ms 216456 KB Output is correct
20 Correct 118 ms 216540 KB Output is correct
21 Correct 115 ms 216536 KB Output is correct
22 Correct 117 ms 216464 KB Output is correct
23 Correct 117 ms 216388 KB Output is correct
24 Correct 115 ms 216420 KB Output is correct
25 Correct 111 ms 216496 KB Output is correct
26 Correct 110 ms 216520 KB Output is correct
27 Correct 121 ms 216424 KB Output is correct
28 Correct 112 ms 216432 KB Output is correct
29 Correct 112 ms 216472 KB Output is correct
30 Correct 113 ms 216548 KB Output is correct
31 Correct 123 ms 216596 KB Output is correct
32 Correct 120 ms 216496 KB Output is correct
33 Correct 113 ms 216524 KB Output is correct
34 Correct 115 ms 216428 KB Output is correct
35 Correct 116 ms 216604 KB Output is correct
36 Correct 122 ms 216508 KB Output is correct
37 Correct 115 ms 216464 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 109 ms 216452 KB Output is correct
2 Correct 107 ms 216372 KB Output is correct
3 Correct 114 ms 216472 KB Output is correct
4 Correct 111 ms 216440 KB Output is correct
5 Correct 117 ms 216624 KB Output is correct
6 Correct 111 ms 216408 KB Output is correct
7 Correct 134 ms 216456 KB Output is correct
8 Correct 113 ms 216448 KB Output is correct
9 Correct 116 ms 216524 KB Output is correct
10 Correct 111 ms 216516 KB Output is correct
11 Correct 116 ms 216408 KB Output is correct
12 Correct 111 ms 216472 KB Output is correct
13 Correct 113 ms 216464 KB Output is correct
14 Correct 127 ms 216416 KB Output is correct
15 Correct 118 ms 216384 KB Output is correct
16 Correct 113 ms 216524 KB Output is correct
17 Correct 114 ms 216480 KB Output is correct
18 Correct 122 ms 216536 KB Output is correct
19 Correct 117 ms 216456 KB Output is correct
20 Correct 118 ms 216540 KB Output is correct
21 Correct 115 ms 216536 KB Output is correct
22 Correct 117 ms 216464 KB Output is correct
23 Correct 117 ms 216388 KB Output is correct
24 Correct 115 ms 216420 KB Output is correct
25 Correct 111 ms 216496 KB Output is correct
26 Correct 110 ms 216520 KB Output is correct
27 Correct 121 ms 216424 KB Output is correct
28 Correct 112 ms 216432 KB Output is correct
29 Correct 112 ms 216472 KB Output is correct
30 Correct 113 ms 216548 KB Output is correct
31 Correct 123 ms 216596 KB Output is correct
32 Correct 120 ms 216496 KB Output is correct
33 Correct 113 ms 216524 KB Output is correct
34 Correct 115 ms 216428 KB Output is correct
35 Correct 116 ms 216604 KB Output is correct
36 Correct 122 ms 216508 KB Output is correct
37 Correct 115 ms 216464 KB Output is correct
38 Correct 163 ms 218292 KB Output is correct
39 Correct 197 ms 250316 KB Output is correct
40 Correct 276 ms 227468 KB Output is correct
41 Correct 206 ms 218236 KB Output is correct
42 Correct 146 ms 218408 KB Output is correct
43 Correct 157 ms 218000 KB Output is correct
44 Correct 124 ms 216936 KB Output is correct
45 Correct 211 ms 242060 KB Output is correct
46 Correct 216 ms 241996 KB Output is correct
47 Correct 664 ms 284768 KB Output is correct
48 Correct 713 ms 285912 KB Output is correct
49 Correct 224 ms 244420 KB Output is correct
50 Correct 228 ms 244504 KB Output is correct
51 Correct 205 ms 242636 KB Output is correct
52 Correct 174 ms 242568 KB Output is correct
53 Correct 129 ms 218316 KB Output is correct
54 Correct 204 ms 240872 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 112 ms 216452 KB Output is correct
2 Correct 116 ms 216336 KB Output is correct
3 Correct 128 ms 216456 KB Output is correct
4 Correct 110 ms 216412 KB Output is correct
5 Correct 118 ms 216360 KB Output is correct
6 Correct 113 ms 216476 KB Output is correct
7 Correct 109 ms 216504 KB Output is correct
8 Correct 112 ms 216372 KB Output is correct
9 Correct 127 ms 216456 KB Output is correct
10 Correct 113 ms 216396 KB Output is correct
11 Correct 112 ms 216476 KB Output is correct
12 Correct 117 ms 216484 KB Output is correct
13 Correct 142 ms 216504 KB Output is correct
14 Correct 162 ms 216524 KB Output is correct
15 Correct 145 ms 216472 KB Output is correct
16 Correct 237 ms 241228 KB Output is correct
17 Correct 357 ms 250948 KB Output is correct
18 Correct 606 ms 263608 KB Output is correct
19 Correct 265 ms 243132 KB Output is correct
20 Correct 281 ms 243596 KB Output is correct
21 Correct 275 ms 243548 KB Output is correct
22 Correct 355 ms 251368 KB Output is correct
23 Correct 324 ms 250892 KB Output is correct
24 Correct 333 ms 251316 KB Output is correct
25 Correct 303 ms 250840 KB Output is correct
26 Correct 344 ms 251120 KB Output is correct
27 Correct 285 ms 243672 KB Output is correct
28 Correct 305 ms 243716 KB Output is correct
29 Correct 287 ms 243752 KB Output is correct
30 Correct 292 ms 244684 KB Output is correct
31 Correct 321 ms 244672 KB Output is correct
32 Correct 290 ms 244572 KB Output is correct
33 Correct 309 ms 244692 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 115 ms 216328 KB Output is correct
2 Correct 109 ms 216552 KB Output is correct
3 Correct 114 ms 216416 KB Output is correct
4 Correct 124 ms 216472 KB Output is correct
5 Correct 151 ms 216488 KB Output is correct
6 Correct 118 ms 216412 KB Output is correct
7 Correct 118 ms 216520 KB Output is correct
8 Correct 112 ms 216536 KB Output is correct
9 Correct 160 ms 218400 KB Output is correct
10 Correct 191 ms 250312 KB Output is correct
11 Correct 120 ms 216444 KB Output is correct
12 Correct 155 ms 216544 KB Output is correct
13 Correct 260 ms 257228 KB Output is correct
14 Correct 248 ms 257452 KB Output is correct
15 Correct 400 ms 262532 KB Output is correct
16 Correct 751 ms 280064 KB Output is correct
17 Correct 316 ms 266356 KB Output is correct
18 Correct 263 ms 258616 KB Output is correct
19 Correct 297 ms 263944 KB Output is correct
20 Correct 283 ms 263992 KB Output is correct
21 Correct 285 ms 265080 KB Output is correct
22 Correct 227 ms 254152 KB Output is correct
23 Correct 109 ms 216452 KB Output is correct
24 Correct 107 ms 216372 KB Output is correct
25 Correct 114 ms 216472 KB Output is correct
26 Correct 111 ms 216440 KB Output is correct
27 Correct 117 ms 216624 KB Output is correct
28 Correct 111 ms 216408 KB Output is correct
29 Correct 134 ms 216456 KB Output is correct
30 Correct 113 ms 216448 KB Output is correct
31 Correct 116 ms 216524 KB Output is correct
32 Correct 111 ms 216516 KB Output is correct
33 Correct 116 ms 216408 KB Output is correct
34 Correct 111 ms 216472 KB Output is correct
35 Correct 113 ms 216464 KB Output is correct
36 Correct 127 ms 216416 KB Output is correct
37 Correct 118 ms 216384 KB Output is correct
38 Correct 113 ms 216524 KB Output is correct
39 Correct 114 ms 216480 KB Output is correct
40 Correct 122 ms 216536 KB Output is correct
41 Correct 117 ms 216456 KB Output is correct
42 Correct 118 ms 216540 KB Output is correct
43 Correct 115 ms 216536 KB Output is correct
44 Correct 117 ms 216464 KB Output is correct
45 Correct 117 ms 216388 KB Output is correct
46 Correct 115 ms 216420 KB Output is correct
47 Correct 111 ms 216496 KB Output is correct
48 Correct 110 ms 216520 KB Output is correct
49 Correct 121 ms 216424 KB Output is correct
50 Correct 112 ms 216432 KB Output is correct
51 Correct 112 ms 216472 KB Output is correct
52 Correct 113 ms 216548 KB Output is correct
53 Correct 123 ms 216596 KB Output is correct
54 Correct 120 ms 216496 KB Output is correct
55 Correct 113 ms 216524 KB Output is correct
56 Correct 115 ms 216428 KB Output is correct
57 Correct 116 ms 216604 KB Output is correct
58 Correct 122 ms 216508 KB Output is correct
59 Correct 115 ms 216464 KB Output is correct
60 Correct 163 ms 218292 KB Output is correct
61 Correct 197 ms 250316 KB Output is correct
62 Correct 276 ms 227468 KB Output is correct
63 Correct 206 ms 218236 KB Output is correct
64 Correct 146 ms 218408 KB Output is correct
65 Correct 157 ms 218000 KB Output is correct
66 Correct 124 ms 216936 KB Output is correct
67 Correct 211 ms 242060 KB Output is correct
68 Correct 216 ms 241996 KB Output is correct
69 Correct 664 ms 284768 KB Output is correct
70 Correct 713 ms 285912 KB Output is correct
71 Correct 224 ms 244420 KB Output is correct
72 Correct 228 ms 244504 KB Output is correct
73 Correct 205 ms 242636 KB Output is correct
74 Correct 174 ms 242568 KB Output is correct
75 Correct 129 ms 218316 KB Output is correct
76 Correct 204 ms 240872 KB Output is correct
77 Correct 112 ms 216452 KB Output is correct
78 Correct 116 ms 216336 KB Output is correct
79 Correct 128 ms 216456 KB Output is correct
80 Correct 110 ms 216412 KB Output is correct
81 Correct 118 ms 216360 KB Output is correct
82 Correct 113 ms 216476 KB Output is correct
83 Correct 109 ms 216504 KB Output is correct
84 Correct 112 ms 216372 KB Output is correct
85 Correct 127 ms 216456 KB Output is correct
86 Correct 113 ms 216396 KB Output is correct
87 Correct 112 ms 216476 KB Output is correct
88 Correct 117 ms 216484 KB Output is correct
89 Correct 142 ms 216504 KB Output is correct
90 Correct 162 ms 216524 KB Output is correct
91 Correct 145 ms 216472 KB Output is correct
92 Correct 237 ms 241228 KB Output is correct
93 Correct 357 ms 250948 KB Output is correct
94 Correct 606 ms 263608 KB Output is correct
95 Correct 265 ms 243132 KB Output is correct
96 Correct 281 ms 243596 KB Output is correct
97 Correct 275 ms 243548 KB Output is correct
98 Correct 355 ms 251368 KB Output is correct
99 Correct 324 ms 250892 KB Output is correct
100 Correct 333 ms 251316 KB Output is correct
101 Correct 303 ms 250840 KB Output is correct
102 Correct 344 ms 251120 KB Output is correct
103 Correct 285 ms 243672 KB Output is correct
104 Correct 305 ms 243716 KB Output is correct
105 Correct 287 ms 243752 KB Output is correct
106 Correct 292 ms 244684 KB Output is correct
107 Correct 321 ms 244672 KB Output is correct
108 Correct 290 ms 244572 KB Output is correct
109 Correct 309 ms 244692 KB Output is correct
110 Correct 156 ms 216608 KB Output is correct
111 Correct 140 ms 216532 KB Output is correct
112 Execution timed out 5093 ms 787844 KB Time limit exceeded
113 Halted 0 ms 0 KB -