제출 #1197392

#제출 시각아이디문제언어결과실행 시간메모리
1197392t9unkubj다리 (APIO19_bridges)C++20
59 / 100
3093 ms10604 KiB
#ifdef t9unkubj
#include"my_template.h"
//#include"my_template_no_debug.h"
#else
#define dbg(...) 199958
#pragma GCC optimize("O3")
#include"bits/stdc++.h"
using namespace std;
using uint=unsigned;
using ll=long long;
using ull=unsigned long long;
using ld=long double;
using i128=__int128;
template<class T>using vc=vector<T>;
template<class T>using vvc=vc<vc<T>>;
template<class T>using vvvc=vc<vvc<T>>;
template<class T>using vvvvc=vc<vvvc<T>>;
template<class T>using smpq=priority_queue<T,vc<T>,greater<T>>;
template<class T>using bipq=priority_queue<T>;
using vi=vc<int>;
using vvi=vvc<int>;
using vl=vc<ll>;
using vvl=vvc<ll>;
#define rep(i,n) for(ll i=0;i<(ll)(n);i++)
#define REP(i,j,n) for(ll i=(j);i<(ll)(n);i++)
#define DREP(i,n,m) for(ll i=(n);i>=(m);i--)
#define drep(i,n) for(ll i=(ll(n)-1);i>=0;i--)
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
#define pb push_back
template<class T,class F>
bool chmin(T &x, F y){
    if(x>y){
        x=y;
        return true;
    }
    return false;
}
template<class T, class F>
bool chmax(T &x, F y){
    if(x<y){
        x=y;
        return true;
    }
    return false;
}
template<class T>
T sum(const vector<T>&v){
    return accumulate(all(v),T(0));
}
template<class T>
T min(const vector<T>&v){
    return *min_element(all(v));
}
template<class T>
T max(const vector<T>&v){
    return *max_element(all(v));
}
void YesNo(bool y){
    cout<<(y?"Yes":"No")<<endl;
}
void Yes(){
    cout<<"Yes"<<endl;
}
void No(){
    cout<<"No"<<endl;
}
template<class T>
void unique(vc<T>&a){
    a.erase(unique(all(a)),a.end());
}
vvi readgraph(int n,int m,int off = -1){
    vvi g(n);
    rep(i, m){
        int u,v;
        cin>>u>>v;
        u+=off,v+=off;
        g[u].push_back(v);
        g[v].push_back(u);
    }
    return g;
}
vvi readtree(int n,int off=-1){
    return readgraph(n,n-1,off);
}
template<class T>
vc<T> presum(vc<T> &a){
    vc<T> ret(a.size()+1);
    rep(i,a.size())ret[i+1]=ret[i]+a[i];
    return ret;
}
template<class T, class F>
vc<T> &operator+=(vc<T> &a,F b){
    for (auto&v:a)v += b;
    return a;
}
template<class T, class F>
vc<T> &operator-=(vc<T>&a,F b){
    for (auto&v:a)v-=b;
    return a;
}
ostream&operator<<(ostream&os,i128 num) {
    if(num==0){
        os<<"0";
        return os;
    }
    if(num<0){
        os<<"-";
        num=-num;
    }
    string res;
    while(num){
        res+='0'+static_cast<int>(num%10);
        num/=10;
    }
    reverse(all(res));
    os<<res;
    return os;
}

istream&operator>>(istream& is,i128&num) {
    string s;
    is>>s;
    bool neg=0;
    num=0;
    if(s[0]=='-'){
        neg=1;
        s=s.substr(1);
    }
    for(auto&c:s){
        if (c>='0'&&c<='9'){
            num=num*10+(c-'0');
        }else{
            is.setstate(ios::failbit);
            return is;
        }
    }
    if(neg){
        num=-num;
    }
    return is;
}

void scan(int&a) { cin >> a; }
void scan(ll&a) { cin >> a; }
void scan(string&a) { cin >> a; }
void scan(char&a) { cin >> a; }
void scan(uint&a) { cin >> a; }
void scan(ull&a) { cin >> a; }
void scan(bool&a) { cin >> a; }
void scan(ld&a){ cin>> a; }
void scan(i128&a){ cin>> a; }
template<class T> void scan(vector<T>&a) { for(auto&x:a) scan(x); }
void read() {}
template<class Head, class... Tail> void read(Head&head, Tail&... tail) { scan(head); read(tail...); }
#define INT(...) int __VA_ARGS__; read(__VA_ARGS__);
#define LL(...) ll __VA_ARGS__; read(__VA_ARGS__);
#define ULL(...) ull __VA_ARGS__; read(__VA_ARGS__);
#define STR(...) string __VA_ARGS__; read(__VA_ARGS__);
#define CHR(...) char __VA_ARGS__; read(__VA_ARGS__);
#define DBL(...) double __VA_ARGS__; read(__VA_ARGS__);
#define LD(...) ld __VA_ARGS__; read(__VA_ARGS__);
#define I128(...) i128 __VA_ARGS__; read(__VA_ARGS__);
#define VC(type, name, ...) vector<type> name(__VA_ARGS__); read(name);
#define VVC(type, name, size, ...) vector<vector<type>> name(size, vector<type>(__VA_ARGS__)); read(name);

