Submission #850439

# Submission time Handle Problem Language Result Execution time Memory
850439 2023-09-16T14:49:01 Z Ahmed57 Stranded Far From Home (BOI22_island) C++17
0 / 100
38 ms 15824 KB
#include <bits/stdc++.h>
using namespace std;
int pr[100001],gs[100001],val[100001];
vector<int> adj[100001],ne[100001];
int find(int x){
    if(x==pr[x])return x;
    return pr[x] = find(pr[x]);
}
bool mergegroup(int a,int b){
    a=find(a),b=find(b);
    if(a==b)return 0;
    pr[b] = a , gs[a]+=gs[b];
    return 1;
}
int main(){
    int n,m;
    cin>>n>>m;
    int arr[n];
    vector<pair<int,int>> v;
    for(int i = 0;i<n;i++){
        cin>>arr[i];val[i] = 1;
        pr[i] = i;gs[i] = arr[i];
        v.push_back({arr[i],i});
    }
    sort(v.begin(),v.end());
    for(int i = 0;i<m;i++){
        int a,b;cin>>a>>b;
        a--;b--;
        if(arr[a]>arr[b])adj[a].push_back(b);
        else if(arr[b]>arr[a])adj[b].push_back(a);
        else adj[b].push_back(a);
    }for(auto i:v){
        int co = i.first , x = i.second;
        for(auto j:adj[x]){
            int lol = find(j) , xd = gs[lol];
            if(mergegroup(lol,x)){
                ne[x].push_back(lol);
                if(xd<co){
                    queue<int> q;q.push(lol);
                    val[lol] = 0;
                    while(!q.empty()){
                        int xx = q.front();q.pop();
                        for(auto j:ne[xx]){
                            if(val[j]){
                                val[j] = 0;
                                q.push(j);
                            }
                        }
                    }
                }
            }
        }
    }
    for(int i = 0;i<n;i++)cout<<val[i];
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4952 KB Output is correct
2 Correct 1 ms 4956 KB Output is correct
3 Correct 2 ms 4952 KB Output is correct
4 Incorrect 3 ms 5208 KB Output isn't correct
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4952 KB Output is correct
2 Correct 1 ms 4952 KB Output is correct
3 Runtime error 35 ms 15824 KB Execution killed with signal 11
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4952 KB Output is correct
2 Runtime error 35 ms 15812 KB Execution killed with signal 11
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4952 KB Output is correct
2 Runtime error 38 ms 15816 KB Execution killed with signal 11
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4952 KB Output is correct
2 Correct 1 ms 4956 KB Output is correct
3 Correct 2 ms 4952 KB Output is correct
4 Incorrect 3 ms 5208 KB Output isn't correct
5 Halted 0 ms 0 KB -