제출 #367477

#제출 시각아이디문제언어결과실행 시간메모리
367477Jarif_RahmanBridges (APIO19_bridges)C++17
13 / 100
3079 ms12216 KiB
#include <bits/stdc++.h>
#define pb push_back
#define f first
#define sc second
using namespace std;
typedef long long int ll;
typedef string str;
struct dsu{
    stack<tuple<int, int, int>> st;
    int n;
    vector<int> id;
    vector<vector<int>> comp;
    dsu(int nn){
        n = nn;
        for(int i = 0; i < n;  i++) id.pb(i), comp.pb({i});
    }
    int unite(int a, int b){
        a = id[a], b = id[b];
        if(a == b) return 0;
        if(comp[a].size() > comp[b].size()) swap(a, b);
        st.push({a, b, (int)comp[a].size()});
        while(!comp[a].empty()){
            int x = comp[a].back(); comp[a].pop_back();
            id[x] = b;
            comp[b].pb(x);
        }
        return 1;
    }
    void rollback(){
        auto [a, b, sz] = st.top(); st.pop();
        while(sz--){
            int x = comp[b].back(); comp[b].pop_back();
            id[x] = a;
            comp[a].pb(x);
        }
    }
};
int sq;
int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int n, m; cin >> n >> m;
    vector<tuple<ll, int, int>> edge;
    for(int i = 0; i < m; i++){
        int a, b; ll w; cin >> a >> b >> w; a--, b--;
        edge.pb({w, a, b});
    }
    int q; cin >> q;
    sq = sqrt(q+0.0);
	sq = min(sq, 50);
    vector<vector<tuple<ll, int, int>>> query;
    query.pb({});
    for(int i = 0; i < q; i++){
        int tt, s; ll w; cin >> tt >> s >> w; s--;
        if(query.back().size() >= sq) query.pb({});
        query.back().pb({tt, s, w});
    }
    vector<int> ans;
    for(auto qq: query){
        dsu ds(n);
        vector<tuple<ll, int, int>> calc;
        vector<bool> ucng(m, 1);
        vector<tuple<ll, int, int>> edges;
        vector<int> cng;
        int cnt = 0;
        for(auto [tt, s, w]: qq){
            if(tt == 1){
                ucng[s] = 0;
            }
            else{
                cnt++;
                calc.pb({w, s, cnt-1});
            }
        }
        for(int i = 0; i < m; i++){
            if(ucng[i]) edges.pb(edge[i]);
            else cng.pb(i);
        }
        vector<int> anss(cnt);
        vector<vector<pair<int, int>>> upd(cnt);
        cnt = 0;
        for(auto [tt, s, w]: qq){
            if(tt == 1){
                get<0>(edge[s]) = w;
            }
            else{
                for(int x: cng){
                    auto [ww, a, b] = edge[x];
                    if(ww >= w) upd[cnt].pb({a, b});
                }
                cnt++;
            }
        }
        sort(edges.begin(), edges.end());
        sort(calc.rbegin(), calc.rend());
        for(auto [w, s, in]: calc){
            while(!edges.empty() && get<0>(edges.back()) >= w){
                auto [ww, a, b] = edges.back(); edges.pop_back();
                ds.unite(a, b);
            }
            int k = 0;
            for(auto [a, b]: upd[in]) k+=ds.unite(a, b);
            anss[in] = ds.comp[ds.id[s]].size();
            while(k--) ds.rollback();
        }
        ans.insert(ans.end(), anss.begin(), anss.end());
    }
    for(int x: ans) cout << x << "\n";
}

컴파일 시 표준 에러 (stderr) 메시지

bridges.cpp: In function 'int main()':
bridges.cpp:55:32: warning: comparison of integer expressions of different signedness: 'std::vector<std::tuple<long long int, int, int> >::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   55 |         if(query.back().size() >= sq) query.pb({});
      |            ~~~~~~~~~~~~~~~~~~~~^~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...