답안 #631534

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
631534 2022-08-18T08:15:07 Z LittleCube Jail (JOI22_jail) C++14
77 / 100
5000 ms 767956 KB
#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] = {};
	
	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);
			}
	}

	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]);
		}
	}

	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);
		}
	}

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

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;
}

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));
		}
}

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]);
}

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

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], 1, Hsz[Hhead[u]], i, 1, Hpos[u], 0);
			hld.segEdge(hld.root[Hhead[u]][1], 1, Hsz[Hhead[u]], i, 1, Hpos[u], 1);
			u = pre[Hhead[u]];
		}
		
		if(dis[u] > dis[v]) swap(u, v);

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


	}

	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 104 ms 216424 KB Output is correct
2 Correct 109 ms 216356 KB Output is correct
3 Correct 114 ms 216356 KB Output is correct
4 Correct 127 ms 216388 KB Output is correct
5 Correct 151 ms 216500 KB Output is correct
6 Correct 112 ms 216524 KB Output is correct
7 Correct 122 ms 216504 KB Output is correct
8 Correct 131 ms 216524 KB Output is correct
9 Correct 163 ms 218572 KB Output is correct
10 Correct 194 ms 255172 KB Output is correct
11 Correct 129 ms 216404 KB Output is correct
12 Correct 173 ms 216552 KB Output is correct
13 Correct 276 ms 262076 KB Output is correct
14 Correct 300 ms 262280 KB Output is correct
15 Correct 463 ms 267392 KB Output is correct
16 Correct 937 ms 284760 KB Output is correct
17 Correct 355 ms 271268 KB Output is correct
18 Correct 247 ms 263376 KB Output is correct
19 Correct 338 ms 268668 KB Output is correct
20 Correct 335 ms 268668 KB Output is correct
21 Correct 332 ms 269880 KB Output is correct
22 Correct 238 ms 258956 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 107 ms 216416 KB Output is correct
2 Correct 108 ms 216392 KB Output is correct
3 Correct 136 ms 216532 KB Output is correct
4 Correct 133 ms 216580 KB Output is correct
5 Correct 105 ms 216516 KB Output is correct
6 Correct 119 ms 216472 KB Output is correct
7 Correct 110 ms 216524 KB Output is correct
8 Correct 122 ms 216524 KB Output is correct
9 Correct 117 ms 216504 KB Output is correct
10 Correct 126 ms 216512 KB Output is correct
11 Correct 103 ms 216508 KB Output is correct
12 Correct 109 ms 216504 KB Output is correct
13 Correct 128 ms 216456 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 107 ms 216416 KB Output is correct
2 Correct 108 ms 216392 KB Output is correct
3 Correct 136 ms 216532 KB Output is correct
4 Correct 133 ms 216580 KB Output is correct
5 Correct 105 ms 216516 KB Output is correct
6 Correct 119 ms 216472 KB Output is correct
7 Correct 110 ms 216524 KB Output is correct
8 Correct 122 ms 216524 KB Output is correct
9 Correct 117 ms 216504 KB Output is correct
10 Correct 126 ms 216512 KB Output is correct
11 Correct 103 ms 216508 KB Output is correct
12 Correct 109 ms 216504 KB Output is correct
13 Correct 128 ms 216456 KB Output is correct
14 Correct 125 ms 216432 KB Output is correct
15 Correct 125 ms 216452 KB Output is correct
16 Correct 132 ms 216540 KB Output is correct
17 Correct 133 ms 216516 KB Output is correct
18 Correct 117 ms 216536 KB Output is correct
19 Correct 120 ms 216464 KB Output is correct
20 Correct 107 ms 216492 KB Output is correct
21 Correct 127 ms 216452 KB Output is correct
22 Correct 123 ms 216484 KB Output is correct
23 Correct 104 ms 216432 KB Output is correct
24 Correct 108 ms 216424 KB Output is correct
25 Correct 107 ms 216480 KB Output is correct
26 Correct 109 ms 216460 KB Output is correct
27 Correct 121 ms 216460 KB Output is correct
28 Correct 114 ms 216420 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 107 ms 216416 KB Output is correct
2 Correct 108 ms 216392 KB Output is correct
3 Correct 136 ms 216532 KB Output is correct
4 Correct 133 ms 216580 KB Output is correct
5 Correct 105 ms 216516 KB Output is correct
6 Correct 119 ms 216472 KB Output is correct
7 Correct 110 ms 216524 KB Output is correct
8 Correct 122 ms 216524 KB Output is correct
9 Correct 117 ms 216504 KB Output is correct
10 Correct 126 ms 216512 KB Output is correct
11 Correct 103 ms 216508 KB Output is correct
12 Correct 109 ms 216504 KB Output is correct
13 Correct 128 ms 216456 KB Output is correct
14 Correct 125 ms 216432 KB Output is correct
15 Correct 125 ms 216452 KB Output is correct
16 Correct 132 ms 216540 KB Output is correct
17 Correct 133 ms 216516 KB Output is correct
18 Correct 117 ms 216536 KB Output is correct
19 Correct 120 ms 216464 KB Output is correct
20 Correct 107 ms 216492 KB Output is correct
21 Correct 127 ms 216452 KB Output is correct
22 Correct 123 ms 216484 KB Output is correct
23 Correct 104 ms 216432 KB Output is correct
24 Correct 108 ms 216424 KB Output is correct
25 Correct 107 ms 216480 KB Output is correct
26 Correct 109 ms 216460 KB Output is correct
27 Correct 121 ms 216460 KB Output is correct
28 Correct 114 ms 216420 KB Output is correct
29 Correct 129 ms 216544 KB Output is correct
30 Correct 133 ms 216428 KB Output is correct
31 Correct 130 ms 216632 KB Output is correct
32 Correct 113 ms 216592 KB Output is correct
33 Correct 130 ms 216496 KB Output is correct
34 Correct 112 ms 216544 KB Output is correct
35 Correct 105 ms 216576 KB Output is correct
36 Correct 104 ms 216536 KB Output is correct
37 Correct 106 ms 216540 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 107 ms 216416 KB Output is correct
2 Correct 108 ms 216392 KB Output is correct
3 Correct 136 ms 216532 KB Output is correct
4 Correct 133 ms 216580 KB Output is correct
5 Correct 105 ms 216516 KB Output is correct
6 Correct 119 ms 216472 KB Output is correct
7 Correct 110 ms 216524 KB Output is correct
8 Correct 122 ms 216524 KB Output is correct
9 Correct 117 ms 216504 KB Output is correct
10 Correct 126 ms 216512 KB Output is correct
11 Correct 103 ms 216508 KB Output is correct
12 Correct 109 ms 216504 KB Output is correct
13 Correct 128 ms 216456 KB Output is correct
14 Correct 125 ms 216432 KB Output is correct
15 Correct 125 ms 216452 KB Output is correct
16 Correct 132 ms 216540 KB Output is correct
17 Correct 133 ms 216516 KB Output is correct
18 Correct 117 ms 216536 KB Output is correct
19 Correct 120 ms 216464 KB Output is correct
20 Correct 107 ms 216492 KB Output is correct
21 Correct 127 ms 216452 KB Output is correct
22 Correct 123 ms 216484 KB Output is correct
23 Correct 104 ms 216432 KB Output is correct
24 Correct 108 ms 216424 KB Output is correct
25 Correct 107 ms 216480 KB Output is correct
26 Correct 109 ms 216460 KB Output is correct
27 Correct 121 ms 216460 KB Output is correct
28 Correct 114 ms 216420 KB Output is correct
29 Correct 129 ms 216544 KB Output is correct
30 Correct 133 ms 216428 KB Output is correct
31 Correct 130 ms 216632 KB Output is correct
32 Correct 113 ms 216592 KB Output is correct
33 Correct 130 ms 216496 KB Output is correct
34 Correct 112 ms 216544 KB Output is correct
35 Correct 105 ms 216576 KB Output is correct
36 Correct 104 ms 216536 KB Output is correct
37 Correct 106 ms 216540 KB Output is correct
38 Correct 193 ms 218516 KB Output is correct
39 Correct 214 ms 255108 KB Output is correct
40 Correct 245 ms 227480 KB Output is correct
41 Correct 173 ms 218196 KB Output is correct
42 Correct 147 ms 218552 KB Output is correct
43 Correct 158 ms 218204 KB Output is correct
44 Correct 128 ms 216944 KB Output is correct
45 Correct 184 ms 242076 KB Output is correct
46 Correct 259 ms 241964 KB Output is correct
47 Correct 652 ms 287220 KB Output is correct
48 Correct 678 ms 288204 KB Output is correct
49 Correct 210 ms 244440 KB Output is correct
50 Correct 215 ms 244584 KB Output is correct
51 Correct 166 ms 243404 KB Output is correct
52 Correct 178 ms 243384 KB Output is correct
53 Correct 116 ms 218260 KB Output is correct
54 Correct 217 ms 240720 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 102 ms 216388 KB Output is correct
2 Correct 105 ms 216472 KB Output is correct
3 Correct 105 ms 216364 KB Output is correct
4 Correct 103 ms 216420 KB Output is correct
5 Correct 112 ms 216464 KB Output is correct
6 Correct 100 ms 216396 KB Output is correct
7 Correct 99 ms 216504 KB Output is correct
8 Correct 118 ms 216460 KB Output is correct
9 Correct 109 ms 216344 KB Output is correct
10 Correct 115 ms 216552 KB Output is correct
11 Correct 99 ms 216472 KB Output is correct
12 Correct 101 ms 216464 KB Output is correct
13 Correct 130 ms 216524 KB Output is correct
14 Correct 143 ms 216532 KB Output is correct
15 Correct 130 ms 216428 KB Output is correct
16 Correct 232 ms 241228 KB Output is correct
17 Correct 376 ms 251072 KB Output is correct
18 Correct 569 ms 263488 KB Output is correct
19 Correct 278 ms 243312 KB Output is correct
20 Correct 283 ms 243608 KB Output is correct
21 Correct 293 ms 243608 KB Output is correct
22 Correct 352 ms 251372 KB Output is correct
23 Correct 317 ms 250956 KB Output is correct
24 Correct 343 ms 251576 KB Output is correct
25 Correct 303 ms 250896 KB Output is correct
26 Correct 322 ms 251192 KB Output is correct
27 Correct 283 ms 243724 KB Output is correct
28 Correct 271 ms 243688 KB Output is correct
29 Correct 263 ms 243688 KB Output is correct
30 Correct 277 ms 244684 KB Output is correct
31 Correct 261 ms 244632 KB Output is correct
32 Correct 306 ms 244592 KB Output is correct
33 Correct 272 ms 244692 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 104 ms 216424 KB Output is correct
2 Correct 109 ms 216356 KB Output is correct
3 Correct 114 ms 216356 KB Output is correct
4 Correct 127 ms 216388 KB Output is correct
5 Correct 151 ms 216500 KB Output is correct
6 Correct 112 ms 216524 KB Output is correct
7 Correct 122 ms 216504 KB Output is correct
8 Correct 131 ms 216524 KB Output is correct
9 Correct 163 ms 218572 KB Output is correct
10 Correct 194 ms 255172 KB Output is correct
11 Correct 129 ms 216404 KB Output is correct
12 Correct 173 ms 216552 KB Output is correct
13 Correct 276 ms 262076 KB Output is correct
14 Correct 300 ms 262280 KB Output is correct
15 Correct 463 ms 267392 KB Output is correct
16 Correct 937 ms 284760 KB Output is correct
17 Correct 355 ms 271268 KB Output is correct
18 Correct 247 ms 263376 KB Output is correct
19 Correct 338 ms 268668 KB Output is correct
20 Correct 335 ms 268668 KB Output is correct
21 Correct 332 ms 269880 KB Output is correct
22 Correct 238 ms 258956 KB Output is correct
23 Correct 107 ms 216416 KB Output is correct
24 Correct 108 ms 216392 KB Output is correct
25 Correct 136 ms 216532 KB Output is correct
26 Correct 133 ms 216580 KB Output is correct
27 Correct 105 ms 216516 KB Output is correct
28 Correct 119 ms 216472 KB Output is correct
29 Correct 110 ms 216524 KB Output is correct
30 Correct 122 ms 216524 KB Output is correct
31 Correct 117 ms 216504 KB Output is correct
32 Correct 126 ms 216512 KB Output is correct
33 Correct 103 ms 216508 KB Output is correct
34 Correct 109 ms 216504 KB Output is correct
35 Correct 128 ms 216456 KB Output is correct
36 Correct 125 ms 216432 KB Output is correct
37 Correct 125 ms 216452 KB Output is correct
38 Correct 132 ms 216540 KB Output is correct
39 Correct 133 ms 216516 KB Output is correct
40 Correct 117 ms 216536 KB Output is correct
41 Correct 120 ms 216464 KB Output is correct
42 Correct 107 ms 216492 KB Output is correct
43 Correct 127 ms 216452 KB Output is correct
44 Correct 123 ms 216484 KB Output is correct
45 Correct 104 ms 216432 KB Output is correct
46 Correct 108 ms 216424 KB Output is correct
47 Correct 107 ms 216480 KB Output is correct
48 Correct 109 ms 216460 KB Output is correct
49 Correct 121 ms 216460 KB Output is correct
50 Correct 114 ms 216420 KB Output is correct
51 Correct 129 ms 216544 KB Output is correct
52 Correct 133 ms 216428 KB Output is correct
53 Correct 130 ms 216632 KB Output is correct
54 Correct 113 ms 216592 KB Output is correct
55 Correct 130 ms 216496 KB Output is correct
56 Correct 112 ms 216544 KB Output is correct
57 Correct 105 ms 216576 KB Output is correct
58 Correct 104 ms 216536 KB Output is correct
59 Correct 106 ms 216540 KB Output is correct
60 Correct 193 ms 218516 KB Output is correct
61 Correct 214 ms 255108 KB Output is correct
62 Correct 245 ms 227480 KB Output is correct
63 Correct 173 ms 218196 KB Output is correct
64 Correct 147 ms 218552 KB Output is correct
65 Correct 158 ms 218204 KB Output is correct
66 Correct 128 ms 216944 KB Output is correct
67 Correct 184 ms 242076 KB Output is correct
68 Correct 259 ms 241964 KB Output is correct
69 Correct 652 ms 287220 KB Output is correct
70 Correct 678 ms 288204 KB Output is correct
71 Correct 210 ms 244440 KB Output is correct
72 Correct 215 ms 244584 KB Output is correct
73 Correct 166 ms 243404 KB Output is correct
74 Correct 178 ms 243384 KB Output is correct
75 Correct 116 ms 218260 KB Output is correct
76 Correct 217 ms 240720 KB Output is correct
77 Correct 102 ms 216388 KB Output is correct
78 Correct 105 ms 216472 KB Output is correct
79 Correct 105 ms 216364 KB Output is correct
80 Correct 103 ms 216420 KB Output is correct
81 Correct 112 ms 216464 KB Output is correct
82 Correct 100 ms 216396 KB Output is correct
83 Correct 99 ms 216504 KB Output is correct
84 Correct 118 ms 216460 KB Output is correct
85 Correct 109 ms 216344 KB Output is correct
86 Correct 115 ms 216552 KB Output is correct
87 Correct 99 ms 216472 KB Output is correct
88 Correct 101 ms 216464 KB Output is correct
89 Correct 130 ms 216524 KB Output is correct
90 Correct 143 ms 216532 KB Output is correct
91 Correct 130 ms 216428 KB Output is correct
92 Correct 232 ms 241228 KB Output is correct
93 Correct 376 ms 251072 KB Output is correct
94 Correct 569 ms 263488 KB Output is correct
95 Correct 278 ms 243312 KB Output is correct
96 Correct 283 ms 243608 KB Output is correct
97 Correct 293 ms 243608 KB Output is correct
98 Correct 352 ms 251372 KB Output is correct
99 Correct 317 ms 250956 KB Output is correct
100 Correct 343 ms 251576 KB Output is correct
101 Correct 303 ms 250896 KB Output is correct
102 Correct 322 ms 251192 KB Output is correct
103 Correct 283 ms 243724 KB Output is correct
104 Correct 271 ms 243688 KB Output is correct
105 Correct 263 ms 243688 KB Output is correct
106 Correct 277 ms 244684 KB Output is correct
107 Correct 261 ms 244632 KB Output is correct
108 Correct 306 ms 244592 KB Output is correct
109 Correct 272 ms 244692 KB Output is correct
110 Correct 148 ms 216684 KB Output is correct
111 Correct 152 ms 216612 KB Output is correct
112 Execution timed out 5085 ms 767956 KB Time limit exceeded
113 Halted 0 ms 0 KB -