Submission #456333

#TimeUsernameProblemLanguageResultExecution timeMemory
456333dutchFlood (IOI07_flood)C++17
54 / 100
231 ms25640 KiB
#include <bits/stdc++.h> using namespace std; const int N = 1e5+1, M = N+N, INF = 1e9; int n, m, x[N], y[N], a[M], b[M], up[N], dn[N], lf[N], rt[N], d[M*2]; bool vertical[M], vis[M*2]; vector<int> g[M*2], ans; signed main(){ cin.tie(0)->sync_with_stdio(0); cin >> n; for(int i=1; i<=n; ++i) cin >> x[i] >> y[i]; cin >> m; for(int i=1; i<=m; ++i){ int &u = a[i], &v = b[i]; cin >> u >> v; if((vertical[i] = (x[u] == x[v]))){ if(y[u] > y[v]) swap(u, v); up[u] = dn[v] = i; }else{ if(x[u] > x[v]) swap(u, v); rt[u] = lf[v] = i; } } vis[0] = 1; for(int i=1; i<=m+m; ++i){ int u = i, v; while(!vis[u]){ vis[u] = !(v = 0); int w = u; if(vertical[((u-1) % m) + 1]){ if(u > m){ // right u -= m; if(rt[a[u]] && !v) v = rt[a[u]] + m; if(dn[a[u]] && !v) v = dn[a[u]] + m; if(lf[a[u]] && !v) v = lf[a[u]]; if(up[a[u]] && !v) v = up[a[u]]; }else{ if(lf[b[u]] && !v) v = lf[b[u]]; if(up[b[u]] && !v) v = up[b[u]]; if(rt[b[u]] && !v) v = rt[b[u]] + m; if(dn[b[u]] && !v) v = dn[b[u]] + m; } }else{ if(u > m){ // up u -= m; if(up[b[u]] && !v) v = up[b[u]]; if(rt[b[u]] && !v) v = rt[b[u]] + m; if(dn[b[u]] && !v) v = dn[b[u]] + m; if(lf[b[u]] && !v) v = lf[b[u]]; }else{ if(dn[a[u]] && !v) v = dn[a[u]] + m; if(lf[a[u]] && !v) v = lf[a[u]]; if(up[a[u]] && !v) v = up[a[u]]; if(rt[a[u]] && !v) v = rt[a[u]] + m; } } if(v){ g[w].push_back(v); g[v].push_back(w); } u = v; } } deque<int> q; array<int, 2> todo[m+1]; int p = 1; for(int i=1; i<=m; ++i) todo[i] = {vertical[i] ? x[a[i]] : y[a[i]], i}; fill(d, d+m+m+1, INF); while(p <= m){ q.push_back(todo[p][1]); d[todo[p][1]] = 0; while(!q.empty()){ int u = q.front(); q.pop_front(); for(int v : g[u]) if(d[v] > d[u]){ d[v] = d[u]; q.push_front(v); } int v = u > m ? u-m : u+m; if(d[v] > d[u] + 1){ d[v] = d[u] + 1; q.push_back(v); } if(d[u] == d[v]) ans.push_back(min(u, v)); } while(p<=m && d[todo[p][1]] < INF) ++p; } sort(begin(ans), end(ans)); ans.resize(unique(begin(ans), end(ans)) - begin(ans)); cout << size(ans) << '\n'; for(int i : ans) cout << i << '\n'; }
#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...
#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...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...