#include <bits/stdc++.h>
using namespace std;
// ordered set whith s.order_of_key(x) method, which returns rank of element x in set s
/*
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
typedef tree<int, null_type, less_equal<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
*/
// pair printing
template <class T, class U>
ostream& operator<<(ostream& out, const pair<T, U> &par) {out << "(" << par.first << "; " << par.second << ")"; return out;}
// set printing
template <class T>
ostream& operator<<(ostream& out, const set<T> &cont) { out << "{"; for(const auto &x:cont) out << x << ", "; out << "}"; return out; }
// map printing
template <class T, class U>
ostream& operator<<(ostream& out, const map<T, U> &cont) {out << "{"; for(const auto &x:cont) out << x << ", "; out << "}"; return out; }
// unordered_set printing
template <class T>
ostream& operator<<(ostream& out, const unordered_set<T> &cont) {out << "{";for(const auto &x:cont) out << x << ", ";out << "}";return out;}
// unordered_map printing
template <class T, class U>
ostream& operator<<(ostream& out, const unordered_map<T, U> &cont) {out << "{";for(const auto &x:cont) out << x << ", ";out << "}";return out;}
// vector printing
template<class T>
ostream& operator<<(ostream& out, const vector<T> &cont){ out << "["; for (const auto &x:cont) out << x << ", "; out << "]"; return out;}
#define print(x) cout << (x) << endl;
#define dmp(x) cerr << #x << " = " << x << endl
#define dmpn(x) cerr << #x << " = " << x << "; "
#define dmpl(x) cerr << "Line " << __LINE__ << ": " << #x << " = " << x << endl
#define int long long
using ll = long long;
using ld = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
#define pb push_back
#define ff first
#define ss second
#define all(x) begin(x), end(x)
#define sz(x) (int)x.size()
#define contains(s,x) ((s).find(x) != (s).end())
const int MOD = 998244353;
int N, M;
vector<vector<int>> g;
vector<int> values;
struct UnionFind {
vector<int> heights, parents, sums;
vector<vector<int>> possibles;
UnionFind() {
heights.assign(N, 1);
parents.assign(N, -2);
for (int i = 0; i < N; i++) {
sums.pb(values[i]);
possibles.pb({ i });
}
}
int find_(int v) {
if (parents[v] == -1) return v;
if (parents[v] == -2) return -1;
return find_(parents[v]);
}
void union_(int u, int v, int merge_possibles) {
int ru = find_(u), rv = find_(v);
// adding rv to ru - root is ru
if (heights[ru] > heights[rv]) {
parents[rv] = ru;
sums[ru] += sums[rv];
if (merge_possibles) {
if (sz(possibles[rv]) > sz(possibles[ru])) swap(possibles[ru], possibles[rv]);
for (int x : possibles[rv]) possibles[ru].pb(x);
// possibles for components should be always in root, which is ru, but our possibles are in rv
} else {
swap(possibles[ru], possibles[rv]);
}
// adding ru to rv - root is rv
} else {
parents[ru] = rv;
sums[rv] += sums[ru];
if (merge_possibles) {
if (sz(possibles[ru]) > sz(possibles[rv])) swap(possibles[ru], possibles[rv]);
for (int x : possibles[ru]) possibles[rv].pb(x);
}
}
if (heights[ru] == heights[rv]) heights[rv]++;
}
};
void solve() {
cin >> N >> M;
g.assign(N, {});
values.assign(N, 0);
vector<pii> sorted_vertices;
for (int i = 0; i < N; i++) {
cin >> values[i];
sorted_vertices.pb({ values[i], i });
}
sort(all(sorted_vertices));
for (int i = 0; i < M; i++) {
int u, v;
cin >> u >> v;
u--;
v--;
g[u].pb(v);
g[v].pb(u);
}
UnionFind uf;
for (pii pr : sorted_vertices) {
int v = pr.ss;
uf.parents[v] = -1;
set<int> components;
for (int u : g[v]) {
int ru = uf.find_(u);
if (ru != -1) components.insert(ru);
}
// dmpn(v); dmp(components);
for (int comp : components) {
bool merge_possibles = (uf.sums[comp] >= values[v]);
// dmpn(comp); dmpn(uf.sums[comp]); dmp(values[v]);
uf.union_(comp, v, merge_possibles);
// dmp(uf.possibles[root]);
}
}
int root = uf.find_(0);
set<int> final_possibles(all(uf.possibles[root]));
for (int i = 0; i < N; i++) {
if (contains(final_possibles, i)) cout << 1;
else cout << 0;
}
cout << endl;
}
int32_t main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t = 1;
// cin >> t;
while (t--) {
solve();
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
600 KB |
Output is correct |
4 |
Correct |
1 ms |
604 KB |
Output is correct |
5 |
Correct |
2 ms |
860 KB |
Output is correct |
6 |
Correct |
2 ms |
604 KB |
Output is correct |
7 |
Correct |
1 ms |
604 KB |
Output is correct |
8 |
Correct |
1 ms |
604 KB |
Output is correct |
9 |
Correct |
1 ms |
600 KB |
Output is correct |
10 |
Correct |
2 ms |
860 KB |
Output is correct |
11 |
Correct |
2 ms |
860 KB |
Output is correct |
12 |
Correct |
1 ms |
860 KB |
Output is correct |
13 |
Correct |
1 ms |
604 KB |
Output is correct |
14 |
Correct |
1 ms |
604 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
352 KB |
Output is correct |
3 |
Correct |
177 ms |
41924 KB |
Output is correct |
4 |
Correct |
139 ms |
40608 KB |
Output is correct |
5 |
Correct |
236 ms |
46980 KB |
Output is correct |
6 |
Correct |
218 ms |
46920 KB |
Output is correct |
7 |
Correct |
188 ms |
48700 KB |
Output is correct |
8 |
Correct |
199 ms |
49580 KB |
Output is correct |
9 |
Correct |
181 ms |
49048 KB |
Output is correct |
10 |
Correct |
206 ms |
48780 KB |
Output is correct |
11 |
Correct |
174 ms |
47868 KB |
Output is correct |
12 |
Correct |
166 ms |
40952 KB |
Output is correct |
13 |
Correct |
121 ms |
40628 KB |
Output is correct |
14 |
Correct |
170 ms |
40584 KB |
Output is correct |
15 |
Correct |
190 ms |
49604 KB |
Output is correct |
16 |
Correct |
153 ms |
46012 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
212 ms |
44900 KB |
Output is correct |
3 |
Correct |
265 ms |
47960 KB |
Output is correct |
4 |
Correct |
170 ms |
48064 KB |
Output is correct |
5 |
Correct |
113 ms |
39436 KB |
Output is correct |
6 |
Correct |
244 ms |
47628 KB |
Output is correct |
7 |
Correct |
182 ms |
48876 KB |
Output is correct |
8 |
Correct |
187 ms |
48948 KB |
Output is correct |
9 |
Correct |
140 ms |
47860 KB |
Output is correct |
10 |
Correct |
145 ms |
41920 KB |
Output is correct |
11 |
Correct |
169 ms |
38104 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
244 ms |
42004 KB |
Output is correct |
3 |
Correct |
237 ms |
47680 KB |
Output is correct |
4 |
Correct |
284 ms |
50928 KB |
Output is correct |
5 |
Correct |
254 ms |
54200 KB |
Output is correct |
6 |
Correct |
286 ms |
51160 KB |
Output is correct |
7 |
Correct |
178 ms |
48928 KB |
Output is correct |
8 |
Correct |
114 ms |
43596 KB |
Output is correct |
9 |
Correct |
107 ms |
23308 KB |
Output is correct |
10 |
Correct |
181 ms |
47040 KB |
Output is correct |
11 |
Correct |
153 ms |
39292 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
600 KB |
Output is correct |
4 |
Correct |
1 ms |
604 KB |
Output is correct |
5 |
Correct |
2 ms |
860 KB |
Output is correct |
6 |
Correct |
2 ms |
604 KB |
Output is correct |
7 |
Correct |
1 ms |
604 KB |
Output is correct |
8 |
Correct |
1 ms |
604 KB |
Output is correct |
9 |
Correct |
1 ms |
600 KB |
Output is correct |
10 |
Correct |
2 ms |
860 KB |
Output is correct |
11 |
Correct |
2 ms |
860 KB |
Output is correct |
12 |
Correct |
1 ms |
860 KB |
Output is correct |
13 |
Correct |
1 ms |
604 KB |
Output is correct |
14 |
Correct |
1 ms |
604 KB |
Output is correct |
15 |
Correct |
0 ms |
344 KB |
Output is correct |
16 |
Correct |
0 ms |
352 KB |
Output is correct |
17 |
Correct |
177 ms |
41924 KB |
Output is correct |
18 |
Correct |
139 ms |
40608 KB |
Output is correct |
19 |
Correct |
236 ms |
46980 KB |
Output is correct |
20 |
Correct |
218 ms |
46920 KB |
Output is correct |
21 |
Correct |
188 ms |
48700 KB |
Output is correct |
22 |
Correct |
199 ms |
49580 KB |
Output is correct |
23 |
Correct |
181 ms |
49048 KB |
Output is correct |
24 |
Correct |
206 ms |
48780 KB |
Output is correct |
25 |
Correct |
174 ms |
47868 KB |
Output is correct |
26 |
Correct |
166 ms |
40952 KB |
Output is correct |
27 |
Correct |
121 ms |
40628 KB |
Output is correct |
28 |
Correct |
170 ms |
40584 KB |
Output is correct |
29 |
Correct |
190 ms |
49604 KB |
Output is correct |
30 |
Correct |
153 ms |
46012 KB |
Output is correct |
31 |
Correct |
1 ms |
348 KB |
Output is correct |
32 |
Correct |
212 ms |
44900 KB |
Output is correct |
33 |
Correct |
265 ms |
47960 KB |
Output is correct |
34 |
Correct |
170 ms |
48064 KB |
Output is correct |
35 |
Correct |
113 ms |
39436 KB |
Output is correct |
36 |
Correct |
244 ms |
47628 KB |
Output is correct |
37 |
Correct |
182 ms |
48876 KB |
Output is correct |
38 |
Correct |
187 ms |
48948 KB |
Output is correct |
39 |
Correct |
140 ms |
47860 KB |
Output is correct |
40 |
Correct |
145 ms |
41920 KB |
Output is correct |
41 |
Correct |
169 ms |
38104 KB |
Output is correct |
42 |
Correct |
0 ms |
344 KB |
Output is correct |
43 |
Correct |
244 ms |
42004 KB |
Output is correct |
44 |
Correct |
237 ms |
47680 KB |
Output is correct |
45 |
Correct |
284 ms |
50928 KB |
Output is correct |
46 |
Correct |
254 ms |
54200 KB |
Output is correct |
47 |
Correct |
286 ms |
51160 KB |
Output is correct |
48 |
Correct |
178 ms |
48928 KB |
Output is correct |
49 |
Correct |
114 ms |
43596 KB |
Output is correct |
50 |
Correct |
107 ms |
23308 KB |
Output is correct |
51 |
Correct |
181 ms |
47040 KB |
Output is correct |
52 |
Correct |
153 ms |
39292 KB |
Output is correct |
53 |
Correct |
0 ms |
348 KB |
Output is correct |
54 |
Correct |
0 ms |
348 KB |
Output is correct |
55 |
Correct |
0 ms |
348 KB |
Output is correct |
56 |
Correct |
2 ms |
604 KB |
Output is correct |
57 |
Correct |
2 ms |
860 KB |
Output is correct |
58 |
Correct |
2 ms |
860 KB |
Output is correct |
59 |
Correct |
2 ms |
604 KB |
Output is correct |
60 |
Correct |
2 ms |
604 KB |
Output is correct |
61 |
Correct |
1 ms |
620 KB |
Output is correct |
62 |
Correct |
2 ms |
860 KB |
Output is correct |
63 |
Correct |
2 ms |
860 KB |
Output is correct |
64 |
Correct |
2 ms |
860 KB |
Output is correct |
65 |
Correct |
1 ms |
604 KB |
Output is correct |
66 |
Correct |
1 ms |
680 KB |
Output is correct |
67 |
Correct |
165 ms |
46948 KB |
Output is correct |
68 |
Correct |
148 ms |
39348 KB |
Output is correct |
69 |
Correct |
182 ms |
46088 KB |
Output is correct |
70 |
Correct |
190 ms |
49092 KB |
Output is correct |
71 |
Correct |
194 ms |
47660 KB |
Output is correct |
72 |
Correct |
173 ms |
48060 KB |
Output is correct |
73 |
Correct |
161 ms |
50368 KB |
Output is correct |
74 |
Correct |
197 ms |
48468 KB |
Output is correct |
75 |
Correct |
159 ms |
47804 KB |
Output is correct |
76 |
Correct |
160 ms |
39356 KB |
Output is correct |
77 |
Correct |
122 ms |
38792 KB |
Output is correct |
78 |
Correct |
138 ms |
39360 KB |
Output is correct |
79 |
Correct |
191 ms |
49496 KB |
Output is correct |
80 |
Correct |
142 ms |
46520 KB |
Output is correct |
81 |
Correct |
222 ms |
47776 KB |
Output is correct |
82 |
Correct |
219 ms |
47804 KB |
Output is correct |
83 |
Correct |
171 ms |
47808 KB |
Output is correct |
84 |
Correct |
132 ms |
39332 KB |
Output is correct |
85 |
Correct |
227 ms |
47804 KB |
Output is correct |
86 |
Correct |
193 ms |
47800 KB |
Output is correct |
87 |
Correct |
188 ms |
47292 KB |
Output is correct |
88 |
Correct |
137 ms |
42568 KB |
Output is correct |
89 |
Correct |
190 ms |
37956 KB |
Output is correct |
90 |
Correct |
246 ms |
45696 KB |
Output is correct |
91 |
Correct |
245 ms |
47808 KB |
Output is correct |
92 |
Correct |
262 ms |
50456 KB |
Output is correct |
93 |
Correct |
260 ms |
53948 KB |
Output is correct |
94 |
Correct |
230 ms |
50092 KB |
Output is correct |
95 |
Correct |
169 ms |
49528 KB |
Output is correct |
96 |
Correct |
106 ms |
42944 KB |
Output is correct |
97 |
Correct |
101 ms |
23496 KB |
Output is correct |
98 |
Correct |
208 ms |
48816 KB |
Output is correct |
99 |
Correct |
154 ms |
39852 KB |
Output is correct |
100 |
Correct |
33 ms |
7244 KB |
Output is correct |
101 |
Correct |
245 ms |
46456 KB |
Output is correct |
102 |
Correct |
243 ms |
41664 KB |
Output is correct |
103 |
Correct |
206 ms |
40284 KB |
Output is correct |
104 |
Correct |
270 ms |
45200 KB |
Output is correct |