Submission #631607

# Submission time Handle Problem Language Result Execution time Memory
631607 2022-08-18T09:56:44 Z LittleCube Jail (JOI22_jail) C++14
77 / 100
5000 ms 737944 KB
#pragma GCC optimize("Ofast,unroll-loops")
#pragma target("avx,avx2,sse,sse2,ssse3,sse4,fma,tune=native")
#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();
	}
}

Compilation message

jail.cpp:2: warning: ignoring '#pragma target ' [-Wunknown-pragmas]
    2 | #pragma target("avx,avx2,sse,sse2,ssse3,sse4,fma,tune=native")
      |
# Verdict Execution time Memory Grader output
1 Correct 85 ms 169552 KB Output is correct
2 Correct 83 ms 169392 KB Output is correct
3 Correct 84 ms 169480 KB Output is correct
4 Correct 98 ms 169512 KB Output is correct
5 Correct 125 ms 169528 KB Output is correct
6 Correct 86 ms 169548 KB Output is correct
7 Correct 86 ms 169560 KB Output is correct
8 Correct 87 ms 169596 KB Output is correct
9 Correct 134 ms 171340 KB Output is correct
10 Correct 165 ms 203400 KB Output is correct
11 Correct 94 ms 169428 KB Output is correct
12 Correct 133 ms 169468 KB Output is correct
13 Correct 247 ms 210212 KB Output is correct
14 Correct 211 ms 210444 KB Output is correct
15 Correct 375 ms 215500 KB Output is correct
16 Correct 679 ms 233012 KB Output is correct
17 Correct 313 ms 219416 KB Output is correct
18 Correct 224 ms 211596 KB Output is correct
19 Correct 302 ms 216916 KB Output is correct
20 Correct 254 ms 216900 KB Output is correct
21 Correct 262 ms 218052 KB Output is correct
22 Correct 212 ms 207212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 87 ms 169480 KB Output is correct
2 Correct 87 ms 169492 KB Output is correct
3 Correct 88 ms 169676 KB Output is correct
4 Correct 89 ms 169472 KB Output is correct
5 Correct 89 ms 169540 KB Output is correct
6 Correct 93 ms 169508 KB Output is correct
7 Correct 90 ms 169476 KB Output is correct
8 Correct 87 ms 169492 KB Output is correct
9 Correct 89 ms 169564 KB Output is correct
10 Correct 96 ms 169564 KB Output is correct
11 Correct 96 ms 169548 KB Output is correct
12 Correct 87 ms 169496 KB Output is correct
13 Correct 87 ms 169548 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 87 ms 169480 KB Output is correct
2 Correct 87 ms 169492 KB Output is correct
3 Correct 88 ms 169676 KB Output is correct
4 Correct 89 ms 169472 KB Output is correct
5 Correct 89 ms 169540 KB Output is correct
6 Correct 93 ms 169508 KB Output is correct
7 Correct 90 ms 169476 KB Output is correct
8 Correct 87 ms 169492 KB Output is correct
9 Correct 89 ms 169564 KB Output is correct
10 Correct 96 ms 169564 KB Output is correct
11 Correct 96 ms 169548 KB Output is correct
12 Correct 87 ms 169496 KB Output is correct
13 Correct 87 ms 169548 KB Output is correct
14 Correct 87 ms 169432 KB Output is correct
15 Correct 99 ms 169492 KB Output is correct
16 Correct 87 ms 169576 KB Output is correct
17 Correct 87 ms 169540 KB Output is correct
18 Correct 85 ms 169544 KB Output is correct
19 Correct 87 ms 169496 KB Output is correct
20 Correct 90 ms 169464 KB Output is correct
21 Correct 90 ms 169556 KB Output is correct
22 Correct 86 ms 169508 KB Output is correct
23 Correct 85 ms 169480 KB Output is correct
24 Correct 86 ms 169420 KB Output is correct
25 Correct 95 ms 169504 KB Output is correct
26 Correct 91 ms 169556 KB Output is correct
27 Correct 90 ms 169564 KB Output is correct
28 Correct 85 ms 169504 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 87 ms 169480 KB Output is correct
2 Correct 87 ms 169492 KB Output is correct
3 Correct 88 ms 169676 KB Output is correct
4 Correct 89 ms 169472 KB Output is correct
5 Correct 89 ms 169540 KB Output is correct
6 Correct 93 ms 169508 KB Output is correct
7 Correct 90 ms 169476 KB Output is correct
8 Correct 87 ms 169492 KB Output is correct
9 Correct 89 ms 169564 KB Output is correct
10 Correct 96 ms 169564 KB Output is correct
11 Correct 96 ms 169548 KB Output is correct
12 Correct 87 ms 169496 KB Output is correct
13 Correct 87 ms 169548 KB Output is correct
14 Correct 87 ms 169432 KB Output is correct
15 Correct 99 ms 169492 KB Output is correct
16 Correct 87 ms 169576 KB Output is correct
17 Correct 87 ms 169540 KB Output is correct
18 Correct 85 ms 169544 KB Output is correct
19 Correct 87 ms 169496 KB Output is correct
20 Correct 90 ms 169464 KB Output is correct
21 Correct 90 ms 169556 KB Output is correct
22 Correct 86 ms 169508 KB Output is correct
23 Correct 85 ms 169480 KB Output is correct
24 Correct 86 ms 169420 KB Output is correct
25 Correct 95 ms 169504 KB Output is correct
26 Correct 91 ms 169556 KB Output is correct
27 Correct 90 ms 169564 KB Output is correct
28 Correct 85 ms 169504 KB Output is correct
29 Correct 93 ms 169560 KB Output is correct
30 Correct 87 ms 169572 KB Output is correct
31 Correct 111 ms 169664 KB Output is correct
32 Correct 89 ms 169524 KB Output is correct
33 Correct 89 ms 169568 KB Output is correct
34 Correct 92 ms 169584 KB Output is correct
35 Correct 90 ms 169620 KB Output is correct
36 Correct 93 ms 169644 KB Output is correct
37 Correct 89 ms 169584 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 87 ms 169480 KB Output is correct
2 Correct 87 ms 169492 KB Output is correct
3 Correct 88 ms 169676 KB Output is correct
4 Correct 89 ms 169472 KB Output is correct
5 Correct 89 ms 169540 KB Output is correct
6 Correct 93 ms 169508 KB Output is correct
7 Correct 90 ms 169476 KB Output is correct
8 Correct 87 ms 169492 KB Output is correct
9 Correct 89 ms 169564 KB Output is correct
10 Correct 96 ms 169564 KB Output is correct
11 Correct 96 ms 169548 KB Output is correct
12 Correct 87 ms 169496 KB Output is correct
13 Correct 87 ms 169548 KB Output is correct
14 Correct 87 ms 169432 KB Output is correct
15 Correct 99 ms 169492 KB Output is correct
16 Correct 87 ms 169576 KB Output is correct
17 Correct 87 ms 169540 KB Output is correct
18 Correct 85 ms 169544 KB Output is correct
19 Correct 87 ms 169496 KB Output is correct
20 Correct 90 ms 169464 KB Output is correct
21 Correct 90 ms 169556 KB Output is correct
22 Correct 86 ms 169508 KB Output is correct
23 Correct 85 ms 169480 KB Output is correct
24 Correct 86 ms 169420 KB Output is correct
25 Correct 95 ms 169504 KB Output is correct
26 Correct 91 ms 169556 KB Output is correct
27 Correct 90 ms 169564 KB Output is correct
28 Correct 85 ms 169504 KB Output is correct
29 Correct 93 ms 169560 KB Output is correct
30 Correct 87 ms 169572 KB Output is correct
31 Correct 111 ms 169664 KB Output is correct
32 Correct 89 ms 169524 KB Output is correct
33 Correct 89 ms 169568 KB Output is correct
34 Correct 92 ms 169584 KB Output is correct
35 Correct 90 ms 169620 KB Output is correct
36 Correct 93 ms 169644 KB Output is correct
37 Correct 89 ms 169584 KB Output is correct
38 Correct 133 ms 171428 KB Output is correct
39 Correct 166 ms 203388 KB Output is correct
40 Correct 232 ms 180304 KB Output is correct
41 Correct 134 ms 171156 KB Output is correct
42 Correct 123 ms 171464 KB Output is correct
43 Correct 121 ms 171084 KB Output is correct
44 Correct 102 ms 169872 KB Output is correct
45 Correct 181 ms 195104 KB Output is correct
46 Correct 179 ms 195072 KB Output is correct
47 Correct 655 ms 237876 KB Output is correct
48 Correct 684 ms 238936 KB Output is correct
49 Correct 204 ms 197412 KB Output is correct
50 Correct 216 ms 197480 KB Output is correct
51 Correct 167 ms 195560 KB Output is correct
52 Correct 248 ms 195620 KB Output is correct
53 Correct 107 ms 171324 KB Output is correct
54 Correct 204 ms 193876 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 85 ms 169420 KB Output is correct
2 Correct 87 ms 169376 KB Output is correct
3 Correct 90 ms 169392 KB Output is correct
4 Correct 89 ms 169488 KB Output is correct
5 Correct 98 ms 169552 KB Output is correct
6 Correct 86 ms 169528 KB Output is correct
7 Correct 86 ms 169544 KB Output is correct
8 Correct 86 ms 169424 KB Output is correct
9 Correct 87 ms 169508 KB Output is correct
10 Correct 87 ms 169496 KB Output is correct
11 Correct 87 ms 169420 KB Output is correct
12 Correct 110 ms 169628 KB Output is correct
13 Correct 115 ms 169580 KB Output is correct
14 Correct 129 ms 169520 KB Output is correct
15 Correct 121 ms 169532 KB Output is correct
16 Correct 253 ms 194340 KB Output is correct
17 Correct 320 ms 204120 KB Output is correct
18 Correct 538 ms 216656 KB Output is correct
19 Correct 241 ms 196300 KB Output is correct
20 Correct 226 ms 196616 KB Output is correct
21 Correct 287 ms 196552 KB Output is correct
22 Correct 318 ms 204380 KB Output is correct
23 Correct 267 ms 204032 KB Output is correct
24 Correct 312 ms 204368 KB Output is correct
25 Correct 291 ms 203936 KB Output is correct
26 Correct 299 ms 204076 KB Output is correct
27 Correct 272 ms 196700 KB Output is correct
28 Correct 237 ms 196740 KB Output is correct
29 Correct 241 ms 196792 KB Output is correct
30 Correct 262 ms 197780 KB Output is correct
31 Correct 253 ms 197672 KB Output is correct
32 Correct 277 ms 197700 KB Output is correct
33 Correct 243 ms 197680 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 85 ms 169552 KB Output is correct
2 Correct 83 ms 169392 KB Output is correct
3 Correct 84 ms 169480 KB Output is correct
4 Correct 98 ms 169512 KB Output is correct
5 Correct 125 ms 169528 KB Output is correct
6 Correct 86 ms 169548 KB Output is correct
7 Correct 86 ms 169560 KB Output is correct
8 Correct 87 ms 169596 KB Output is correct
9 Correct 134 ms 171340 KB Output is correct
10 Correct 165 ms 203400 KB Output is correct
11 Correct 94 ms 169428 KB Output is correct
12 Correct 133 ms 169468 KB Output is correct
13 Correct 247 ms 210212 KB Output is correct
14 Correct 211 ms 210444 KB Output is correct
15 Correct 375 ms 215500 KB Output is correct
16 Correct 679 ms 233012 KB Output is correct
17 Correct 313 ms 219416 KB Output is correct
18 Correct 224 ms 211596 KB Output is correct
19 Correct 302 ms 216916 KB Output is correct
20 Correct 254 ms 216900 KB Output is correct
21 Correct 262 ms 218052 KB Output is correct
22 Correct 212 ms 207212 KB Output is correct
23 Correct 87 ms 169480 KB Output is correct
24 Correct 87 ms 169492 KB Output is correct
25 Correct 88 ms 169676 KB Output is correct
26 Correct 89 ms 169472 KB Output is correct
27 Correct 89 ms 169540 KB Output is correct
28 Correct 93 ms 169508 KB Output is correct
29 Correct 90 ms 169476 KB Output is correct
30 Correct 87 ms 169492 KB Output is correct
31 Correct 89 ms 169564 KB Output is correct
32 Correct 96 ms 169564 KB Output is correct
33 Correct 96 ms 169548 KB Output is correct
34 Correct 87 ms 169496 KB Output is correct
35 Correct 87 ms 169548 KB Output is correct
36 Correct 87 ms 169432 KB Output is correct
37 Correct 99 ms 169492 KB Output is correct
38 Correct 87 ms 169576 KB Output is correct
39 Correct 87 ms 169540 KB Output is correct
40 Correct 85 ms 169544 KB Output is correct
41 Correct 87 ms 169496 KB Output is correct
42 Correct 90 ms 169464 KB Output is correct
43 Correct 90 ms 169556 KB Output is correct
44 Correct 86 ms 169508 KB Output is correct
45 Correct 85 ms 169480 KB Output is correct
46 Correct 86 ms 169420 KB Output is correct
47 Correct 95 ms 169504 KB Output is correct
48 Correct 91 ms 169556 KB Output is correct
49 Correct 90 ms 169564 KB Output is correct
50 Correct 85 ms 169504 KB Output is correct
51 Correct 93 ms 169560 KB Output is correct
52 Correct 87 ms 169572 KB Output is correct
53 Correct 111 ms 169664 KB Output is correct
54 Correct 89 ms 169524 KB Output is correct
55 Correct 89 ms 169568 KB Output is correct
56 Correct 92 ms 169584 KB Output is correct
57 Correct 90 ms 169620 KB Output is correct
58 Correct 93 ms 169644 KB Output is correct
59 Correct 89 ms 169584 KB Output is correct
60 Correct 133 ms 171428 KB Output is correct
61 Correct 166 ms 203388 KB Output is correct
62 Correct 232 ms 180304 KB Output is correct
63 Correct 134 ms 171156 KB Output is correct
64 Correct 123 ms 171464 KB Output is correct
65 Correct 121 ms 171084 KB Output is correct
66 Correct 102 ms 169872 KB Output is correct
67 Correct 181 ms 195104 KB Output is correct
68 Correct 179 ms 195072 KB Output is correct
69 Correct 655 ms 237876 KB Output is correct
70 Correct 684 ms 238936 KB Output is correct
71 Correct 204 ms 197412 KB Output is correct
72 Correct 216 ms 197480 KB Output is correct
73 Correct 167 ms 195560 KB Output is correct
74 Correct 248 ms 195620 KB Output is correct
75 Correct 107 ms 171324 KB Output is correct
76 Correct 204 ms 193876 KB Output is correct
77 Correct 85 ms 169420 KB Output is correct
78 Correct 87 ms 169376 KB Output is correct
79 Correct 90 ms 169392 KB Output is correct
80 Correct 89 ms 169488 KB Output is correct
81 Correct 98 ms 169552 KB Output is correct
82 Correct 86 ms 169528 KB Output is correct
83 Correct 86 ms 169544 KB Output is correct
84 Correct 86 ms 169424 KB Output is correct
85 Correct 87 ms 169508 KB Output is correct
86 Correct 87 ms 169496 KB Output is correct
87 Correct 87 ms 169420 KB Output is correct
88 Correct 110 ms 169628 KB Output is correct
89 Correct 115 ms 169580 KB Output is correct
90 Correct 129 ms 169520 KB Output is correct
91 Correct 121 ms 169532 KB Output is correct
92 Correct 253 ms 194340 KB Output is correct
93 Correct 320 ms 204120 KB Output is correct
94 Correct 538 ms 216656 KB Output is correct
95 Correct 241 ms 196300 KB Output is correct
96 Correct 226 ms 196616 KB Output is correct
97 Correct 287 ms 196552 KB Output is correct
98 Correct 318 ms 204380 KB Output is correct
99 Correct 267 ms 204032 KB Output is correct
100 Correct 312 ms 204368 KB Output is correct
101 Correct 291 ms 203936 KB Output is correct
102 Correct 299 ms 204076 KB Output is correct
103 Correct 272 ms 196700 KB Output is correct
104 Correct 237 ms 196740 KB Output is correct
105 Correct 241 ms 196792 KB Output is correct
106 Correct 262 ms 197780 KB Output is correct
107 Correct 253 ms 197672 KB Output is correct
108 Correct 277 ms 197700 KB Output is correct
109 Correct 243 ms 197680 KB Output is correct
110 Correct 129 ms 169612 KB Output is correct
111 Correct 116 ms 169536 KB Output is correct
112 Execution timed out 5076 ms 737944 KB Time limit exceeded
113 Halted 0 ms 0 KB -