제출 #755444

#제출 시각아이디문제언어결과실행 시간메모리
755444guagua0407다리 (APIO19_bridges)C++17
100 / 100
2587 ms8240 KiB
#pragma GCC optimize("O4") #include <bits/stdc++.h> using namespace std; #define ll long long #define pii pair<int,int> #define f first #define s second #define all(x) x.begin(),x.end() #define _ ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); void setIO(string s) { freopen((s + ".in").c_str(), "r", stdin); freopen((s + ".out").c_str(), "w", stdout); } struct edge{ int a,b,w; }; struct query{ int t,a,b,ind; }; const int mxn=5e4+5; const int mxm=1e5+5; edge E[mxm]; query qs[mxm]; bool in[mxm]; bool visited[mxm]; int ans[mxm]; int W[mxm]; int K=333; struct DSU{ vector<int> e; stack<pair<int,int>> st; DSU(int n){ e=vector<int>(n,-1); } void init(){ for(auto &v:e) v=-1; while(!st.empty()) st.pop(); } int find(int x){ return (e[x]<0?x:find(e[x])); } int sz(int x){ return -e[find(x)]; } bool unite(int a,int b){ a=find(a); b=find(b); if(a==b) return false; if(e[a]>e[b]) swap(a,b); st.push({a,e[a]}); st.push({b,e[b]}); e[a]+=e[b]; e[b]=a; return true; } void roll_back(int sz){ while(st.size()>sz){ pair<int,int> tmp=st.top(); st.pop(); e[tmp.f]=tmp.s; } } }; bool comp(query x,query y){ return x.b>y.b; } bool comp2(edge x,edge y){ return x.w>y.w; } int main() {_ int n,m,q; cin>>n>>m; for(int i=0;i<m;i++){ int a,b,w; cin>>a>>b>>w; a--; b--; E[i]={a,b,w}; } cin>>q; K=500; for(int i=0;i<q;i++){ int t,a,b; cin>>t>>a>>b; a--; qs[i]={t,a,b,i}; } DSU dsu(n); for(int k=0;k<q;k+=K){ vector<query> tmp; vector<int> ein; for(int i=k;i<min(k+K,q);i++){ if(qs[i].t==1){ in[qs[i].a]=true; ein.push_back(qs[i].a); } else{ tmp.push_back(qs[i]); } } sort(all(tmp),comp); vector<edge> ok; for(int i=0;i<m;i++){ if(!in[i]) ok.push_back(E[i]); } sort(all(ok),comp2); int pos=0; for(int i=0;i<tmp.size();i++){ while(pos<ok.size() and ok[pos].w>=tmp[i].b){ dsu.unite(ok[pos].a,ok[pos].b); pos++; } int prev_sz=dsu.st.size(); for(auto v:ein){ W[v]=E[v].w; } for(int j=k;j<tmp[i].ind;j++){ if(qs[j].t==1){ W[qs[j].a]=qs[j].b; } } for(auto v:ein){ if(W[v]>=tmp[i].b){ dsu.unite(E[v].a,E[v].b); } } ans[tmp[i].ind]=dsu.sz(tmp[i].a); dsu.roll_back(prev_sz); } for(int i=k;i<min(k+K,q);i++){ if(qs[i].t==1){ E[qs[i].a].w=qs[i].b; in[qs[i].a]=false; } } dsu.roll_back(0); } for(int i=0;i<q;i++){ if(qs[i].t==2){ cout<<ans[i]<<'\n'; } } return 0; } //maybe its multiset not set

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

bridges.cpp: In member function 'void DSU::roll_back(int)':
bridges.cpp:62:24: warning: comparison of integer expressions of different signedness: 'std::stack<std::pair<int, int> >::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   62 |         while(st.size()>sz){
      |               ~~~~~~~~~^~~
bridges.cpp: In function 'int main()':
bridges.cpp:116:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<query>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  116 |         for(int i=0;i<tmp.size();i++){
      |                     ~^~~~~~~~~~~
bridges.cpp:117:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<edge>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  117 |             while(pos<ok.size() and ok[pos].w>=tmp[i].b){
      |                   ~~~^~~~~~~~~~
bridges.cpp: In function 'void setIO(std::string)':
bridges.cpp:12:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   12 |     freopen((s + ".in").c_str(), "r", stdin);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
bridges.cpp:13:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   13 |     freopen((s + ".out").c_str(), "w", stdout);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#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...