답안 #544497

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
544497 2022-04-02T07:02:53 Z model_code Jail (JOI22_jail) C++17
100 / 100
1710 ms 446332 KB
#include <bits/stdc++.h>
using namespace std;

// Input & Graph
int N, A[200009], B[200009];
int M, S[200009], T[200009];
int SVal[200009], TVal[200009];
int Sidx[200009][20];
int Tidx[200009][20];
vector<int> G[200009];
vector<int> H[9000009];

// Others
int NumVertices;
int parent[200009][20], dist[200009];
int indeg[9000009];
vector<int> tmp, Path;

void AllInit(int N, int M, int Verts) {
	for (int i = 0; i < N; i++) G[i].clear();
	for (int i = 0; i < Verts; i++) H[i].clear();
	NumVertices = 0;
}

void dfs_init(int pos, int par, int dep) {
	dist[pos] = dep;
	for (int to : G[pos]) {
		if (to == par) continue;
		parent[to][0] = pos;
		dfs_init(to, pos, dep + 1);
	}
}

int prevs(int pos, int x) {
	for (int i = 19; i >= 0; i--) {
		if (x >= (1 << i)) { pos = parent[pos][i]; x -= (1 << i); }
	}
	return pos;
}

int GetLCA(int u, int v) {
	if (dist[u] > dist[v]) swap(u, v);
	v = prevs(v, dist[v] - dist[u]);
	if (u == v) return u;
	for (int i = 19; i >= 0; i--) {
		if (parent[u][i] == parent[v][i]) continue;
		u = parent[u][i];
		v = parent[v][i];
	}
	return parent[u][0];
}

bool TopologicalSort() {
	for (int i = 0; i < NumVertices; i++) indeg[i] = 0;
	for (int i = 0; i < NumVertices; i++) {
		for (int j : H[i]) indeg[j] += 1;
	}
	
	queue<int> Q;
	for (int i = 0; i < NumVertices; i++) {
		if (indeg[i] == 0) Q.push(i);
	}
	while (!Q.empty()) {
		int pos = Q.front(); Q.pop();
		for (int to : H[pos]) {
			indeg[to] -= 1;
			if (indeg[to] == 0) Q.push(to);
		}
	}
	
	for (int i = 0; i < NumVertices; i++) {
		if (indeg[i] != 0) return false;
	}
	return true;
}

void AddS(int pos, int x, int base) {
	for (int i = 19; i >= 0; i--) {
		if (x < (1 << i)) continue;
		H[Sidx[pos][i]].push_back(base);
		x -= (1 << i);
		pos = parent[pos][i];
	}
}

void AddT(int pos, int x, int base) {
	for (int i = 19; i >= 0; i--) {
		if (x < (1 << i)) continue;
		H[base].push_back(Tidx[pos][i]);
		x -= (1 << i);
		pos = parent[pos][i];
	}
}

