Submission #1040568

# Submission time Handle Problem Language Result Execution time Memory
1040568 2024-08-01T07:29:25 Z 정희우(#10998) The Ties That Guide Us (CEOI23_incursion) C++17
100 / 100
247 ms 14480 KB
#include <vector>
#include "incursion.h"
#include <cstdio>
#include<iostream>
#include<algorithm>

using namespace std;
using lint = long long;
using intp = pair<int,int>;
using vint = vector<int>;
using vintp = vector<intp>;

const int MAX_N=45010;

int n;
vintp eg[MAX_N];
int sz[MAX_N];
vint val;
int isleaf=0;

bool compe(const intp &e1,const intp &e2)
{
	return e1.second>e2.second;
}

int fillsz(int v,int p)
{
	sz[v]=1;
	for(auto &e : eg[v])
		if(e.first!=p)
		{
			e.second=fillsz(e.first,v);
			sz[v]+=e.second;
		}
	for(auto &e : eg[v])
		if(e.first==p)
			e.second=n-sz[v];
	sort(eg[v].begin(),eg[v].end(),compe);
	return sz[v];
}

bool ismid(int v)
{
	int mx=0;
	for(auto e : eg[v])
		mx=max(mx,e.second);
	int c=0;
	for(auto e : eg[v])
		if(e.second==mx)c++;
	return c>=2;
}

void dfs(int v,int p)
{
	if(ismid(v))
	{
		val[v-1]=isleaf;
	}
	else if(p==0)
	{
		val[v-1]=0;
	}
	else
	{
		int mx=0;
		for(auto e : eg[v])
			mx=max(mx,e.second);
		for(auto e : eg[v])
			if(e.first==p)
				val[v-1]=(e.second==mx);
	}
	for(auto e : eg[v])
		if(e.first!=p)
			dfs(e.first,v);
}

vint mark(vintp F, int safe)
{
  	n=F.size()+1;
	for(int i=1;i<=n;i++)
		eg[i].clear();
	for(int i=0;i<n-1;i++)
	{
		int u=F[i].first,v=F[i].second;
		eg[u].push_back({v,0});
		eg[v].push_back({u,0});
	}
	val.resize(n);
	fillsz(safe,0);
	isleaf=(eg[safe].size()<=1);
	dfs(safe,0);
	return val;
}

bool checkleaf(int v,int p)
{
	if(isleaf==-1)return false;
	int flag=1;
	for(auto e : eg[v])
		if(e.first!=p && eg[e.first].size()>1)flag=0;
	if(flag==0)return false;
	if(isleaf==0)return true;
	for(auto e : eg[v])
		if(e.first!=p)
		{
			if(visit(e.first)==0)return true;
			visit(v);
		}
	return false;
}

void calc(int v,int p,int t)
{
	if(ismid(v))
	{
		isleaf=t;
		if(checkleaf(v,p))
			return;
		for(auto e : eg[v])
			if(e.first!=p)
			{
				int nt=visit(e.first);
				if(nt==0)
				{
					calc(e.first,v,nt);
					return;
				}
				visit(v);
			}
		return;
	}
	if(checkleaf(v,p))
		return;
	if(t==1)return calc(eg[v][0].first,v,visit(eg[v][0].first));
	if(t==0 && eg[v].size()<=1)return;
	int nt;
	if(eg[v][1].first!=p)
	{
		nt=visit(eg[v][1].first);
		if(nt==0)
		{
			calc(eg[v][1].first,v,nt);
			return;
		}
		visit(v);
	}
	if(eg[v].size()>=3 && eg[v][2].first!=p)
	{
		nt=visit(eg[v][2].first);
		if(nt==0)
		{
			calc(eg[v][2].first,v,nt);
			return;
		}
		visit(v);
	}
}

void locate(vintp F, int curr, int t)
{
	n=F.size()+1;
	for(int i=1;i<=n;i++)
		eg[i].clear();
	for(int i=0;i<n-1;i++)
	{
		int u=F[i].first,v=F[i].second;
		eg[u].push_back({v,0});
		eg[v].push_back({u,0});
	}
	fillsz(curr,0);
	isleaf=-1;
	calc(curr,0,t);
	return;
}

Compilation message

interface.cpp: In function 'int main()':
interface.cpp:44:55: warning: comparison of integer expressions of different signedness: 'size_t' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   44 |     if(fread(T.data(), sizeof(int), 2 * N - 2, stdin) != 2 * N - 2) exit(0);
      |        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~
interface.cpp:50:33: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   50 |         int l = (numbers.size() == N ? N : 0);
      |                  ~~~~~~~~~~~~~~~^~~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 2816 KB Correct
# Verdict Execution time Memory Grader output
1 Correct 197 ms 13048 KB Correct
2 Correct 181 ms 13024 KB Correct
3 Correct 100 ms 14216 KB Correct
4 Correct 79 ms 12188 KB Correct
5 Correct 170 ms 12980 KB Correct
6 Correct 68 ms 11148 KB Correct
7 Correct 60 ms 11164 KB Correct
8 Correct 186 ms 13016 KB Correct
9 Correct 165 ms 13012 KB Correct
10 Correct 128 ms 12192 KB Correct
11 Correct 80 ms 13180 KB Correct
12 Correct 247 ms 13708 KB Correct
13 Correct 69 ms 11176 KB Correct
14 Correct 78 ms 11148 KB Correct
15 Correct 178 ms 12968 KB Correct
16 Correct 183 ms 13036 KB Correct
17 Correct 99 ms 12368 KB Correct
18 Correct 73 ms 13616 KB Correct
19 Correct 133 ms 13260 KB Correct
20 Correct 68 ms 11144 KB Correct
21 Correct 68 ms 11128 KB Correct
22 Correct 197 ms 13032 KB Correct
23 Correct 185 ms 13012 KB Correct
24 Correct 89 ms 12676 KB Correct
25 Correct 74 ms 14240 KB Correct
26 Correct 83 ms 12700 KB Correct
27 Correct 67 ms 11104 KB Correct
28 Correct 80 ms 11168 KB Correct
29 Correct 183 ms 12936 KB Correct
30 Correct 185 ms 12960 KB Correct
31 Correct 76 ms 11856 KB Correct
32 Correct 208 ms 13424 KB Correct
33 Correct 207 ms 13356 KB Correct
34 Correct 76 ms 11168 KB Correct
35 Correct 76 ms 11040 KB Correct
36 Correct 178 ms 13212 KB Correct
37 Correct 178 ms 12956 KB Correct
38 Correct 221 ms 13576 KB Correct
39 Correct 133 ms 13444 KB Correct
40 Correct 159 ms 12780 KB Correct
41 Correct 73 ms 11140 KB Correct
42 Correct 69 ms 11132 KB Correct
43 Correct 164 ms 13220 KB Correct
44 Correct 190 ms 13016 KB Correct
45 Correct 74 ms 13172 KB Correct
46 Correct 71 ms 13684 KB Correct
47 Correct 86 ms 12692 KB Correct
48 Correct 68 ms 11100 KB Correct
49 Correct 70 ms 11232 KB Correct
# Verdict Execution time Memory Grader output
1 Correct 72 ms 8804 KB Correct
2 Correct 72 ms 8824 KB Correct
3 Correct 67 ms 8872 KB Correct
4 Correct 93 ms 11800 KB Correct
5 Correct 110 ms 11176 KB Correct
6 Correct 131 ms 11844 KB Correct
7 Correct 65 ms 8816 KB Correct
8 Correct 67 ms 8728 KB Correct
9 Correct 71 ms 8576 KB Correct
10 Correct 64 ms 8584 KB Correct
11 Correct 64 ms 8560 KB Correct
12 Correct 65 ms 8576 KB Correct
13 Correct 69 ms 8576 KB Correct
14 Correct 45 ms 7224 KB Correct
15 Correct 52 ms 7208 KB Correct
16 Correct 75 ms 8564 KB Correct
17 Correct 74 ms 8552 KB Correct
18 Correct 67 ms 8572 KB Correct
19 Correct 68 ms 8608 KB Correct
20 Correct 45 ms 7208 KB Correct
21 Correct 54 ms 7220 KB Correct
22 Correct 49 ms 7088 KB Correct
23 Correct 52 ms 7212 KB Correct
24 Correct 52 ms 7260 KB Correct
25 Correct 49 ms 7220 KB Correct
26 Correct 70 ms 9076 KB Correct
27 Correct 66 ms 8576 KB Correct
28 Correct 71 ms 8580 KB Correct
29 Correct 67 ms 8712 KB Correct
30 Correct 76 ms 8828 KB Correct
31 Correct 69 ms 8640 KB Correct
32 Correct 67 ms 8596 KB Correct
33 Correct 69 ms 8608 KB Correct
34 Correct 71 ms 8832 KB Correct
35 Correct 60 ms 8812 KB Correct
36 Correct 76 ms 8608 KB Correct
37 Correct 59 ms 8668 KB Correct
38 Correct 70 ms 8804 KB Correct
39 Correct 74 ms 8608 KB Correct
40 Correct 77 ms 8572 KB Correct
41 Correct 66 ms 8596 KB Correct
42 Correct 70 ms 8604 KB Correct
43 Correct 68 ms 8788 KB Correct
44 Correct 72 ms 8612 KB Correct
45 Correct 66 ms 8580 KB Correct
46 Correct 75 ms 8580 KB Correct
47 Correct 68 ms 8836 KB Correct
48 Correct 79 ms 8736 KB Correct
49 Correct 67 ms 8684 KB Correct
50 Correct 71 ms 8444 KB Correct
51 Correct 68 ms 8568 KB Correct
52 Correct 68 ms 8608 KB Correct
53 Correct 73 ms 8560 KB Correct
54 Correct 76 ms 8792 KB Correct
55 Correct 68 ms 8580 KB Correct
56 Correct 70 ms 8868 KB Correct
57 Correct 70 ms 8732 KB Correct
58 Correct 74 ms 8816 KB Correct
59 Correct 69 ms 8868 KB Correct
60 Correct 64 ms 8832 KB Correct
61 Correct 64 ms 8776 KB Correct
62 Correct 68 ms 8860 KB Correct
63 Correct 68 ms 8860 KB Correct
64 Correct 65 ms 8840 KB Correct
65 Correct 66 ms 8868 KB Correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 2816 KB Correct
2 Correct 197 ms 13048 KB Correct
3 Correct 181 ms 13024 KB Correct
4 Correct 100 ms 14216 KB Correct
5 Correct 79 ms 12188 KB Correct
6 Correct 170 ms 12980 KB Correct
7 Correct 68 ms 11148 KB Correct
8 Correct 60 ms 11164 KB Correct
9 Correct 186 ms 13016 KB Correct
10 Correct 165 ms 13012 KB Correct
11 Correct 128 ms 12192 KB Correct
12 Correct 80 ms 13180 KB Correct
13 Correct 247 ms 13708 KB Correct
14 Correct 69 ms 11176 KB Correct
15 Correct 78 ms 11148 KB Correct
16 Correct 178 ms 12968 KB Correct
17 Correct 183 ms 13036 KB Correct
18 Correct 99 ms 12368 KB Correct
19 Correct 73 ms 13616 KB Correct
20 Correct 133 ms 13260 KB Correct
21 Correct 68 ms 11144 KB Correct
22 Correct 68 ms 11128 KB Correct
23 Correct 197 ms 13032 KB Correct
24 Correct 185 ms 13012 KB Correct
25 Correct 89 ms 12676 KB Correct
26 Correct 74 ms 14240 KB Correct
27 Correct 83 ms 12700 KB Correct
28 Correct 67 ms 11104 KB Correct
29 Correct 80 ms 11168 KB Correct
30 Correct 183 ms 12936 KB Correct
31 Correct 185 ms 12960 KB Correct
32 Correct 76 ms 11856 KB Correct
33 Correct 208 ms 13424 KB Correct
34 Correct 207 ms 13356 KB Correct
35 Correct 76 ms 11168 KB Correct
36 Correct 76 ms 11040 KB Correct
37 Correct 178 ms 13212 KB Correct
38 Correct 178 ms 12956 KB Correct
39 Correct 221 ms 13576 KB Correct
40 Correct 133 ms 13444 KB Correct
41 Correct 159 ms 12780 KB Correct
42 Correct 73 ms 11140 KB Correct
43 Correct 69 ms 11132 KB Correct
44 Correct 164 ms 13220 KB Correct
45 Correct 190 ms 13016 KB Correct
46 Correct 74 ms 13172 KB Correct
47 Correct 71 ms 13684 KB Correct
48 Correct 86 ms 12692 KB Correct
49 Correct 68 ms 11100 KB Correct
50 Correct 70 ms 11232 KB Correct
51 Correct 72 ms 8804 KB Correct
52 Correct 72 ms 8824 KB Correct
53 Correct 67 ms 8872 KB Correct
54 Correct 93 ms 11800 KB Correct
55 Correct 110 ms 11176 KB Correct
56 Correct 131 ms 11844 KB Correct
57 Correct 65 ms 8816 KB Correct
58 Correct 67 ms 8728 KB Correct
59 Correct 71 ms 8576 KB Correct
60 Correct 64 ms 8584 KB Correct
61 Correct 64 ms 8560 KB Correct
62 Correct 65 ms 8576 KB Correct
63 Correct 69 ms 8576 KB Correct
64 Correct 45 ms 7224 KB Correct
65 Correct 52 ms 7208 KB Correct
66 Correct 75 ms 8564 KB Correct
67 Correct 74 ms 8552 KB Correct
68 Correct 67 ms 8572 KB Correct
69 Correct 68 ms 8608 KB Correct
70 Correct 45 ms 7208 KB Correct
71 Correct 54 ms 7220 KB Correct
72 Correct 49 ms 7088 KB Correct
73 Correct 52 ms 7212 KB Correct
74 Correct 52 ms 7260 KB Correct
75 Correct 49 ms 7220 KB Correct
76 Correct 70 ms 9076 KB Correct
77 Correct 66 ms 8576 KB Correct
78 Correct 71 ms 8580 KB Correct
79 Correct 67 ms 8712 KB Correct
80 Correct 76 ms 8828 KB Correct
81 Correct 69 ms 8640 KB Correct
82 Correct 67 ms 8596 KB Correct
83 Correct 69 ms 8608 KB Correct
84 Correct 71 ms 8832 KB Correct
85 Correct 60 ms 8812 KB Correct
86 Correct 76 ms 8608 KB Correct
87 Correct 59 ms 8668 KB Correct
88 Correct 70 ms 8804 KB Correct
89 Correct 74 ms 8608 KB Correct
90 Correct 77 ms 8572 KB Correct
91 Correct 66 ms 8596 KB Correct
92 Correct 70 ms 8604 KB Correct
93 Correct 68 ms 8788 KB Correct
94 Correct 72 ms 8612 KB Correct
95 Correct 66 ms 8580 KB Correct
96 Correct 75 ms 8580 KB Correct
97 Correct 68 ms 8836 KB Correct
98 Correct 79 ms 8736 KB Correct
99 Correct 67 ms 8684 KB Correct
100 Correct 71 ms 8444 KB Correct
101 Correct 68 ms 8568 KB Correct
102 Correct 68 ms 8608 KB Correct
103 Correct 73 ms 8560 KB Correct
104 Correct 76 ms 8792 KB Correct
105 Correct 68 ms 8580 KB Correct
106 Correct 70 ms 8868 KB Correct
107 Correct 70 ms 8732 KB Correct
108 Correct 74 ms 8816 KB Correct
109 Correct 69 ms 8868 KB Correct
110 Correct 64 ms 8832 KB Correct
111 Correct 64 ms 8776 KB Correct
112 Correct 68 ms 8860 KB Correct
113 Correct 68 ms 8860 KB Correct
114 Correct 65 ms 8840 KB Correct
115 Correct 66 ms 8868 KB Correct
116 Correct 68 ms 8348 KB Correct
117 Correct 76 ms 8328 KB Correct
118 Correct 78 ms 8092 KB Correct
119 Correct 68 ms 8328 KB Correct
120 Correct 74 ms 8840 KB Correct
121 Correct 85 ms 8944 KB Correct
122 Correct 78 ms 8860 KB Correct
123 Correct 77 ms 11360 KB Correct
124 Correct 103 ms 11540 KB Correct
125 Correct 170 ms 11384 KB Correct
126 Correct 76 ms 8508 KB Correct
127 Correct 76 ms 8572 KB Correct
128 Correct 79 ms 8672 KB Correct
129 Correct 80 ms 8508 KB Correct
130 Correct 75 ms 8532 KB Correct
131 Correct 87 ms 8528 KB Correct
132 Correct 69 ms 8560 KB Correct
133 Correct 56 ms 7204 KB Correct
134 Correct 56 ms 7012 KB Correct
135 Correct 71 ms 8524 KB Correct
136 Correct 79 ms 8556 KB Correct
137 Correct 88 ms 8612 KB Correct
138 Correct 68 ms 8672 KB Correct
139 Correct 56 ms 7232 KB Correct
140 Correct 45 ms 6956 KB Correct
141 Correct 45 ms 7092 KB Correct
142 Correct 57 ms 7080 KB Correct
143 Correct 50 ms 7204 KB Correct
144 Correct 54 ms 7220 KB Correct
145 Correct 75 ms 8448 KB Correct
146 Correct 70 ms 8920 KB Correct
147 Correct 77 ms 8572 KB Correct
148 Correct 77 ms 8580 KB Correct
149 Correct 68 ms 8604 KB Correct
150 Correct 71 ms 8556 KB Correct
151 Correct 82 ms 8808 KB Correct
152 Correct 69 ms 8480 KB Correct
153 Correct 68 ms 8804 KB Correct
154 Correct 71 ms 8720 KB Correct
155 Correct 72 ms 8624 KB Correct
156 Correct 68 ms 8560 KB Correct
157 Correct 76 ms 8584 KB Correct
158 Correct 79 ms 8792 KB Correct
159 Correct 73 ms 8608 KB Correct
160 Correct 74 ms 8756 KB Correct
161 Correct 83 ms 8928 KB Correct
162 Correct 73 ms 8780 KB Correct
163 Correct 66 ms 8836 KB Correct
164 Correct 75 ms 8448 KB Correct
165 Correct 82 ms 8808 KB Correct
166 Correct 72 ms 9100 KB Correct
167 Correct 68 ms 8572 KB Correct
168 Correct 64 ms 8688 KB Correct
169 Correct 73 ms 9060 KB Correct
170 Correct 65 ms 8620 KB Correct
171 Correct 76 ms 8820 KB Correct
172 Correct 81 ms 8444 KB Correct
173 Correct 92 ms 9168 KB Correct
174 Correct 68 ms 8544 KB Correct
175 Correct 69 ms 8796 KB Correct
176 Correct 90 ms 8604 KB Correct
177 Correct 75 ms 8820 KB Correct
178 Correct 82 ms 8808 KB Correct
179 Correct 77 ms 8972 KB Correct
180 Correct 78 ms 8652 KB Correct
181 Correct 81 ms 9040 KB Correct
182 Correct 76 ms 8772 KB Correct
183 Correct 88 ms 9020 KB Correct
184 Correct 80 ms 8688 KB Correct
185 Correct 170 ms 12992 KB Correct
186 Correct 191 ms 12992 KB Correct
187 Correct 105 ms 14480 KB Correct
188 Correct 100 ms 12396 KB Correct
189 Correct 156 ms 12984 KB Correct
190 Correct 83 ms 11104 KB Correct
191 Correct 69 ms 11144 KB Correct
192 Correct 181 ms 13216 KB Correct
193 Correct 170 ms 13132 KB Correct
194 Correct 127 ms 12460 KB Correct
195 Correct 87 ms 13044 KB Correct
196 Correct 211 ms 13908 KB Correct
197 Correct 75 ms 11396 KB Correct
198 Correct 81 ms 11128 KB Correct
199 Correct 175 ms 13044 KB Correct
200 Correct 189 ms 13012 KB Correct
201 Correct 104 ms 12600 KB Correct
202 Correct 78 ms 13656 KB Correct
203 Correct 125 ms 13380 KB Correct
204 Correct 72 ms 11140 KB Correct
205 Correct 82 ms 11088 KB Correct
206 Correct 180 ms 13048 KB Correct
207 Correct 177 ms 13028 KB Correct
208 Correct 83 ms 12668 KB Correct
209 Correct 72 ms 14220 KB Correct
210 Correct 76 ms 12676 KB Correct
211 Correct 67 ms 11132 KB Correct
212 Correct 68 ms 11176 KB Correct
213 Correct 194 ms 13024 KB Correct
214 Correct 192 ms 13040 KB Correct
215 Correct 73 ms 11940 KB Correct
216 Correct 200 ms 13432 KB Correct
217 Correct 219 ms 13360 KB Correct
218 Correct 72 ms 11108 KB Correct
219 Correct 76 ms 11164 KB Correct
220 Correct 176 ms 13044 KB Correct
221 Correct 183 ms 13008 KB Correct
222 Correct 243 ms 13580 KB Correct
223 Correct 120 ms 13240 KB Correct
224 Correct 184 ms 12776 KB Correct
225 Correct 74 ms 11156 KB Correct
226 Correct 69 ms 11288 KB Correct
227 Correct 178 ms 13088 KB Correct
228 Correct 191 ms 13036 KB Correct
229 Correct 75 ms 13188 KB Correct
230 Correct 72 ms 13468 KB Correct
231 Correct 82 ms 12648 KB Correct
232 Correct 70 ms 11136 KB Correct
233 Correct 68 ms 11140 KB Correct
234 Correct 72 ms 11912 KB Correct
235 Correct 89 ms 10920 KB Correct
236 Correct 52 ms 7220 KB Correct
237 Correct 53 ms 7204 KB Correct
238 Correct 50 ms 7124 KB Correct
239 Correct 35 ms 6124 KB Correct
240 Correct 62 ms 8552 KB Correct
241 Correct 72 ms 8604 KB Correct
242 Correct 68 ms 8604 KB Correct
243 Correct 70 ms 8544 KB Correct
244 Correct 67 ms 8608 KB Correct
245 Correct 67 ms 8576 KB Correct
246 Correct 72 ms 8580 KB Correct
247 Correct 61 ms 8584 KB Correct
248 Correct 73 ms 8612 KB Correct
249 Correct 68 ms 8572 KB Correct
250 Correct 72 ms 8552 KB Correct
251 Correct 73 ms 8556 KB Correct
252 Correct 71 ms 8584 KB Correct
253 Correct 68 ms 8588 KB Correct
254 Correct 69 ms 8604 KB Correct
255 Correct 66 ms 8556 KB Correct
256 Correct 61 ms 8580 KB Correct
257 Correct 67 ms 8592 KB Correct
258 Correct 72 ms 8640 KB Correct
259 Correct 71 ms 8608 KB Correct
260 Correct 64 ms 8616 KB Correct
261 Correct 68 ms 8532 KB Correct
262 Correct 69 ms 8576 KB Correct
263 Correct 63 ms 8572 KB Correct
264 Correct 71 ms 8672 KB Correct
265 Correct 51 ms 7256 KB Correct
266 Correct 44 ms 6952 KB Correct
267 Correct 46 ms 7232 KB Correct
268 Correct 50 ms 7348 KB Correct
269 Correct 47 ms 6836 KB Correct
270 Correct 45 ms 7196 KB Correct
271 Correct 48 ms 7196 KB Correct
272 Correct 53 ms 7208 KB Correct
273 Correct 46 ms 6968 KB Correct
274 Correct 49 ms 7204 KB Correct
275 Correct 46 ms 7208 KB Correct
276 Correct 47 ms 7216 KB Correct
277 Correct 51 ms 7216 KB Correct
278 Correct 52 ms 7200 KB Correct
279 Correct 53 ms 6820 KB Correct
280 Correct 71 ms 8092 KB Correct
281 Correct 74 ms 8176 KB Correct
282 Correct 70 ms 8476 KB Correct
283 Correct 67 ms 8024 KB Correct
284 Correct 69 ms 8196 KB Correct
285 Correct 64 ms 8096 KB Correct
286 Correct 69 ms 8348 KB Correct
287 Correct 74 ms 8288 KB Correct
288 Correct 66 ms 8268 KB Correct
289 Correct 70 ms 8220 KB Correct