Submission #788482

#TimeUsernameProblemLanguageResultExecution timeMemory
788482alexander707070Fountain Parks (IOI21_parks)C++17
55 / 100
1444 ms181520 KiB
#include<bits/stdc++.h> #include "parks.h" #define MAXN 800007 using namespace std; struct bench{ int from,to; int a,b; }; int n,curr,benches,br,k,comp[MAXN],ans[MAXN],dsu[MAXN]; bool li[MAXN]; vector<int> v[MAXN],r[MAXN]; stack<int> st; vector<bench> sol; vector<int> A,B,C,D; vector< pair<int,int> > ors; map< pair<int,int>, int> mp; map< pair<int,int>, vector<int> > bro; int op(int x){ if(x<400000)return x+400000; return x-400000; } void add(int x,int y,int z){ for(int i=0;i<bro[{x,y}].size();i++){ ors.push_back({op(bro[{x,y}][i]),op(z)}); } bro[{x,y}].push_back(z); } void dfs(int x){ li[x]=true; for(int i=0;i<v[x].size();i++){ if(!li[v[x][i]])dfs(v[x][i]); } st.push(x); } void scc(int x){ comp[x]=k; li[x]=true; for(int i=0;i<r[x].size();i++){ if(!li[r[x][i]])scc(r[x][i]); } } int root(int x){ if(dsu[x]==x)return x; dsu[x]=root(dsu[x]); return dsu[x]; } void mergev(int x,int y){ dsu[root(x)]=root(y); } int construct_roads(vector<int> x,vector<int> y){ n=int(x.size()); for(int i=0;i<n;i++){ dsu[i]=i; mp[{x[i],y[i]}]=i+1; } br=0; for(int i=0;i<n;i++){ curr=mp[{x[i]-2,y[i]}]; if(curr!=0 and root(i)!=root(curr-1)){ curr--; mergev(i,curr); add(x[i]-1,y[i]-1,br); add(x[i]-1,y[i]+1,br+1); ors.push_back({br,br+1}); br+=2; } curr=mp[{x[i],y[i]-2}]; if(curr!=0 and root(i)!=root(curr-1)){ curr--; mergev(i,curr); add(x[i]-1,y[i]-1,br); add(x[i]+1,y[i]-1,br+1); ors.push_back({br,br+1}); br+=2; } } if(br!=2*n-2)return 0; for(int i=0;i<ors.size();i++){ v[op(ors[i].first)].push_back(ors[i].second); r[ors[i].second].push_back(op(ors[i].first)); v[op(ors[i].second)].push_back(ors[i].first); r[ors[i].first].push_back(op(ors[i].second)); } for(int i=0;i<800000;i++){ if(!li[i])dfs(i); } for(int i=0;i<800000;i++)li[i]=false; while(!st.empty()){ if(!li[st.top()]){ k++; scc(st.top()); } st.pop(); } for(int i=0;i<br;i++){ if(comp[i]==comp[op(i)])return 0; if(comp[i]<comp[op(i)])ans[i]=0; else ans[i]=1; } for(int i=0;i<n;i++)dsu[i]=i; br=0; for(int i=0;i<n;i++){ curr=mp[{x[i]-2,y[i]}]; if(curr!=0 and root(i)!=root(curr-1)){ curr--; mergev(i,curr); if(ans[br]==0 and ans[br+1]==0)return -1; if(ans[br]==1){ sol.push_back({curr,i,x[i]-1,y[i]-1}); }else{ sol.push_back({curr,i,x[i]-1,y[i]+1}); } br+=2; } curr=mp[{x[i],y[i]-2}]; if(curr!=0 and root(i)!=root(curr-1)){ curr--; mergev(i,curr); if(ans[br]==0 and ans[br+1]==0)return -1; if(ans[br]==1){ sol.push_back({curr,i,x[i]-1,y[i]-1}); }else{ sol.push_back({curr,i,x[i]+1,y[i]-1}); } br+=2; } } for(int i=0;i<sol.size();i++){ A.push_back(sol[i].from); B.push_back(sol[i].to); C.push_back(sol[i].a); D.push_back(sol[i].b); } build(A,B,C,D); return 1; } /* int main(){ cout<<construct_roads({4, 4, 6, 4, 2}, {4, 6, 4, 2, 4}); } */

Compilation message (stderr)

parks.cpp: In function 'void add(int, int, int)':
parks.cpp:29:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   29 |     for(int i=0;i<bro[{x,y}].size();i++){
      |                 ~^~~~~~~~~~~~~~~~~~
parks.cpp: In function 'void dfs(int)':
parks.cpp:37:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   37 |     for(int i=0;i<v[x].size();i++){
      |                 ~^~~~~~~~~~~~
parks.cpp: In function 'void scc(int)':
parks.cpp:47:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   47 |     for(int i=0;i<r[x].size();i++){
      |                 ~^~~~~~~~~~~~
parks.cpp: In function 'int construct_roads(std::vector<int>, std::vector<int>)':
parks.cpp:97:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   97 |     for(int i=0;i<ors.size();i++){
      |                 ~^~~~~~~~~~~
parks.cpp:155:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<bench>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  155 |     for(int i=0;i<sol.size();i++){
      |                 ~^~~~~~~~~~~
#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...