Submission #559280

# Submission time Handle Problem Language Result Execution time Memory
559280 2022-05-09T15:00:47 Z Redhood Paint (COI20_paint) C++14
31 / 100
153 ms 16768 KB
#include<bits/stdc++.h>

#define fi first
#define se second
#define sz(x) (int)(x).size()
#define pb push_back
#define mkp make_pair
using namespace std;

typedef long long ll;
typedef long double ld;



vector < vector < int > > g;

const int N = (int)2e5 + 10;

vector < int > st[N];
int p[N], cur_color[N];
void make(int v, int C){
    p[v] = v;
    cur_color[v] = C;
}

int fin(int v){
    if(p[v] == v)return v;
    return p[v] = fin(p[v]);
}
void un(int a , int b){
    a = fin(a) , b = fin(b);
    if(a == b)
        return;
    if(sz(st[a]) < sz(st[b]))
        swap(a , b);
    p[b] = a;
    for(auto &u : st[b]){
        if(fin(u) != a)
            st[a].pb(u);
    }
}


vector < pair < int , int > > dir = {{-1,0},{+1,0},{0,-1},{0,+1}};
int r , s;
bool valid(int x , int y){
    if(x >= 0 && x < r && y >= 0 && y < s)
        return true;
    return false;
}
bool onezero = 1;
void makeit(int cell, int clr){


    if(onezero){
        cell = fin(cell);
        if(cur_color[cell] == clr)
            return;

        vector < int > unite_with;
        for(auto &u : st[cell]){
            if(fin(u) != cell && cur_color[fin(u)] == clr){
                unite_with.pb(u);
            }
        }
        st[cell].clear();

        for(auto &u : unite_with)
            un(u , cell);


        cur_color[fin(cell)] = clr;
    }else{
        vector < int > unite_with;
        vector<int>save;
        for(auto &u : st[cell]){
            if(fin(u) == cell)
                continue;
            if(cur_color[fin(u)] == clr){
                unite_with.pb(u);
            }else
                save.pb(u);
        }
        st[cell] = save;
        for(auto &t : unite_with)
            un(cell, t);
        cur_color[fin(cell)] = clr;
    }
}
signed main(){
    ios_base::sync_with_stdio(0) , cin.tie(0) , cout.tie(0);
    cin >> r >> s;
    g.resize(r , vector < int > (s));
    onezero = 1;
    for(int i = 0; i < r; ++i)
        for(int j = 0; j < s; ++j){
            cin >> g[i][j];
            if(g[i][j] > 1)
                onezero = 0;
        }

    for(int i = 0; i < r * s; ++i)
        make(i, g[i / s][i % s]);
    for(int i = 0; i < r; ++i)
        for(int j = 0; j < s; ++j){
            for(auto &u : dir){
                int i1 = i + u.fi , j1 = j + u.se;
                if(valid(i1, j1)){
                    if(g[i1][j1] == g[i][j])
                        un(i1*s + j1, i*s + j);
                }
            }
        }
    for(int i = 0; i < r; ++i)
        for(int j = 0; j < s; ++j){
            for(auto &u : dir){
                int i1 = i + u.fi , j1 = j + u.se;
                if(valid(i1, j1)){
                    if(g[i1][j1] != g[i][j]){
                        st[fin(i*s+j)].pb(fin(i1*s+j1));
                    }
                }
            }
        }


    int q;
    cin >> q;

    vector < pair < int , int > > memory;
    for(int i = 0; i < q; ++i){
        /// online lol
        int x , y, c;
        cin >> x >> y >> c;
        --x,--y;
        memory.pb({x*s + y, c});
        if(c > 1){
            onezero = 0;
        }

    }

    for(auto &i : memory){
        int x , y;
        tie(x , y) = i;
        makeit(x , y);

    }


    for(int i = 0; i < r; ++i){
        for(int j = 0; j < s; ++j){
            cout << cur_color[fin(i * s + j)] << ' ';
        }
        cout << '\n';
    }

    return 0;
}
# Verdict Execution time Memory Grader output
1 Incorrect 4 ms 4948 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 66 ms 8296 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 99 ms 16456 KB Output is correct
2 Correct 106 ms 16256 KB Output is correct
3 Correct 103 ms 16768 KB Output is correct
4 Correct 153 ms 16652 KB Output is correct
5 Correct 129 ms 15804 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 85 ms 12128 KB Output isn't correct
2 Halted 0 ms 0 KB -