Approach
Stack-based processing
For ABC304 C — Virus, the implementation keeps unresolved items in last-in, first-out order, often to match boundaries, parse structure, or maintain monotonic candidates.
- Define what every stack entry represents.
- Pop entries once the current item resolves or invalidates them.
- Push the current item with only the information later steps need.
Code notes
- 51 lines of C++ from the credited upstream file abc304_c.cpp.
- The implementation visibly relies on sequence storage, ordered lookup.
- 6 loop blocks detected.
Complexity
If each item is pushed and popped at most once, the stack work is linear.
Check the problem constraints before deciding whether this complexity will pass.
Use this to learn the idea, then write your own version.
1#include <iostream>2#include <map>3#include <stack>4#include <vector>5 6using namespace std;7using ui = unsigned int;8 9struct User {10 ui i;11 int x, y;12 bool virus;13};14 15int main() {16 ui n, d;17 cin >> n >> d;18 19 ui dd = d * d;20 21 vector<User> users(n);22 for (ui i = 0; i < n; i++) {23 users[i].i = i;24 cin >> users[i].x >> users[i].y;25 }26 27 map<ui, vector<ui>> um;28 for (auto& u : users)29 for (auto& uu : users)30 if (ui((u.x - uu.x) * (u.x - uu.x) + (u.y - uu.y) * (u.y - uu.y)) <=31 dd)32 um[u.i].push_back(uu.i);33 34 stack<ui> st;35 st.push(0);36 users[0].virus = true;37 while (!st.empty()) {38 ui i = st.top();39 st.pop();40 for (auto ti : um[i]) {41 if (!users[ti].virus) {42 st.push(ti);43 users[ti].virus = true;44 }45 }46 }47 48 for (auto& u : users) cout << (u.virus ? "Yes" : "No") << endl;49 50 return 0;51}