#include <bits/stdc++.h>
#define fr(i, n, m) for(int i = (n); i < (m); i ++)
#define st first
#define nd second
#define pb push_back
#define pq priority_queue
#define all(x) begin(x), end(x)
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int,int> pii;
const int mxn = 2e5 + 5;
int n, m;
vector<int> g[mxn];
ll a[mxn];
vector<int> S[mxn];
vector<int> G[mxn];
ll sum[mxn];
int v_count = 0;
int vis_ver[mxn];
int vis_edg[mxn];
vector<int> merge(int u, int v){
sum[u] += sum[v];
vector<int> cand;
for(auto vp : S[v]){
if(vis_ver[vp] == v_count) continue;
S[u].pb(vp);
vis_ver[vp] = v_count;
}
for(auto ep : G[v]){
if(vis_edg[ep] == v_count || vis_ver[ep] == v_count) continue;
G[u].pb(ep);
cand.pb(ep);
vis_edg[ep] = v_count;
}
return cand;
}
void expand(int u){
++v_count;
for(auto e : S[u]) vis_ver[e] = v_count;
for(auto e : G[u]) vis_edg[e] = v_count;
pq <pair<int,int> > Q;
for(auto e : G[u]) Q.push({-a[e], e});
while(!Q.empty()){
int c = Q.top().nd;
Q.pop();
if(vis_ver[c] == v_count) continue;
if(sum[u] < a[c]) continue;
vector<int> cand = merge(u, c);
for(auto c : cand){
Q.push({-a[c], c});
}
}
}
int main(){
cin >> n >> m;
fr(i, 0, n){
cin >> a[i];
}
fr(i, 0, m){
int u, v;
cin >> u >> v;
--u, --v;
g[u].pb(v);
g[v].pb(u);
}
fr(i, 0, n){
G[i] = g[i];
S[i] = {i};
sum[i] = a[i];
}
fr(i, 0, n){
expand(i);
}
fr(i, 0, n){
if(S[i].size() > n) cout<<2/0<<endl;
if((int)S[i].size() == n) cout<<1;
else cout<<0;
}
}
Compilation message
island.cpp: In function 'int main()':
island.cpp:90:18: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
90 | if(S[i].size() > n) cout<<2/0<<endl;
| ~~~~~~~~~~~~^~~
island.cpp:90:30: warning: division by zero [-Wdiv-by-zero]
90 | if(S[i].size() > n) cout<<2/0<<endl;
| ~^~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
14420 KB |
Output is correct |
2 |
Correct |
8 ms |
14420 KB |
Output is correct |
3 |
Correct |
8 ms |
14404 KB |
Output is correct |
4 |
Incorrect |
29 ms |
22808 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
14344 KB |
Output is correct |
2 |
Correct |
8 ms |
14368 KB |
Output is correct |
3 |
Execution timed out |
1104 ms |
111496 KB |
Time limit exceeded |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
14420 KB |
Output is correct |
2 |
Execution timed out |
1061 ms |
414688 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
14420 KB |
Output is correct |
2 |
Execution timed out |
1089 ms |
220720 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
14420 KB |
Output is correct |
2 |
Correct |
8 ms |
14420 KB |
Output is correct |
3 |
Correct |
8 ms |
14404 KB |
Output is correct |
4 |
Incorrect |
29 ms |
22808 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |