#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;
}
vector<bool> brute()
{
vector<bool> possible(n, 0);
for (int i=0; i<n; i++)
{
priority_queue<pair<int, int> > pq;
pq.push({-a[i], i});
long long sum=0;
bool visited[n]={false};
while (!pq.empty())
{
int x=pq.top().second;
pq.pop();
if (x!=i && sum<a[x])
continue;
if (visited[x])
continue;
visited[x]=true;
sum=sum+a[x];
for (auto y : adj[x])
if (!visited[y])
pq.push({-a[y], y});
}
if (*min_element(visited, visited+n)==1)
possible[i]=true;
}
return possible;
}
int main()
{
//ios_base::sync_with_stdio(0);
//cin.tie(0);
srand(time(0));
int tc=0;
int K=10;
//cin >> tc;
//while (true)
{
/*n=rand()%K+1;
m=n-1+rand()%K;
if (m>n*(n-1)/2)
continue;
tc=tc+1;
cout << "tc " << tc << '\n';
for (int i=0; i<n; i++)
{
adj[i].clear();
neighbours[i].clear();
}
for (int i=0; i<n; i++)
a[i]=rand()%K+1;
set<pair<int, int> > used;
for (int x=1; x<n; x++)
{
int y=rand()%x;
adj[x].push_back(y);
adj[y].push_back(x);
used.insert({x, y});
}
while (used.size()<m)
{
int x=rand()%n;
int y=rand()%n;
if (x==y)
continue;
if (used.count({x, y}) || used.count({y, x}))
continue;
adj[x].push_back(y);
adj[y].push_back(x);
used.insert({x, y});
}*/
/*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].push_back(y);
adj[y].push_back(x);
}*/
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();
//cout << "x sum min " << x << ' ' << sum[leader] << ' ' << (*it).first << '\n';
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];
/*auto br=brute();
bool flop=false;
for (int i=0; i<n; i++)
if (possible[i]!=br[i])
flop=true;
if (flop)
{
cout << "FLOP\n";
cout << n << ' ' << m << '\n';
for (int i=0; i<n; i++)
cout << a[i] << ' ';
cout << '\n';
for (auto edge : used)
cout << edge.first << ' ' << edge.second << '\n';
for (int i=0; i<n; i++)
cout << possible[i];
cout << '\n';
for (int i=0; i<n; i++)
cout << br[i];
cout << '\n';
//break;
}*/
}
return 0;
}
/*#include <bits/stdc++.h>
using namespace std;
const int MAXN=2e5, INF=2e9;
int n, m;
int a[MAXN];
vector<int> adj[MAXN];
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);
}
set<int> values;
for (int i=0; i<n; i++)
values.insert(-a[i]);
bool possible[n]={false};
queue<int> q;
for (auto kt : values)
{
int k=-kt;
bool visited[n]={false};
for (int i=0; i<n; i++)
{
if (a[i]!=k || visited[i])
continue;
visited[i]=true;
long long sum=a[i];
q.push(i);
vector<int> nodes;
nodes.push_back(i);
int minsum=INF;
while (!q.empty())
{
int x=q.front();
q.pop();
for (auto y : adj[x])
{
if (a[y]<=k && !visited[y])
{
visited[y]=true;
sum=sum+a[y];
q.push(y);
if (a[y]==k)
nodes.push_back(y);
}
if (a[y]>k && possible[y])
minsum=min(minsum, a[y]);
}
}
if (minsum>sum && k!=-(*values.begin()))
continue;
for (auto x : nodes)
possible[x]=true;
}
}
for (int i=0; i<n; i++)
cout << possible[i];
return 0;
}*/
/*#include <bits/stdc++.h>
using namespace std;
const int MAXN=300000;
int n, m;
int a[MAXN];
vector<int> adj[MAXN];
long long prefix[MAXN];
bool compare(int x, int y)
{
return a[x]>a[y];
}
long long sum(int l, int r)
{
if (l==0)
return prefix[r];
return prefix[r]-prefix[l-1];
}
vector<bool> brute()
{
vector<bool> possible(n, 0);
for (int i=0; i<n; i++)
{
priority_queue<pair<int, int> > pq;
pq.push({-a[i], i});
long long sum=0;
bool visited[n]={false};
while (!pq.empty())
{
int x=pq.top().second;
pq.pop();
if (x!=i && sum<a[x])
continue;
if (visited[x])
continue;
visited[x]=true;
sum=sum+a[x];
for (auto y : adj[x])
if (!visited[y])
pq.push({-a[y], y});
}
if (*min_element(visited, visited+n)==1)
possible[i]=true;
}
return possible;
}
int main()
{
//ios_base::sync_with_stdio(0);
//cin.tie(0);
srand(time(0));
int tc=0;
//cin >> tc;
//while (true)
{
tc=tc+1;
cout << "tc " << tc << '\n';
n=rand()%100+1, m=0;
for (int i=0; i<n; i++)
a[i]=rand()%100000+1;
//for (int i=0; i<n; i++)
// cout << a[i] << ' ';
//cout << '\n';
cin >> n >> m;
for (int i=0; i<n; i++)
cin >> a[i];
for (int i=0; i<n; i++)
adj[i].clear();
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);
int possible[n]={false};
possible[sorted[0]]=true;
set<int> larger;
larger.insert(sorted[0]);
prefix[0]=a[0];
for (int i=1; i<=n; i++)
prefix[i]=prefix[i-1]+a[i];
for (int i=1; i<n; i++)
{
int x=sorted[i];
int l=-1, r=n;
auto it=larger.lower_bound(x);
if (it!=larger.end())
r=*it;
if (it!=larger.begin())
{
it--;
l=*it;
}
if (l!=-1 && possible[l] && sum(l+1, r-1)>=a[l])
possible[x]=true;
if (r!=n && possible[r] && sum(l+1, r-1)>=a[r])
possible[x]=true;
larger.insert(x);
}
for (int i=0; i<n; i++)
cout << possible[i];
auto br=brute();
bool flop=false;
for (int i=0; i<n; i++)
if (possible[i]!=br[i])
flop=true;
if (flop)
{
cout << "FLOP\n";
cout << n << '\n';
for (int i=0; i<n; i++)
cout << a[i] << ' ';
cout << '\n';
for (int i=0; i<n; i++)
cout << possible[i];
cout << '\n';
for (int i=0; i<n; i++)
cout << br[i];
cout << '\n';
break;
}
}
return 0;
}*/
Compilation message
island.cpp: In function 'int main()':
island.cpp:82:9: warning: unused variable 'tc' [-Wunused-variable]
82 | int tc=0;
| ^~
island.cpp:83:9: warning: unused variable 'K' [-Wunused-variable]
83 | int K=10;
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
19032 KB |
Output is correct |
2 |
Correct |
2 ms |
19036 KB |
Output is correct |
3 |
Correct |
2 ms |
19032 KB |
Output is correct |
4 |
Correct |
4 ms |
19560 KB |
Output is correct |
5 |
Correct |
4 ms |
19548 KB |
Output is correct |
6 |
Correct |
3 ms |
19292 KB |
Output is correct |
7 |
Correct |
4 ms |
19524 KB |
Output is correct |
8 |
Correct |
4 ms |
19292 KB |
Output is correct |
9 |
Correct |
3 ms |
19304 KB |
Output is correct |
10 |
Correct |
5 ms |
19804 KB |
Output is correct |
11 |
Correct |
4 ms |
19644 KB |
Output is correct |
12 |
Correct |
4 ms |
19800 KB |
Output is correct |
13 |
Correct |
3 ms |
19292 KB |
Output is correct |
14 |
Correct |
4 ms |
19292 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
19036 KB |
Output is correct |
2 |
Correct |
2 ms |
19036 KB |
Output is correct |
3 |
Correct |
221 ms |
71868 KB |
Output is correct |
4 |
Correct |
152 ms |
51464 KB |
Output is correct |
5 |
Correct |
281 ms |
70228 KB |
Output is correct |
6 |
Correct |
235 ms |
66896 KB |
Output is correct |
7 |
Correct |
268 ms |
72016 KB |
Output is correct |
8 |
Correct |
207 ms |
51656 KB |
Output is correct |
9 |
Correct |
238 ms |
66676 KB |
Output is correct |
10 |
Correct |
249 ms |
67544 KB |
Output is correct |
11 |
Correct |
222 ms |
52324 KB |
Output is correct |
12 |
Correct |
218 ms |
51724 KB |
Output is correct |
13 |
Correct |
136 ms |
51396 KB |
Output is correct |
14 |
Correct |
140 ms |
51400 KB |
Output is correct |
15 |
Correct |
210 ms |
72016 KB |
Output is correct |
16 |
Correct |
167 ms |
71248 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
19032 KB |
Output is correct |
2 |
Correct |
381 ms |
71760 KB |
Output is correct |
3 |
Correct |
360 ms |
71324 KB |
Output is correct |
4 |
Correct |
169 ms |
56008 KB |
Output is correct |
5 |
Correct |
148 ms |
54468 KB |
Output is correct |
6 |
Correct |
428 ms |
76368 KB |
Output is correct |
7 |
Correct |
247 ms |
76368 KB |
Output is correct |
8 |
Correct |
229 ms |
76480 KB |
Output is correct |
9 |
Correct |
172 ms |
75176 KB |
Output is correct |
10 |
Correct |
209 ms |
65276 KB |
Output is correct |
11 |
Correct |
213 ms |
54868 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
19036 KB |
Output is correct |
2 |
Correct |
274 ms |
51072 KB |
Output is correct |
3 |
Correct |
269 ms |
51056 KB |
Output is correct |
4 |
Correct |
301 ms |
55908 KB |
Output is correct |
5 |
Correct |
234 ms |
56020 KB |
Output is correct |
6 |
Correct |
200 ms |
54768 KB |
Output is correct |
7 |
Correct |
218 ms |
56596 KB |
Output is correct |
8 |
Correct |
139 ms |
54476 KB |
Output is correct |
9 |
Correct |
169 ms |
48184 KB |
Output is correct |
10 |
Correct |
207 ms |
54496 KB |
Output is correct |
11 |
Correct |
202 ms |
54072 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
19032 KB |
Output is correct |
2 |
Correct |
2 ms |
19036 KB |
Output is correct |
3 |
Correct |
2 ms |
19032 KB |
Output is correct |
4 |
Correct |
4 ms |
19560 KB |
Output is correct |
5 |
Correct |
4 ms |
19548 KB |
Output is correct |
6 |
Correct |
3 ms |
19292 KB |
Output is correct |
7 |
Correct |
4 ms |
19524 KB |
Output is correct |
8 |
Correct |
4 ms |
19292 KB |
Output is correct |
9 |
Correct |
3 ms |
19304 KB |
Output is correct |
10 |
Correct |
5 ms |
19804 KB |
Output is correct |
11 |
Correct |
4 ms |
19644 KB |
Output is correct |
12 |
Correct |
4 ms |
19800 KB |
Output is correct |
13 |
Correct |
3 ms |
19292 KB |
Output is correct |
14 |
Correct |
4 ms |
19292 KB |
Output is correct |
15 |
Correct |
2 ms |
19036 KB |
Output is correct |
16 |
Correct |
2 ms |
19036 KB |
Output is correct |
17 |
Correct |
221 ms |
71868 KB |
Output is correct |
18 |
Correct |
152 ms |
51464 KB |
Output is correct |
19 |
Correct |
281 ms |
70228 KB |
Output is correct |
20 |
Correct |
235 ms |
66896 KB |
Output is correct |
21 |
Correct |
268 ms |
72016 KB |
Output is correct |
22 |
Correct |
207 ms |
51656 KB |
Output is correct |
23 |
Correct |
238 ms |
66676 KB |
Output is correct |
24 |
Correct |
249 ms |
67544 KB |
Output is correct |
25 |
Correct |
222 ms |
52324 KB |
Output is correct |
26 |
Correct |
218 ms |
51724 KB |
Output is correct |
27 |
Correct |
136 ms |
51396 KB |
Output is correct |
28 |
Correct |
140 ms |
51400 KB |
Output is correct |
29 |
Correct |
210 ms |
72016 KB |
Output is correct |
30 |
Correct |
167 ms |
71248 KB |
Output is correct |
31 |
Correct |
2 ms |
19032 KB |
Output is correct |
32 |
Correct |
381 ms |
71760 KB |
Output is correct |
33 |
Correct |
360 ms |
71324 KB |
Output is correct |
34 |
Correct |
169 ms |
56008 KB |
Output is correct |
35 |
Correct |
148 ms |
54468 KB |
Output is correct |
36 |
Correct |
428 ms |
76368 KB |
Output is correct |
37 |
Correct |
247 ms |
76368 KB |
Output is correct |
38 |
Correct |
229 ms |
76480 KB |
Output is correct |
39 |
Correct |
172 ms |
75176 KB |
Output is correct |
40 |
Correct |
209 ms |
65276 KB |
Output is correct |
41 |
Correct |
213 ms |
54868 KB |
Output is correct |
42 |
Correct |
2 ms |
19036 KB |
Output is correct |
43 |
Correct |
274 ms |
51072 KB |
Output is correct |
44 |
Correct |
269 ms |
51056 KB |
Output is correct |
45 |
Correct |
301 ms |
55908 KB |
Output is correct |
46 |
Correct |
234 ms |
56020 KB |
Output is correct |
47 |
Correct |
200 ms |
54768 KB |
Output is correct |
48 |
Correct |
218 ms |
56596 KB |
Output is correct |
49 |
Correct |
139 ms |
54476 KB |
Output is correct |
50 |
Correct |
169 ms |
48184 KB |
Output is correct |
51 |
Correct |
207 ms |
54496 KB |
Output is correct |
52 |
Correct |
202 ms |
54072 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 |
19036 KB |
Output is correct |
56 |
Correct |
4 ms |
19548 KB |
Output is correct |
57 |
Correct |
5 ms |
19548 KB |
Output is correct |
58 |
Correct |
4 ms |
19292 KB |
Output is correct |
59 |
Correct |
5 ms |
19548 KB |
Output is correct |
60 |
Correct |
4 ms |
19340 KB |
Output is correct |
61 |
Correct |
3 ms |
19292 KB |
Output is correct |
62 |
Correct |
4 ms |
19616 KB |
Output is correct |
63 |
Correct |
4 ms |
19548 KB |
Output is correct |
64 |
Correct |
3 ms |
19588 KB |
Output is correct |
65 |
Correct |
3 ms |
19292 KB |
Output is correct |
66 |
Correct |
3 ms |
19292 KB |
Output is correct |
67 |
Correct |
233 ms |
76392 KB |
Output is correct |
68 |
Correct |
157 ms |
54540 KB |
Output is correct |
69 |
Correct |
259 ms |
74576 KB |
Output is correct |
70 |
Correct |
240 ms |
71492 KB |
Output is correct |
71 |
Correct |
258 ms |
76368 KB |
Output is correct |
72 |
Correct |
225 ms |
56012 KB |
Output is correct |
73 |
Correct |
249 ms |
71256 KB |
Output is correct |
74 |
Correct |
248 ms |
71216 KB |
Output is correct |
75 |
Correct |
224 ms |
55624 KB |
Output is correct |
76 |
Correct |
193 ms |
54628 KB |
Output is correct |
77 |
Correct |
136 ms |
54500 KB |
Output is correct |
78 |
Correct |
137 ms |
54520 KB |
Output is correct |
79 |
Correct |
212 ms |
76368 KB |
Output is correct |
80 |
Correct |
169 ms |
75088 KB |
Output is correct |
81 |
Correct |
384 ms |
76112 KB |
Output is correct |
82 |
Correct |
378 ms |
71504 KB |
Output is correct |
83 |
Correct |
171 ms |
56004 KB |
Output is correct |
84 |
Correct |
141 ms |
54544 KB |
Output is correct |
85 |
Correct |
372 ms |
76368 KB |
Output is correct |
86 |
Correct |
237 ms |
76500 KB |
Output is correct |
87 |
Correct |
244 ms |
76504 KB |
Output is correct |
88 |
Correct |
186 ms |
65228 KB |
Output is correct |
89 |
Correct |
220 ms |
54980 KB |
Output is correct |
90 |
Correct |
287 ms |
55564 KB |
Output is correct |
91 |
Correct |
259 ms |
55400 KB |
Output is correct |
92 |
Correct |
264 ms |
55772 KB |
Output is correct |
93 |
Correct |
213 ms |
55792 KB |
Output is correct |
94 |
Correct |
207 ms |
54876 KB |
Output is correct |
95 |
Correct |
229 ms |
56664 KB |
Output is correct |
96 |
Correct |
133 ms |
54464 KB |
Output is correct |
97 |
Correct |
208 ms |
48464 KB |
Output is correct |
98 |
Correct |
201 ms |
54472 KB |
Output is correct |
99 |
Correct |
183 ms |
53944 KB |
Output is correct |
100 |
Correct |
130 ms |
42372 KB |
Output is correct |
101 |
Correct |
374 ms |
71504 KB |
Output is correct |
102 |
Correct |
181 ms |
52440 KB |
Output is correct |
103 |
Correct |
356 ms |
66392 KB |
Output is correct |
104 |
Correct |
398 ms |
75604 KB |
Output is correct |