Download 2017/finals/moles.cpp from hackercupai/hackercup: direct link, hf CLI and curl.
- Browser
- Download file 4.37 kB
-
https://huggingface.co/datasets/hackercupai/hackercup/resolve/main/2017/finals/moles.cpp
- Command line
-
hf download hf://datasets/hackercupai/hackercup/2017/finals/moles.cpp
-
curl -L -o moles.cpp https://huggingface.co/datasets/hackercupai/hackercup/resolve/main/2017/finals/moles.cpp
4.37 kB
| // Hacker Cup 2017 | |
| // Final Round | |
| // Fox Moles | |
| // Jacob Plachta | |
| using namespace std; | |
| template<typename T> T Abs(T x) { return(x<0 ? -x : x); } | |
| template<typename T> T Sqr(T x) { return(x*x); } | |
| const int INF = (int)1e9; | |
| const LD EPS = 1e-12; | |
| const LD PI = acos(-1.0); | |
| bool Read(int &x) | |
| { | |
| char c,r=0,n=0; | |
| x=0; | |
| for(;;) | |
| { | |
| c=getchar(); | |
| if ((c<0) && (!r)) | |
| return(0); | |
| if ((c=='-') && (!r)) | |
| n=1; | |
| else | |
| if ((c>='0') && (c<='9')) | |
| x=x*10+c-'0',r=1; | |
| else | |
| if (r) | |
| break; | |
| } | |
| if (n) | |
| x=-x; | |
| return(1); | |
| } | |
| int K,A,B; | |
| map<int,int> M; | |
| vector<int> con[LIM],con2[LIM]; | |
| bool col0[LIM]; | |
| int col[LIM],ind[LIM],P1[LIM],P2[LIM],dist[LIM]; | |
| int Make(int i) | |
| { | |
| if (M.count(i)) | |
| return(M[i]); | |
| return(M[i]=K++); | |
| } | |
| bool DFS(int i,int c) | |
| { | |
| // conflict? | |
| if ((col0[i]) && (c) || (col[i]>=0) && (c!=col[i])) | |
| return(0); | |
| // already coloured? | |
| if (col[i]>=0) | |
| return(1); | |
| // colour node and its neighbours | |
| col[i]=c; | |
| int j; | |
| Fox(j,Sz(con[i])) | |
| if (!DFS(con[i][j],1-c)) | |
| return(0); | |
| return(1); | |
| } | |
| bool MatchBFS() | |
| { | |
| int i,a,b,a2; | |
| queue<int> Q; | |
| Fill(dist,60); | |
| Fox(a,A) | |
| if (P1[a]<0) | |
| dist[a]=0,Q.push(a); | |
| while (!Q.empty()) | |
| { | |
| a=Q.front(),Q.pop(); | |
| if (dist[a]<dist[A]) | |
| Fox(i,Sz(con2[a])) | |
| { | |
| b=con2[a][i]; | |
| a2=P2[b]; | |
| if (dist[a2]>=INF) | |
| dist[a2]=dist[a]+1,Q.push(a2); | |
| } | |
| } | |
| return(dist[A]<INF); | |
| } | |
| bool MatchDFS(int a) | |
| { | |
| if (a==A) | |
| return(1); | |
| int i,b,a2; | |
| Fox(i,Sz(con2[a])) | |
| { | |
| b=con2[a][i]; | |
| a2=P2[b]; | |
| if ((dist[a2]==dist[a]+1) && (MatchDFS(a2))) | |
| { | |
| P1[a]=b; | |
| P2[b]=a; | |
| return(1); | |
| } | |
| } | |
| dist[a]=INF; | |
| return(0); | |
| } | |
| int MaxMatching() | |
| { | |
| int a,b,m=0; | |
| Fill(P1,-1); | |
| Fox(b,B) | |
| P2[b]=A; | |
| while (MatchBFS()) | |
| { | |
| Fox(a,A) | |
| if ((P1[a]<0) && (MatchDFS(a))) | |
| m++; | |
| } | |
| return(m); | |
| } | |
| int main() | |
| { | |
| // vars | |
| int T,t; | |
| int N=0; | |
| int i,j,a,b,p,r,z,ans; | |
| // testcase loop | |
| Read(T); | |
| Fox1(t,T) | |
| { | |
| // init | |
| Fox(i,K) | |
| con[i].clear(); | |
| Fox(i,A) | |
| con2[i].clear(); | |
| K=0; | |
| M.clear(); | |
| Fill(col0,0); | |
| Fill(col,-1); | |
| Fill(ind,-1); | |
| Fill(P1,-1); | |
| Fill(P2,-1); | |
| // input | |
| Read(N); | |
| while (N--) | |
| { | |
| Read(p),Read(r); | |
| i=Make(p),a=Make(p-r),b=Make(p+r); | |
| col0[i]=1; | |
| con[a].pb(b); | |
| con[b].pb(a); | |
| } | |
| // attempt to 2-color the graph (initially just from forced nodes) | |
| Fox(z,2) | |
| Fox(i,K) | |
| if (col[i]<0) | |
| { | |
| if ((!z) && (!col0[i])) | |
| continue; | |
| if (!DFS(i,0)) | |
| { | |
| ans=-1; | |
| goto Done; | |
| } | |
| } | |
| // construct the bipartite graph, ignoring forced nodes | |
| A=B=0; | |
| Fox(i,K) | |
| if (!col0[i]) | |
| { | |
| Fox(j,Sz(con[i])) | |
| if (col0[con[i][j]]) | |
| goto Skip; | |
| if (!col[i]) | |
| ind[i]=A++; | |
| else | |
| ind[i]=B++; | |
| Skip:; | |
| } | |
| Fox(i,K) | |
| if ((ind[i]>=0) && (!col[i])) | |
| { | |
| a=ind[i]; | |
| Fox(j,Sz(con[i])) | |
| { | |
| b=ind[con[i][j]]; | |
| if (b>=0) | |
| con2[a].pb(b); | |
| } | |
| } | |
| ans=A+B-MaxMatching()+1; | |
| // output | |
| Done:; | |
| printf("Case #%d: %d\n",t,ans); | |
| } | |
| return(0); | |
| } |