bool GetAnswer(int NN, vector<int> AA, vector<int> BB, int MM, vector<int> SS, vector<int> TT) {
	AllInit(N, M, NumVertices);
	N = NN;
	for (int i = 0; i < N - 1; i++) A[i] = AA[i] - 1;
	for (int i = 0; i < N - 1; i++) B[i] = BB[i] - 1;
	M = MM;
	for (int i = 0; i < M; i++) S[i] = SS[i] - 1;
	for (int i = 0; i < M; i++) T[i] = TT[i] - 1;
	
	// Step #1. Obvious Case
	for (int i = 0; i < N; i++) SVal[i] = -1;
	for (int i = 0; i < N; i++) TVal[i] = -1;
	for (int i = 0; i < M; i++) {
		if (SVal[S[i]] != -1) return false;
		else SVal[S[i]] = i;
		if (TVal[T[i]] != -1) return false;
		else TVal[T[i]] = i;
	}
	
	// Step #2. Make Graph
	for (int i = 0; i < N - 1; i++) {
		G[A[i]].push_back(B[i]);
		G[B[i]].push_back(A[i]);
	}
	
	// Step #3. Get LCA
	dfs_init(0, -1, 0);
	parent[0][0] = -1;
	for (int i = 0; i < 19; i++) {
		for (int j = 0; j < N; j++) {
			if (parent[j][i] == -1) parent[j][i + 1] = -1;
			else parent[j][i + 1] = parent[parent[j][i]][i];
		}
	}
	
	// Step #4. Init Sparse Table
	NumVertices = M;
	for (int i = 0; i < 20; i++) {
		for (int j = 0; j < N; j++) { NumVertices += 1; Sidx[j][i] = NumVertices; }
	}
	for (int i = 0; i < 20; i++) {
		for (int j = 0; j < N; j++) { NumVertices += 1; Tidx[j][i] = NumVertices; }
	}
	for (int i = 0; i < 19; i++) {
		for (int j = 0; j < N; j++) {
			int idx1 = Sidx[j][i + 1];
			int idx2 = Tidx[j][i + 1];
			int pars = parent[j][i];
			H[Sidx[j][i]].push_back(idx1);
			H[idx2].push_back(Tidx[j][i]);
			if (pars != -1) {
				H[Sidx[pars][i]].push_back(idx1);
				H[idx2].push_back(Tidx[pars][i]);
			}
		}
	}
	for (int i = 0; i < N; i++) {
		int idx1 = Sidx[i][0];
		int idx2 = Tidx[i][0];
		if (SVal[i] != -1) H[SVal[i]].push_back(idx1);
		if (TVal[i] != -1) H[idx2].push_back(TVal[i]);
	}
	
	// Step #5. Add Edges
	for (int i = 0; i < M; i++) {
		int LCA = GetLCA(S[i], T[i]);
		int LengthS = dist[S[i]] - dist[LCA];
		int LengthT = dist[T[i]] - dist[LCA];
		if (LengthS >= 2) {
			AddS(parent[S[i]][0], LengthS - 1, i);
			AddT(parent[S[i]][0], LengthS - 1, i);
		}
		if (LengthT >= 2) {
			AddS(parent[T[i]][0], LengthT - 1, i);
			AddT(parent[T[i]][0], LengthT - 1, i);
		}
		vector<int> Cand = {S[i], T[i], LCA};
		for (int j : Cand) {
			if (SVal[j] != i && SVal[j] != -1) H[SVal[j]].push_back(i);
			if (TVal[j] != i && TVal[j] != -1) H[i].push_back(TVal[j]);
		}
	}
	
	// Step #6. Topological Sort
	bool ret = TopologicalSort();
	return ret;
}

