Submission #391388

# Submission time Handle Problem Language Result Execution time Memory
391388 2021-04-18T16:46:08 Z phathnv Street Lamps (APIO19_street_lamps) C++11
0 / 100
12 ms 2380 KB
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;

const int N = 5e4 + 7;
const int M = 1e5 + 7;
const int BLOCK_SIZE = 1000;

struct Edge{
    int u, v, c;
};

struct Query{
    int type, a, b;
};

struct Dsu{
    int root[N], sz[N];
    stack<int> his;
    void init(int n){
        for(int i = 1; i <= n; i++){
            root[i] = i;
            sz[i] = 1;
        }
        while (!his.empty())
            his.pop();
    }
    int findRoot(int u){
        if (u == root[u])
            return u;
        return findRoot(root[u]);
    }
    int getSz(int u){
        return sz[findRoot(u)];
    }
    void unite(int u, int v){
        u = findRoot(u);
        v = findRoot(v);
        if (u == v)
            return;
        if (sz[u] < sz[v])
            swap(u, v);
        root[v] = u;
        sz[u] += sz[v];
        his.push(v);
    }
    int backup(){
        return his.size();
    }
    void rollback(int ver){
        while ((int) his.size() > ver){
            int v = his.top();
            int u = root[v];
            root[v] = v;
            while (true){
                sz[u] -= sz[v];
                if (u == root[u])
                    break;
                u = root[u];
            }
            his.pop();
        }
    }
} dsu;

int n, m, q, ordEdge[M];
Edge eds[M];
bool changed[M];

void Process(const vector<Query> &queries){
    vector<int> ordType2;
    vector<pair<int, int>> changedEdges;
    for(int i = 1; i <= m; i++){
        changed[i] = 0;
        ordEdge[i] = i;
    }
    for(int i = 0; i < (int) queries.size(); i++)
        if (queries[i].type == 2){
            ordType2.push_back(i);
        } else {
            changed[queries[i].a] = 1;
            changedEdges.push_back({queries[i].a, eds[queries[i].a].c});
        }

    sort(ordType2.begin(), ordType2.end(), [&](const int &a, const int &b){
            return queries[a].b > queries[b].b;
        });
    sort(ordEdge + 1, ordEdge + 1 + m, [&](const int &a, const int &b){
            return eds[a].c > eds[b].c;
        });

    dsu.init(n);
    vector<pair<int, int>> answer;
    int ptr = 1;
    for(int ind : ordType2){
        int u = queries[ind].a;
        int c = queries[ind].b;
        while (ptr <= m && eds[ordEdge[ptr]].c >= c){
            if (!changed[ordEdge[ptr]])
                dsu.unite(eds[ordEdge[ptr]].u, eds[ordEdge[ptr]].v);
            ptr++;
        }
        int ver = dsu.backup();
        for(int i = 0; i < ind; i++)
            if (queries[i].type == 1)
                eds[queries[i].a].c = queries[i].b;
        for(auto p : changedEdges){
            if (eds[p.first].c >= c)
                dsu.unite(eds[p.first].u, eds[p.first].v);
        }
        answer.push_back({ind, dsu.getSz(u)});
        for(auto p : changedEdges)
            eds[p.first].c = p.second;
        dsu.rollback(ver);
    }
    sort(answer.begin(), answer.end());
    for(auto p : answer)
        cout << p.second << '\n';
    for(Query q : queries)
        if (q.type == 1)
            eds[q.a].c = q.b;
}

int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);

    cin >> n >> m;
    for(int i = 1; i <= m; i++)
        cin >> eds[i].u >> eds[i].v >> eds[i].c;
    cin >> q;

    vector<Query> queries;
    for(int i = 1; i <= q; i++){
        Query query;
        cin >> query.type >> query.a >> query.b;
        queries.push_back(query);
        if (queries.size() == BLOCK_SIZE){
            Process(queries);
            queries.clear();
        }
    }
    Process(queries);
    queries.clear();

    return 0;
}
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 332 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 12 ms 2380 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 332 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 332 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 332 KB Output isn't correct
2 Halted 0 ms 0 KB -