제출 #994351

#제출 시각아이디문제언어결과실행 시간메모리
994351yeediot다리 (APIO19_bridges)C++17
14 / 100
1379 ms13092 KiB
#include<bits/stdc++.h> using namespace std; #define int long long #define all(x) x.begin(),x.end() #define F first #define S second #define pii pair<int,int> #define pb push_back #define sz(x) (int)(x.size()) #define tiii tuple<int, int, int> #define toi_is_so_de ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); void __print(int x) {cerr << x;} void __print(float x) {cerr << x;} void __print(double x) {cerr << x;} void __print(long double x) {cerr << x;} void __print(char x) {cerr << '\'' << x << '\'';} void __print(const char *x) {cerr << '\"' << x << '\"';} void __print(const string &x) {cerr << '\"' << x << '\"';} void __print(bool x) {cerr << (x ? "true" : "false");} template<typename T, typename V> void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';} template<typename T> void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";} void _print() {cerr << "]\n";} template <typename T, typename... V> void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);} #ifdef local void CHECK(); void setio(){ freopen("/Users/iantsai/cpp/input.txt","r",stdin); freopen("/Users/iantsai/cpp/output.txt","w",stdout); } #define debug(x...) cerr << "[" << #x << "] = ["; _print(x) #else void setio(){} #define debug(x...) #endif const int mxn = 5e4+5, mxm = 1e5+5, siz = sqrt(mxm); int n, m, q, v[mxm], id[mxm], ans[mxm], op[mxm], changed[mxm], a[mxm], b[mxm], c[mxm], x[mxm], y[mxm], ord[mxm]; bool vis[mxm]; struct DSU { int to[mxn], num[mxn]; vector<pii>temp; void init(int n){ for(int i = 1; i <= n; i++){ to[i] = i; num[i] = 1; } temp.clear(); } int find(int x){ return x == to[x] ? x : to[x] = find(to[x]); } bool merge(int x, int y){ x = find(x), y = find(y); if(x == y)return false; if(num[x] < num[y])swap(x, y); temp.pb({y, x}); to[y] = x; num[x] += num[y]; return true; } void undo(int ed){ while(sz(temp) > ed){ auto [y, x] = temp.back(); num[x] -= num[y]; to[y] = y; temp.pop_back(); } } }d; signed main(){ setio(); toi_is_so_de; cin >> n >> m; for(int i=1;i<=m;i++){ cin >> a[i] >> b[i] >> c[i]; } for(int i=1;i<=m;i++){ ord[i] = i; } cin >> q; for(int i=1;i<=q;i++){ cin >> op[i] >> x[i] >> y[i]; } for(int i=1;i<=q;i+=siz){ d.init(n); sort(ord+1, ord+1+m, [&](int i, int j){return c[i] > c[j];}); vector<int> qres, temp; for(int j=min(q, i+siz-1);j>=i;j--){ if(op[j] == 1){ changed[x[j]] = j; temp.pb(x[j]); } else{ qres.pb(j); } } sort(all(temp)); temp.resize(unique(all(temp))-temp.begin()); int pos = 0; sort(all(qres), [&](int i, int j){return y[i] > y[j];}); for(auto t : qres){ while(pos+1 <= m and c[ord[pos+1]] >= y[t]){ pos++; if(changed[ord[pos]] == 0) d.merge(a[ord[pos]], b[ord[pos]]); } int ro = sz(d.temp); for(int j=t;j>=i;j--){ if(op[j] == 2 or vis[x[j]])continue; vis[x[j]] = 1; if(y[j] >= y[t]){ d.merge(a[x[j]], b[x[j]]); } } for(auto x : temp){ if(vis[x]) vis[x] = 0; else if(c[x] >= y[t]){ d.merge(a[x], b[x]); } } ans[t] = d.num[d.find(x[t])]; d.undo(ro); } for(int j=i;j<=min(q,i+siz-1);j++){ if(op[j] == 1){ changed[x[j]] = 0; c[x[j]] = y[j]; } } } for(int i=1;i<=q;i++){ if(ans[i]) cout<< ans[i] << '\n'; } } #ifdef local void CHECK(){ cerr << "\n[Time]: " << 1000.0 * clock() / CLOCKS_PER_SEC << " ms.\n"; function<bool(string,string)> compareFiles = [](string p1, string p2)->bool { std::ifstream file1(p1); std::ifstream file2(p2); if (!file1.is_open() || !file2.is_open())return false; std::string line1, line2; while (getline(file1, line1) && getline(file2, line2)) { if (line1 != line2)return false; } int cnta=0,cntb=0; while(getline(file1,line1))cnta++; while(getline(file2,line2))cntb++; return cntb-cnta<=1; }; bool check = compareFiles("output.txt","expected.txt"); if (check) cerr<<"ACCEPTED\n"; else cerr<<"WRONG ANSWER!\n"; } #else #endif
#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...