void print(int a) { cout << a; }
void print(ll a) { cout << a; }
void print(string a) { cout << a; }
void print(char a) { cout << a; }
void print(uint a) { cout << a; }
void print(bool a) { cout << a; }
void print(ull a) { cout << a; }
void print(double a) { cout << a; }
void print(ld a){ cout<< a; }
void print(i128 a){ cout<< a; }
template<class T> void print(vector<T>a) { for(int i=0;i<(int)a.size();i++){if(i)cout<<" ";print(a[i]);}cout<<endl;}
void PRT() { cout <<endl; return ; }
template<class T> void PRT(T a) { print(a); cout <<endl; return; }
template<class Head, class... Tail> void PRT(Head head, Tail ... tail) { print(head); cout << " "; PRT(tail...); return; }
struct ioset{
    ioset(){
        cin.tie(0)->sync_with_stdio(0);
        #ifdef t9unkubj
        cout<<fixed<<setprecision(6);
        #else
        cout<<fixed<<setprecision(20);
        #endif
    }
}ioset_______;
struct MY_TIMER{
    double start_time;
    MY_TIMER(){
        start_time=clock();
    }
    double out(){
        return (clock()-start_time)/double(CLOCKS_PER_SEC);
    }
}TIMER;
#endif
#include<vector>
#include <utility>
#include<stack>
struct undo_Unionfind {
    std::vector<int>par;
    std::stack<std::pair<int, int>>history;
    undo_Unionfind(int n) {
        par.resize(n, -1);
    }
    int root(int x) {
        if (par[x] < 0)return x;
        else return root(par[x]);
    }
    bool same(int x, int y) {
        return root(x) == root(y);
    }
    bool merge(int x, int y) {
        x = root(x), y = root(y);
        history.push({ x,par[x] });
        history.push({ y,par[y] });
        if (x == y)return false;
        else if (par[x] > par[y])std::swap(x, y);
        par[x] += par[y];
        par[y] = x;
        return true;
    }
    void save() {
        while (history.size())history.pop();
    }
    void undo() {
        for(int jj=0;jj<2;jj++){
            par[history.top().first] = history.top().second;
            history.pop();
        }
        return;
    }
    void rollback() {
        while (history.size())undo();
    }
    int size(int x){
        return -par[root(x)];
    }
};
constexpr int B=300;
void solve(){
    INT(n,m);
    vc<int>d(m);
    vc<array<int,3>>edge;
    rep(i,m){
        INT(u,v,D);
        --u,--v;
        d[i]=D;
        edge.push_back({u,v,(int)i});
    }

    INT(q);
    vc<array<int,4>>Query;
    rep(i,q){
        int a,b,c;
        cin>>a>>b>>c;
        --b;
        Query.push_back({a,b,c,(int)i});
    }
    
    vc<int>ans(q,-1);
    for(int t=0;;t++){
        undo_Unionfind uf(n);
        int L=t*B,R=min(L+B,q);
        if(L>=R)break;
        vc<int>nd=d;
        vc<array<int,3>>show;
        vc<int>chan(m);
        REP(j,L,R){
            if(Query[j][0]==1){
                chan[Query[j][1]]=1;
            }
        }
        vc<array<int,6>>ret;
        for(auto&v:edge){
            if(!chan[v[2]]){
                ret.push_back({d[v[2]],1,v[0],v[1],v[2],-1});
            }else{
                show.push_back(v);
            }
        }
        REP(j,L,R){
            if(Query[j][0]==2){
                //W-type-type-vertex-W-idx
                ret.push_back({Query[j][2],0,Query[j][0],Query[j][1],Query[j][2],Query[j][3]});
            }
        }
        sort(rall(ret));
        dbg(d);
        for(auto&x:ret){
            if(x[1]==1){
                uf.merge(x[2],x[3]);
            }else{
                for(auto&x:show){
                    nd[x[2]]=d[x[2]];
                }
                dbg(nd);
                REP(j,L,x[5]){
                    if(Query[j][0]==1){
                        nd[Query[j][1]]=Query[j][2];
                    }
                }
                dbg(x[5]);
                dbg(nd);
                uf.save();
                for(auto&tar:show){
                    if(nd[tar[2]]>=x[0]){
                        uf.merge(tar[0],tar[1]);
                    }
                }
                ans[x[5]]=uf.size(x[3]);
                uf.rollback();
            }
        }
         REP(j,L,R){
            if(Query[j][0]==1){
                d[Query[j][1]]=Query[j][2];
            }
        }
    }
    
    rep(i,q)if(ans[i]!=-1)cout<<ans[i]<<"\n";
}
signed main(){
    int t=1;
    //cin>>t;
    while(t--)solve();
}
#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...