#include <bits/stdc++.h>
using namespace std;
#define int long long
mt19937_64 RNG(chrono::steady_clock::now().time_since_epoch().count());
const int N = 2e5 + 5;
vector<int> g[N];
int a[N];
struct node
{
int index, sum, parent;
bool merge;
pair<int, int> children;
node(int i, int s, int p, bool m)
{
index = i;
sum = s;
parent = p;
merge = m;
children = {-1, -1};
}
} *reach[2 * N];
int Find(int idx)
{
if(reach[idx]->parent == idx)
{
return idx;
}
return reach[idx]->parent = Find(reach[idx]->parent);
}
void Unite(int node1, int node2, int parent_node)
{
reach[node1]->parent = parent_node;
reach[node2]->parent = parent_node;
reach[parent_node]->sum = reach[node1]->sum + reach[node2]->sum;
reach[parent_node]->children = {node1, node2};
reach[parent_node]->merge = false;
}
void dfs(int node, bool merge_parents)
{
reach[node]->merge &= merge_parents;
if(reach[node]->children.first == -1)
{
return;
}
dfs(reach[node]->children.first, reach[node]->merge);
dfs(reach[node]->children.second, reach[node]->merge);
}
void Solve()
{
int n, m;
cin >> n >> m;
for(int i = n + 1; i <= 2 * n; i++)
{
reach[i] = new node(i, 0, i, false);
}
vector<array<int, 2> > order;
for(int i = 1; i <= n; i++)
{
cin >> a[i];
reach[i] = new node(i, a[i], i, false);
order.push_back({a[i], i});
}
for(int i = 1; i <= m; i++)
{
int u, v;
cin >> u >> v;
g[u].push_back(v);
g[v].push_back(u);
}
sort(order.begin(), order.end());
int new_index = n + 1;
for(int i = 0; i < n; i++)
{
int current_index = order[i][1];
for(int to: g[current_index])
{
if(a[to] <= a[current_index])
{
int parent_current_index = Find(current_index);
int parent_index = Find(to);
if(parent_index == parent_current_index)
{
continue;
}
reach[parent_current_index]->merge = true;
if(reach[parent_index]->sum >= a[current_index])
{
reach[parent_index]->merge = true;
}
Unite(parent_current_index, parent_index, new_index);
new_index = new_index + 1;
}
}
}
reach[new_index - 1]->merge = true;
dfs(new_index - 1, true);
for(int i = 1; i <= n; i++)
{
cout << ((reach[i]->merge) ? ('1') : ('0'));
}
}
int32_t main()
{
auto begin = std::chrono::high_resolution_clock::now();
ios_base::sync_with_stdio(0);
cin.tie(0);
int t = 1;
for(int i = 1; i <= t; i++)
{
Solve();
}
auto end = std::chrono::high_resolution_clock::now();
auto elapsed = std::chrono::duration_cast<std::chrono::nanoseconds>(end - begin);
cerr << "Time measured: " << elapsed.count() * 1e-9 << " seconds.\n";
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
4 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
4948 KB |
Output is correct |
4 |
Correct |
5 ms |
5332 KB |
Output is correct |
5 |
Correct |
4 ms |
5428 KB |
Output is correct |
6 |
Correct |
4 ms |
5332 KB |
Output is correct |
7 |
Correct |
4 ms |
5296 KB |
Output is correct |
8 |
Correct |
4 ms |
5332 KB |
Output is correct |
9 |
Correct |
4 ms |
5460 KB |
Output is correct |
10 |
Correct |
4 ms |
5424 KB |
Output is correct |
11 |
Correct |
4 ms |
5420 KB |
Output is correct |
12 |
Correct |
6 ms |
5460 KB |
Output is correct |
13 |
Correct |
4 ms |
5460 KB |
Output is correct |
14 |
Correct |
5 ms |
5460 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
5028 KB |
Output is correct |
3 |
Correct |
181 ms |
46416 KB |
Output is correct |
4 |
Correct |
168 ms |
51440 KB |
Output is correct |
5 |
Correct |
245 ms |
47356 KB |
Output is correct |
6 |
Correct |
246 ms |
48688 KB |
Output is correct |
7 |
Correct |
241 ms |
48540 KB |
Output is correct |
8 |
Correct |
296 ms |
54792 KB |
Output is correct |
9 |
Correct |
183 ms |
49984 KB |
Output is correct |
10 |
Correct |
182 ms |
55472 KB |
Output is correct |
11 |
Correct |
218 ms |
55504 KB |
Output is correct |
12 |
Correct |
290 ms |
49720 KB |
Output is correct |
13 |
Correct |
147 ms |
53472 KB |
Output is correct |
14 |
Correct |
167 ms |
53004 KB |
Output is correct |
15 |
Correct |
186 ms |
47568 KB |
Output is correct |
16 |
Correct |
111 ms |
47152 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5028 KB |
Output is correct |
2 |
Correct |
257 ms |
44400 KB |
Output is correct |
3 |
Correct |
272 ms |
44616 KB |
Output is correct |
4 |
Correct |
187 ms |
53844 KB |
Output is correct |
5 |
Correct |
168 ms |
51504 KB |
Output is correct |
6 |
Correct |
238 ms |
47640 KB |
Output is correct |
7 |
Correct |
177 ms |
47536 KB |
Output is correct |
8 |
Correct |
177 ms |
47552 KB |
Output is correct |
9 |
Correct |
109 ms |
47308 KB |
Output is correct |
10 |
Correct |
160 ms |
49144 KB |
Output is correct |
11 |
Correct |
180 ms |
47936 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5076 KB |
Output is correct |
2 |
Correct |
333 ms |
45632 KB |
Output is correct |
3 |
Correct |
328 ms |
48396 KB |
Output is correct |
4 |
Correct |
307 ms |
48636 KB |
Output is correct |
5 |
Correct |
361 ms |
48552 KB |
Output is correct |
6 |
Correct |
302 ms |
47288 KB |
Output is correct |
7 |
Correct |
211 ms |
52560 KB |
Output is correct |
8 |
Correct |
125 ms |
52212 KB |
Output is correct |
9 |
Correct |
174 ms |
30044 KB |
Output is correct |
10 |
Correct |
328 ms |
48372 KB |
Output is correct |
11 |
Correct |
182 ms |
47908 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
4 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
4948 KB |
Output is correct |
4 |
Correct |
5 ms |
5332 KB |
Output is correct |
5 |
Correct |
4 ms |
5428 KB |
Output is correct |
6 |
Correct |
4 ms |
5332 KB |
Output is correct |
7 |
Correct |
4 ms |
5296 KB |
Output is correct |
8 |
Correct |
4 ms |
5332 KB |
Output is correct |
9 |
Correct |
4 ms |
5460 KB |
Output is correct |
10 |
Correct |
4 ms |
5424 KB |
Output is correct |
11 |
Correct |
4 ms |
5420 KB |
Output is correct |
12 |
Correct |
6 ms |
5460 KB |
Output is correct |
13 |
Correct |
4 ms |
5460 KB |
Output is correct |
14 |
Correct |
5 ms |
5460 KB |
Output is correct |
15 |
Correct |
3 ms |
4948 KB |
Output is correct |
16 |
Correct |
3 ms |
5028 KB |
Output is correct |
17 |
Correct |
181 ms |
46416 KB |
Output is correct |
18 |
Correct |
168 ms |
51440 KB |
Output is correct |
19 |
Correct |
245 ms |
47356 KB |
Output is correct |
20 |
Correct |
246 ms |
48688 KB |
Output is correct |
21 |
Correct |
241 ms |
48540 KB |
Output is correct |
22 |
Correct |
296 ms |
54792 KB |
Output is correct |
23 |
Correct |
183 ms |
49984 KB |
Output is correct |
24 |
Correct |
182 ms |
55472 KB |
Output is correct |
25 |
Correct |
218 ms |
55504 KB |
Output is correct |
26 |
Correct |
290 ms |
49720 KB |
Output is correct |
27 |
Correct |
147 ms |
53472 KB |
Output is correct |
28 |
Correct |
167 ms |
53004 KB |
Output is correct |
29 |
Correct |
186 ms |
47568 KB |
Output is correct |
30 |
Correct |
111 ms |
47152 KB |
Output is correct |
31 |
Correct |
3 ms |
5028 KB |
Output is correct |
32 |
Correct |
257 ms |
44400 KB |
Output is correct |
33 |
Correct |
272 ms |
44616 KB |
Output is correct |
34 |
Correct |
187 ms |
53844 KB |
Output is correct |
35 |
Correct |
168 ms |
51504 KB |
Output is correct |
36 |
Correct |
238 ms |
47640 KB |
Output is correct |
37 |
Correct |
177 ms |
47536 KB |
Output is correct |
38 |
Correct |
177 ms |
47552 KB |
Output is correct |
39 |
Correct |
109 ms |
47308 KB |
Output is correct |
40 |
Correct |
160 ms |
49144 KB |
Output is correct |
41 |
Correct |
180 ms |
47936 KB |
Output is correct |
42 |
Correct |
3 ms |
5076 KB |
Output is correct |
43 |
Correct |
333 ms |
45632 KB |
Output is correct |
44 |
Correct |
328 ms |
48396 KB |
Output is correct |
45 |
Correct |
307 ms |
48636 KB |
Output is correct |
46 |
Correct |
361 ms |
48552 KB |
Output is correct |
47 |
Correct |
302 ms |
47288 KB |
Output is correct |
48 |
Correct |
211 ms |
52560 KB |
Output is correct |
49 |
Correct |
125 ms |
52212 KB |
Output is correct |
50 |
Correct |
174 ms |
30044 KB |
Output is correct |
51 |
Correct |
328 ms |
48372 KB |
Output is correct |
52 |
Correct |
182 ms |
47908 KB |
Output is correct |
53 |
Correct |
3 ms |
5028 KB |
Output is correct |
54 |
Correct |
3 ms |
4948 KB |
Output is correct |
55 |
Correct |
3 ms |
4948 KB |
Output is correct |
56 |
Correct |
5 ms |
5332 KB |
Output is correct |
57 |
Correct |
4 ms |
5460 KB |
Output is correct |
58 |
Correct |
5 ms |
5332 KB |
Output is correct |
59 |
Correct |
5 ms |
5332 KB |
Output is correct |
60 |
Correct |
4 ms |
5296 KB |
Output is correct |
61 |
Correct |
4 ms |
5460 KB |
Output is correct |
62 |
Correct |
4 ms |
5460 KB |
Output is correct |
63 |
Correct |
4 ms |
5372 KB |
Output is correct |
64 |
Correct |
4 ms |
5460 KB |
Output is correct |
65 |
Correct |
4 ms |
5460 KB |
Output is correct |
66 |
Correct |
4 ms |
5460 KB |
Output is correct |
67 |
Correct |
163 ms |
49172 KB |
Output is correct |
68 |
Correct |
181 ms |
53880 KB |
Output is correct |
69 |
Correct |
217 ms |
47072 KB |
Output is correct |
70 |
Correct |
237 ms |
48176 KB |
Output is correct |
71 |
Correct |
247 ms |
48328 KB |
Output is correct |
72 |
Correct |
297 ms |
54476 KB |
Output is correct |
73 |
Correct |
175 ms |
49712 KB |
Output is correct |
74 |
Correct |
197 ms |
54748 KB |
Output is correct |
75 |
Correct |
220 ms |
54724 KB |
Output is correct |
76 |
Correct |
278 ms |
49232 KB |
Output is correct |
77 |
Correct |
163 ms |
53296 KB |
Output is correct |
78 |
Correct |
166 ms |
52620 KB |
Output is correct |
79 |
Correct |
172 ms |
47024 KB |
Output is correct |
80 |
Correct |
105 ms |
46632 KB |
Output is correct |
81 |
Correct |
247 ms |
46668 KB |
Output is correct |
82 |
Correct |
274 ms |
46768 KB |
Output is correct |
83 |
Correct |
179 ms |
53048 KB |
Output is correct |
84 |
Correct |
157 ms |
51016 KB |
Output is correct |
85 |
Correct |
252 ms |
46672 KB |
Output is correct |
86 |
Correct |
173 ms |
46732 KB |
Output is correct |
87 |
Correct |
177 ms |
46696 KB |
Output is correct |
88 |
Correct |
165 ms |
48048 KB |
Output is correct |
89 |
Correct |
175 ms |
47092 KB |
Output is correct |
90 |
Correct |
311 ms |
47400 KB |
Output is correct |
91 |
Correct |
348 ms |
47432 KB |
Output is correct |
92 |
Correct |
303 ms |
47452 KB |
Output is correct |
93 |
Correct |
391 ms |
47424 KB |
Output is correct |
94 |
Correct |
348 ms |
46040 KB |
Output is correct |
95 |
Correct |
192 ms |
51136 KB |
Output is correct |
96 |
Correct |
116 ms |
50908 KB |
Output is correct |
97 |
Correct |
184 ms |
29244 KB |
Output is correct |
98 |
Correct |
325 ms |
47232 KB |
Output is correct |
99 |
Correct |
181 ms |
46912 KB |
Output is correct |
100 |
Correct |
58 ms |
12024 KB |
Output is correct |
101 |
Correct |
337 ms |
47276 KB |
Output is correct |
102 |
Correct |
319 ms |
40412 KB |
Output is correct |
103 |
Correct |
292 ms |
41832 KB |
Output is correct |
104 |
Correct |
314 ms |
46724 KB |
Output is correct |