int main() {
	int Q;
	cin >> Q;
	for (int testcase = 1; testcase <= Q; testcase++) {
		// Input
		int NN, MM;
		cin >> NN;
		vector<int> AA(NN - 1, 0), BB(NN - 1, 0);
		for (int i = 0; i < NN - 1; i++) cin >> AA[i] >> BB[i];
		cin >> MM;
		vector<int> SS(MM, 0), TT(MM, 0);
		for (int i = 0; i < MM; i++) cin >> SS[i] >> TT[i];
		
		// Output
		bool FinalAns = GetAnswer(NN, AA, BB, MM, SS, TT);
		if (FinalAns == true) cout << "Yes" << endl;
		if (FinalAns == false) cout << "No" << endl;
	}
	return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 96 ms 216352 KB Output is correct
2 Correct 97 ms 216360 KB Output is correct
3 Correct 97 ms 216328 KB Output is correct
4 Correct 174 ms 217032 KB Output is correct
5 Correct 256 ms 217236 KB Output is correct
6 Correct 103 ms 216748 KB Output is correct
7 Correct 103 ms 216844 KB Output is correct
8 Correct 108 ms 216896 KB Output is correct
9 Correct 401 ms 227788 KB Output is correct
10 Correct 754 ms 419908 KB Output is correct
11 Correct 154 ms 216572 KB Output is correct
12 Correct 301 ms 217552 KB Output is correct
13 Correct 1000 ms 425696 KB Output is correct
14 Correct 828 ms 425912 KB Output is correct
15 Correct 1120 ms 427916 KB Output is correct
16 Correct 1612 ms 442408 KB Output is correct
17 Correct 1041 ms 428488 KB Output is correct
18 Correct 1100 ms 431608 KB Output is correct
19 Correct 1016 ms 428604 KB Output is correct
20 Correct 963 ms 428524 KB Output is correct
21 Correct 939 ms 428312 KB Output is correct
22 Correct 896 ms 425652 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 130 ms 216336 KB Output is correct
2 Correct 132 ms 216324 KB Output is correct
3 Correct 124 ms 216740 KB Output is correct
4 Correct 138 ms 216840 KB Output is correct
5 Correct 125 ms 216856 KB Output is correct
6 Correct 126 ms 216744 KB Output is correct
7 Correct 123 ms 216784 KB Output is correct
8 Correct 122 ms 216780 KB Output is correct
9 Correct 127 ms 216780 KB Output is correct
10 Correct 135 ms 216860 KB Output is correct
11 Correct 140 ms 216728 KB Output is correct
12 Correct 120 ms 216720 KB Output is correct
13 Correct 113 ms 216756 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 130 ms 216336 KB Output is correct
2 Correct 132 ms 216324 KB Output is correct
3 Correct 124 ms 216740 KB Output is correct
4 Correct 138 ms 216840 KB Output is correct
5 Correct 125 ms 216856 KB Output is correct
6 Correct 126 ms 216744 KB Output is correct
7 Correct 123 ms 216784 KB Output is correct
8 Correct 122 ms 216780 KB Output is correct
9 Correct 127 ms 216780 KB Output is correct
10 Correct 135 ms 216860 KB Output is correct
11 Correct 140 ms 216728 KB Output is correct
12 Correct 120 ms 216720 KB Output is correct
13 Correct 113 ms 216756 KB Output is correct
14 Correct 125 ms 216392 KB Output is correct
15 Correct 111 ms 216324 KB Output is correct
16 Correct 133 ms 216796 KB Output is correct
17 Correct 113 ms 216904 KB Output is correct
18 Correct 137 ms 216836 KB Output is correct
19 Correct 126 ms 216324 KB Output is correct
20 Correct 119 ms 216764 KB Output is correct
21 Correct 123 ms 216828 KB Output is correct
22 Correct 120 ms 216752 KB Output is correct
23 Correct 144 ms 216324 KB Output is correct
24 Correct 128 ms 216568 KB Output is correct
25 Correct 126 ms 216848 KB Output is correct
26 Correct 139 ms 216696 KB Output is correct
27 Correct 163 ms 216816 KB Output is correct
28 Correct 133 ms 216404 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 130 ms 216336 KB Output is correct
2 Correct 132 ms 216324 KB Output is correct
3 Correct 124 ms 216740 KB Output is correct
4 Correct 138 ms 216840 KB Output is correct
5 Correct 125 ms 216856 KB Output is correct
6 Correct 126 ms 216744 KB Output is correct
7 Correct 123 ms 216784 KB Output is correct
8 Correct 122 ms 216780 KB Output is correct
9 Correct 127 ms 216780 KB Output is correct
10 Correct 135 ms 216860 KB Output is correct
11 Correct 140 ms 216728 KB Output is correct
12 Correct 120 ms 216720 KB Output is correct
13 Correct 113 ms 216756 KB Output is correct
14 Correct 125 ms 216392 KB Output is correct
15 Correct 111 ms 216324 KB Output is correct
16 Correct 133 ms 216796 KB Output is correct
17 Correct 113 ms 216904 KB Output is correct
18 Correct 137 ms 216836 KB Output is correct
19 Correct 126 ms 216324 KB Output is correct
20 Correct 119 ms 216764 KB Output is correct
21 Correct 123 ms 216828 KB Output is correct
22 Correct 120 ms 216752 KB Output is correct
23 Correct 144 ms 216324 KB Output is correct
24 Correct 128 ms 216568 KB Output is correct
25 Correct 126 ms 216848 KB Output is correct
26 Correct 139 ms 216696 KB Output is correct
27 Correct 163 ms 216816 KB Output is correct
28 Correct 133 ms 216404 KB Output is correct
29 Correct 133 ms 216860 KB Output is correct
30 Correct 145 ms 216876 KB Output is correct
31 Correct 148 ms 216872 KB Output is correct
32 Correct 126 ms 216896 KB Output is correct
33 Correct 137 ms 216828 KB Output is correct
34 Correct 143 ms 216696 KB Output is correct
35 Correct 206 ms 216752 KB Output is correct
36 Correct 142 ms 216736 KB Output is correct
37 Correct 123 ms 216708 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 130 ms 216336 KB Output is correct
2 Correct 132 ms 216324 KB Output is correct
3 Correct 124 ms 216740 KB Output is correct
4 Correct 138 ms 216840 KB Output is correct
5 Correct 125 ms 216856 KB Output is correct
6 Correct 126 ms 216744 KB Output is correct
7 Correct 123 ms 216784 KB Output is correct
8 Correct 122 ms 216780 KB Output is correct
9 Correct 127 ms 216780 KB Output is correct
10 Correct 135 ms 216860 KB Output is correct
11 Correct 140 ms 216728 KB Output is correct
12 Correct 120 ms 216720 KB Output is correct
13 Correct 113 ms 216756 KB Output is correct
14 Correct 125 ms 216392 KB Output is correct
15 Correct 111 ms 216324 KB Output is correct
16 Correct 133 ms 216796 KB Output is correct
17 Correct 113 ms 216904 KB Output is correct
18 Correct 137 ms 216836 KB Output is correct
19 Correct 126 ms 216324 KB Output is correct
20 Correct 119 ms 216764 KB Output is correct
21 Correct 123 ms 216828 KB Output is correct
22 Correct 120 ms 216752 KB Output is correct
23 Correct 144 ms 216324 KB Output is correct
24 Correct 128 ms 216568 KB Output is correct
25 Correct 126 ms 216848 KB Output is correct
26 Correct 139 ms 216696 KB Output is correct
27 Correct 163 ms 216816 KB Output is correct
28 Correct 133 ms 216404 KB Output is correct
29 Correct 133 ms 216860 KB Output is correct
30 Correct 145 ms 216876 KB Output is correct
31 Correct 148 ms 216872 KB Output is correct
32 Correct 126 ms 216896 KB Output is correct
33 Correct 137 ms 216828 KB Output is correct
34 Correct 143 ms 216696 KB Output is correct
35 Correct 206 ms 216752 KB Output is correct
36 Correct 142 ms 216736 KB Output is correct
37 Correct 123 ms 216708 KB Output is correct
38 Correct 516 ms 227800 KB Output is correct
39 Correct 995 ms 419856 KB Output is correct
40 Correct 520 ms 228976 KB Output is correct
41 Correct 510 ms 228120 KB Output is correct
42 Correct 441 ms 228612 KB Output is correct
43 Correct 544 ms 228724 KB Output is correct
44 Correct 183 ms 218440 KB Output is correct
45 Correct 897 ms 418880 KB Output is correct
46 Correct 925 ms 418916 KB Output is correct
47 Correct 984 ms 418508 KB Output is correct
48 Correct 1032 ms 418504 KB Output is correct
49 Correct 938 ms 421428 KB Output is correct
50 Correct 934 ms 421432 KB Output is correct
51 Correct 1131 ms 421700 KB Output is correct
52 Correct 993 ms 421716 KB Output is correct
53 Correct 266 ms 230992 KB Output is correct
54 Correct 1284 ms 419156 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 128 ms 216364 KB Output is correct
2 Correct 139 ms 216396 KB Output is correct
3 Correct 130 ms 216368 KB Output is correct
4 Correct 111 ms 216348 KB Output is correct
5 Correct 148 ms 216508 KB Output is correct
6 Correct 139 ms 216764 KB Output is correct
7 Correct 117 ms 216772 KB Output is correct
8 Correct 113 ms 216344 KB Output is correct
9 Correct 109 ms 216380 KB Output is correct
10 Correct 128 ms 216544 KB Output is correct
11 Correct 130 ms 216368 KB Output is correct
12 Correct 122 ms 216708 KB Output is correct
13 Correct 248 ms 217096 KB Output is correct
14 Correct 335 ms 217376 KB Output is correct
15 Correct 285 ms 217244 KB Output is correct
16 Correct 1056 ms 419712 KB Output is correct
17 Correct 1126 ms 427612 KB Output is correct
18 Correct 1167 ms 439448 KB Output is correct
19 Correct 976 ms 421192 KB Output is correct
20 Correct 1022 ms 421132 KB Output is correct
21 Correct 1070 ms 421168 KB Output is correct
22 Correct 948 ms 426404 KB Output is correct
23 Correct 883 ms 426412 KB Output is correct
24 Correct 1269 ms 426904 KB Output is correct
25 Correct 1287 ms 426860 KB Output is correct
26 Correct 1214 ms 426804 KB Output is correct
27 Correct 981 ms 429716 KB Output is correct
28 Correct 869 ms 429588 KB Output is correct
29 Correct 815 ms 429680 KB Output is correct
30 Correct 987 ms 423396 KB Output is correct
31 Correct 793 ms 423384 KB Output is correct
32 Correct 1101 ms 423348 KB Output is correct
33 Correct 859 ms 423260 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 96 ms 216352 KB Output is correct
2 Correct 97 ms 216360 KB Output is correct
3 Correct 97 ms 216328 KB Output is correct
4 Correct 174 ms 217032 KB Output is correct
5 Correct 256 ms 217236 KB Output is correct
6 Correct 103 ms 216748 KB Output is correct
7 Correct 103 ms 216844 KB Output is correct
8 Correct 108 ms 216896 KB Output is correct
9 Correct 401 ms 227788 KB Output is correct
10 Correct 754 ms 419908 KB Output is correct
11 Correct 154 ms 216572 KB Output is correct
12 Correct 301 ms 217552 KB Output is correct
13 Correct 1000 ms 425696 KB Output is correct
14 Correct 828 ms 425912 KB Output is correct
15 Correct 1120 ms 427916 KB Output is correct
16 Correct 1612 ms 442408 KB Output is correct
17 Correct 1041 ms 428488 KB Output is correct
18 Correct 1100 ms 431608 KB Output is correct
19 Correct 1016 ms 428604 KB Output is correct
20 Correct 963 ms 428524 KB Output is correct
21 Correct 939 ms 428312 KB Output is correct
22 Correct 896 ms 425652 KB Output is correct
23 Correct 130 ms 216336 KB Output is correct
24 Correct 132 ms 216324 KB Output is correct
25 Correct 124 ms 216740 KB Output is correct
26 Correct 138 ms 216840 KB Output is correct
27 Correct 125 ms 216856 KB Output is correct
28 Correct 126 ms 216744 KB Output is correct
29 Correct 123 ms 216784 KB Output is correct
30 Correct 122 ms 216780 KB Output is correct
31 Correct 127 ms 216780 KB Output is correct
32 Correct 135 ms 216860 KB Output is correct
33 Correct 140 ms 216728 KB Output is correct
34 Correct 120 ms 216720 KB Output is correct
35 Correct 113 ms 216756 KB Output is correct
36 Correct 125 ms 216392 KB Output is correct
37 Correct 111 ms 216324 KB Output is correct
38 Correct 133 ms 216796 KB Output is correct
39 Correct 113 ms 216904 KB Output is correct
40 Correct 137 ms 216836 KB Output is correct
41 Correct 126 ms 216324 KB Output is correct
42 Correct 119 ms 216764 KB Output is correct
43 Correct 123 ms 216828 KB Output is correct
44 Correct 120 ms 216752 KB Output is correct
45 Correct 144 ms 216324 KB Output is correct
46 Correct 128 ms 216568 KB Output is correct
47 Correct 126 ms 216848 KB Output is correct
48 Correct 139 ms 216696 KB Output is correct
49 Correct 163 ms 216816 KB Output is correct
50 Correct 133 ms 216404 KB Output is correct
51 Correct 133 ms 216860 KB Output is correct
52 Correct 145 ms 216876 KB Output is correct
53 Correct 148 ms 216872 KB Output is correct
54 Correct 126 ms 216896 KB Output is correct
55 Correct 137 ms 216828 KB Output is correct
56 Correct 143 ms 216696 KB Output is correct
57 Correct 206 ms 216752 KB Output is correct
58 Correct 142 ms 216736 KB Output is correct
59 Correct 123 ms 216708 KB Output is correct
60 Correct 516 ms 227800 KB Output is correct
61 Correct 995 ms 419856 KB Output is correct
62 Correct 520 ms 228976 KB Output is correct
63 Correct 510 ms 228120 KB Output is correct
64 Correct 441 ms 228612 KB Output is correct
65 Correct 544 ms 228724 KB Output is correct
66 Correct 183 ms 218440 KB Output is correct
67 Correct 897 ms 418880 KB Output is correct
68 Correct 925 ms 418916 KB Output is correct
69 Correct 984 ms 418508 KB Output is correct
70 Correct 1032 ms 418504 KB Output is correct
71 Correct 938 ms 421428 KB Output is correct
72 Correct 934 ms 421432 KB Output is correct
73 Correct 1131 ms 421700 KB Output is correct
74 Correct 993 ms 421716 KB Output is correct
75 Correct 266 ms 230992 KB Output is correct
76 Correct 1284 ms 419156 KB Output is correct
77 Correct 128 ms 216364 KB Output is correct
78 Correct 139 ms 216396 KB Output is correct
79 Correct 130 ms 216368 KB Output is correct
80 Correct 111 ms 216348 KB Output is correct
81 Correct 148 ms 216508 KB Output is correct
82 Correct 139 ms 216764 KB Output is correct
83 Correct 117 ms 216772 KB Output is correct
84 Correct 113 ms 216344 KB Output is correct
85 Correct 109 ms 216380 KB Output is correct
86 Correct 128 ms 216544 KB Output is correct
87 Correct 130 ms 216368 KB Output is correct
88 Correct 122 ms 216708 KB Output is correct
89 Correct 248 ms 217096 KB Output is correct
90 Correct 335 ms 217376 KB Output is correct
91 Correct 285 ms 217244 KB Output is correct
92 Correct 1056 ms 419712 KB Output is correct
93 Correct 1126 ms 427612 KB Output is correct
94 Correct 1167 ms 439448 KB Output is correct
95 Correct 976 ms 421192 KB Output is correct
96 Correct 1022 ms 421132 KB Output is correct
97 Correct 1070 ms 421168 KB Output is correct
98 Correct 948 ms 426404 KB Output is correct
99 Correct 883 ms 426412 KB Output is correct
100 Correct 1269 ms 426904 KB Output is correct
101 Correct 1287 ms 426860 KB Output is correct
102 Correct 1214 ms 426804 KB Output is correct
103 Correct 981 ms 429716 KB Output is correct
104 Correct 869 ms 429588 KB Output is correct
105 Correct 815 ms 429680 KB Output is correct
106 Correct 987 ms 423396 KB Output is correct
107 Correct 793 ms 423384 KB Output is correct
108 Correct 1101 ms 423348 KB Output is correct
109 Correct 859 ms 423260 KB Output is correct
110 Correct 349 ms 217804 KB Output is correct
111 Correct 301 ms 217248 KB Output is correct
112 Correct 1263 ms 433252 KB Output is correct
113 Correct 1298 ms 423160 KB Output is correct
114 Correct 1055 ms 428544 KB Output is correct
115 Correct 604 ms 417520 KB Output is correct
116 Correct 1272 ms 425392 KB Output is correct
117 Correct 1385 ms 442320 KB Output is correct
118 Correct 1135 ms 419124 KB Output is correct
119 Correct 1350 ms 419120 KB Output is correct
120 Correct 213 ms 233828 KB Output is correct
121 Correct 1551 ms 427292 KB Output is correct
122 Correct 1432 ms 427084 KB Output is correct
123 Correct 1392 ms 423176 KB Output is correct
124 Correct 1104 ms 423184 KB Output is correct
125 Correct 1477 ms 423744 KB Output is correct
126 Correct 1710 ms 446332 KB Output is correct
127 Correct 1138 ms 433004 KB Output is correct
128 Correct 1040 ms 432984 KB Output is correct
129 Correct 1302 ms 433080 KB Output is correct
130 Correct 1253 ms 433104 KB Output is correct