#include <bits/stdc++.h>
using namespace std;
const int MAXN=2e5, INF=2e9;
int n, m;
int a[MAXN];
vector<int> adj[MAXN];
int link[MAXN], csize[MAXN], cindex[MAXN];
long long sum[MAXN];
set<pair<int, int> > neighbours[MAXN];
bool compare(int x, int y)
{
return a[x]<a[y];
}
int find(int x)
{
if (x==link[x])
return x;
return link[x]=find(link[x]);
}
void unite(int x, int y, int maxvalue)
{
x=find(x);
y=find(y);
if (neighbours[cindex[x]].size()<neighbours[cindex[y]].size())
swap(x, y);
for (auto t : neighbours[cindex[y]])
if (t.first>maxvalue)
neighbours[cindex[x]].insert(t);
neighbours[cindex[y]].clear();
cindex[y]=cindex[x];
if (csize[x]<csize[y])
swap(x, y);
csize[x]=csize[x]+csize[y];
sum[x]=sum[x]+sum[y];
link[y]=x;
return;
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n >> m;
for (int i=0; i<n; i++)
cin >> a[i];
for (int i=0; i<m; i++)
{
int x, y;
cin >> x >> y;
adj[x-1].push_back(y-1);
adj[y-1].push_back(x-1);
}
int sorted[n];
for (int i=0; i<n; i++)
sorted[i]=i;
sort(sorted, sorted+n, compare);
for (int i=0; i<n; i++)
{
link[i]=i;
csize[i]=1;
sum[i]=a[i];
cindex[i]=i;
for (auto x : adj[i])
neighbours[i].insert({a[x], x});
}
map<int, vector<int> > byvalue;
for (int i=0; i<n; i++)
byvalue[a[i]].push_back(i);
bool possible[n];
for (int i=0; i<n; i++)
possible[i]=true;
queue<int> q;
vector<int> todelete[n];
for (auto k : byvalue)
{
for (auto x : k.second)
{
for (auto y : adj[x])
if (a[y]<=a[x] && find(x)!=find(y))
unite(x, y, a[x]);
}
for (auto x : k.second)
{
int leader=find(x);
while (!neighbours[cindex[leader]].empty() && (*neighbours[cindex[leader]].begin()).first<=a[x])
neighbours[cindex[leader]].erase(neighbours[cindex[leader]].begin());
if (neighbours[cindex[leader]].empty())
continue;
auto it=neighbours[cindex[leader]].begin();
if (sum[leader]>=(*it).first)
{
todelete[(*it).second].push_back(x);
continue;
}
possible[x]=false;
q.push(x);
while (!q.empty())
{
int y=q.front();
q.pop();
for (auto t : todelete[y])
if (a[t]<=a[x] && possible[t])
{
possible[t]=false;
q.push(t);
}
}
}
}
for (int i=0; i<n; i++)
cout << possible[i];
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
19036 KB |
Output is correct |
2 |
Correct |
2 ms |
19036 KB |
Output is correct |
3 |
Correct |
2 ms |
19036 KB |
Output is correct |
4 |
Correct |
4 ms |
19548 KB |
Output is correct |
5 |
Correct |
4 ms |
19604 KB |
Output is correct |
6 |
Correct |
4 ms |
19292 KB |
Output is correct |
7 |
Correct |
4 ms |
19320 KB |
Output is correct |
8 |
Correct |
4 ms |
19292 KB |
Output is correct |
9 |
Correct |
3 ms |
19288 KB |
Output is correct |
10 |
Correct |
3 ms |
19548 KB |
Output is correct |
11 |
Correct |
4 ms |
19548 KB |
Output is correct |
12 |
Correct |
3 ms |
19548 KB |
Output is correct |
13 |
Correct |
4 ms |
19292 KB |
Output is correct |
14 |
Correct |
3 ms |
19292 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
19036 KB |
Output is correct |
2 |
Correct |
3 ms |
19036 KB |
Output is correct |
3 |
Correct |
189 ms |
72008 KB |
Output is correct |
4 |
Correct |
119 ms |
51396 KB |
Output is correct |
5 |
Correct |
220 ms |
70400 KB |
Output is correct |
6 |
Correct |
229 ms |
66812 KB |
Output is correct |
7 |
Correct |
223 ms |
72144 KB |
Output is correct |
8 |
Correct |
181 ms |
51664 KB |
Output is correct |
9 |
Correct |
151 ms |
66992 KB |
Output is correct |
10 |
Correct |
229 ms |
67680 KB |
Output is correct |
11 |
Correct |
205 ms |
52208 KB |
Output is correct |
12 |
Correct |
182 ms |
51572 KB |
Output is correct |
13 |
Correct |
92 ms |
51488 KB |
Output is correct |
14 |
Correct |
110 ms |
51508 KB |
Output is correct |
15 |
Correct |
175 ms |
71888 KB |
Output is correct |
16 |
Correct |
124 ms |
71336 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
19036 KB |
Output is correct |
2 |
Correct |
356 ms |
71848 KB |
Output is correct |
3 |
Correct |
357 ms |
67116 KB |
Output is correct |
4 |
Correct |
114 ms |
51548 KB |
Output is correct |
5 |
Correct |
108 ms |
51468 KB |
Output is correct |
6 |
Correct |
363 ms |
71940 KB |
Output is correct |
7 |
Correct |
176 ms |
72080 KB |
Output is correct |
8 |
Correct |
193 ms |
72020 KB |
Output is correct |
9 |
Correct |
144 ms |
71548 KB |
Output is correct |
10 |
Correct |
157 ms |
61644 KB |
Output is correct |
11 |
Correct |
201 ms |
52044 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
19036 KB |
Output is correct |
2 |
Correct |
243 ms |
51364 KB |
Output is correct |
3 |
Correct |
250 ms |
51080 KB |
Output is correct |
4 |
Correct |
250 ms |
51532 KB |
Output is correct |
5 |
Correct |
158 ms |
51364 KB |
Output is correct |
6 |
Correct |
151 ms |
50440 KB |
Output is correct |
7 |
Correct |
198 ms |
52252 KB |
Output is correct |
8 |
Correct |
84 ms |
51304 KB |
Output is correct |
9 |
Correct |
142 ms |
45564 KB |
Output is correct |
10 |
Correct |
148 ms |
51720 KB |
Output is correct |
11 |
Correct |
174 ms |
51108 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
19036 KB |
Output is correct |
2 |
Correct |
2 ms |
19036 KB |
Output is correct |
3 |
Correct |
2 ms |
19036 KB |
Output is correct |
4 |
Correct |
4 ms |
19548 KB |
Output is correct |
5 |
Correct |
4 ms |
19604 KB |
Output is correct |
6 |
Correct |
4 ms |
19292 KB |
Output is correct |
7 |
Correct |
4 ms |
19320 KB |
Output is correct |
8 |
Correct |
4 ms |
19292 KB |
Output is correct |
9 |
Correct |
3 ms |
19288 KB |
Output is correct |
10 |
Correct |
3 ms |
19548 KB |
Output is correct |
11 |
Correct |
4 ms |
19548 KB |
Output is correct |
12 |
Correct |
3 ms |
19548 KB |
Output is correct |
13 |
Correct |
4 ms |
19292 KB |
Output is correct |
14 |
Correct |
3 ms |
19292 KB |
Output is correct |
15 |
Correct |
2 ms |
19036 KB |
Output is correct |
16 |
Correct |
3 ms |
19036 KB |
Output is correct |
17 |
Correct |
189 ms |
72008 KB |
Output is correct |
18 |
Correct |
119 ms |
51396 KB |
Output is correct |
19 |
Correct |
220 ms |
70400 KB |
Output is correct |
20 |
Correct |
229 ms |
66812 KB |
Output is correct |
21 |
Correct |
223 ms |
72144 KB |
Output is correct |
22 |
Correct |
181 ms |
51664 KB |
Output is correct |
23 |
Correct |
151 ms |
66992 KB |
Output is correct |
24 |
Correct |
229 ms |
67680 KB |
Output is correct |
25 |
Correct |
205 ms |
52208 KB |
Output is correct |
26 |
Correct |
182 ms |
51572 KB |
Output is correct |
27 |
Correct |
92 ms |
51488 KB |
Output is correct |
28 |
Correct |
110 ms |
51508 KB |
Output is correct |
29 |
Correct |
175 ms |
71888 KB |
Output is correct |
30 |
Correct |
124 ms |
71336 KB |
Output is correct |
31 |
Correct |
2 ms |
19036 KB |
Output is correct |
32 |
Correct |
356 ms |
71848 KB |
Output is correct |
33 |
Correct |
357 ms |
67116 KB |
Output is correct |
34 |
Correct |
114 ms |
51548 KB |
Output is correct |
35 |
Correct |
108 ms |
51468 KB |
Output is correct |
36 |
Correct |
363 ms |
71940 KB |
Output is correct |
37 |
Correct |
176 ms |
72080 KB |
Output is correct |
38 |
Correct |
193 ms |
72020 KB |
Output is correct |
39 |
Correct |
144 ms |
71548 KB |
Output is correct |
40 |
Correct |
157 ms |
61644 KB |
Output is correct |
41 |
Correct |
201 ms |
52044 KB |
Output is correct |
42 |
Correct |
3 ms |
19036 KB |
Output is correct |
43 |
Correct |
243 ms |
51364 KB |
Output is correct |
44 |
Correct |
250 ms |
51080 KB |
Output is correct |
45 |
Correct |
250 ms |
51532 KB |
Output is correct |
46 |
Correct |
158 ms |
51364 KB |
Output is correct |
47 |
Correct |
151 ms |
50440 KB |
Output is correct |
48 |
Correct |
198 ms |
52252 KB |
Output is correct |
49 |
Correct |
84 ms |
51304 KB |
Output is correct |
50 |
Correct |
142 ms |
45564 KB |
Output is correct |
51 |
Correct |
148 ms |
51720 KB |
Output is correct |
52 |
Correct |
174 ms |
51108 KB |
Output is correct |
53 |
Correct |
2 ms |
19032 KB |
Output is correct |
54 |
Correct |
2 ms |
19040 KB |
Output is correct |
55 |
Correct |
2 ms |
19056 KB |
Output is correct |
56 |
Correct |
4 ms |
19544 KB |
Output is correct |
57 |
Correct |
4 ms |
19548 KB |
Output is correct |
58 |
Correct |
3 ms |
19292 KB |
Output is correct |
59 |
Correct |
3 ms |
19292 KB |
Output is correct |
60 |
Correct |
4 ms |
19292 KB |
Output is correct |
61 |
Correct |
3 ms |
19292 KB |
Output is correct |
62 |
Correct |
3 ms |
19548 KB |
Output is correct |
63 |
Correct |
3 ms |
19632 KB |
Output is correct |
64 |
Correct |
3 ms |
19548 KB |
Output is correct |
65 |
Correct |
3 ms |
19300 KB |
Output is correct |
66 |
Correct |
3 ms |
19292 KB |
Output is correct |
67 |
Correct |
139 ms |
71964 KB |
Output is correct |
68 |
Correct |
95 ms |
51400 KB |
Output is correct |
69 |
Correct |
178 ms |
70260 KB |
Output is correct |
70 |
Correct |
190 ms |
66900 KB |
Output is correct |
71 |
Correct |
193 ms |
72020 KB |
Output is correct |
72 |
Correct |
141 ms |
51660 KB |
Output is correct |
73 |
Correct |
125 ms |
66732 KB |
Output is correct |
74 |
Correct |
208 ms |
67524 KB |
Output is correct |
75 |
Correct |
160 ms |
52296 KB |
Output is correct |
76 |
Correct |
153 ms |
51724 KB |
Output is correct |
77 |
Correct |
83 ms |
51456 KB |
Output is correct |
78 |
Correct |
87 ms |
51396 KB |
Output is correct |
79 |
Correct |
141 ms |
72016 KB |
Output is correct |
80 |
Correct |
115 ms |
71504 KB |
Output is correct |
81 |
Correct |
299 ms |
71760 KB |
Output is correct |
82 |
Correct |
294 ms |
67116 KB |
Output is correct |
83 |
Correct |
85 ms |
51652 KB |
Output is correct |
84 |
Correct |
82 ms |
51396 KB |
Output is correct |
85 |
Correct |
305 ms |
72020 KB |
Output is correct |
86 |
Correct |
195 ms |
72072 KB |
Output is correct |
87 |
Correct |
164 ms |
72016 KB |
Output is correct |
88 |
Correct |
124 ms |
61644 KB |
Output is correct |
89 |
Correct |
146 ms |
51868 KB |
Output is correct |
90 |
Correct |
220 ms |
51256 KB |
Output is correct |
91 |
Correct |
198 ms |
51024 KB |
Output is correct |
92 |
Correct |
219 ms |
51536 KB |
Output is correct |
93 |
Correct |
129 ms |
51388 KB |
Output is correct |
94 |
Correct |
153 ms |
50636 KB |
Output is correct |
95 |
Correct |
152 ms |
52292 KB |
Output is correct |
96 |
Correct |
83 ms |
51144 KB |
Output is correct |
97 |
Correct |
130 ms |
45648 KB |
Output is correct |
98 |
Correct |
136 ms |
51656 KB |
Output is correct |
99 |
Correct |
138 ms |
51104 KB |
Output is correct |
100 |
Correct |
95 ms |
40276 KB |
Output is correct |
101 |
Correct |
304 ms |
67152 KB |
Output is correct |
102 |
Correct |
111 ms |
48580 KB |
Output is correct |
103 |
Correct |
306 ms |
62076 KB |
Output is correct |
104 |
Correct |
332 ms |
71248 KB |
Output is correct |