Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- //
- #define ll long long
- #define ull unsigned long long
- #define mx 100010
- #define mod 1000000007
- #define inf INT_MAX
- #define pi acos(-1)
- #define endl '\n'
- #define pb push_back
- #define pll pair<ll, ll>
- #define Fast ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)
- //
- vector<ll> visited(mx), TTL(mx);
- void bfs(ll node, ll ttl, vector<ll> adj[]) {
- queue<ll> q;
- q.push(node);
- TTL[node] = ttl;
- visited[node] = 1;
- while (!q.empty()) {
- ll cNode = q.front();
- q.pop();
- for (ll i = 0; i < adj[cNode].size(); i++) {
- if (!visited[adj[cNode][i]] && TTL[cNode] > 0) {
- q.push(adj[cNode][i]);
- visited[adj[cNode][i]] = 1;
- TTL[adj[cNode][i]] = TTL[cNode] - 1;
- }
- }
- }
- }
- int main() {
- ll tc = 1;
- while (1) {
- ll nc;
- cin >> nc;
- if (nc == 0) break;
- set<ll> nodeCount;
- vector<ll> adj[mx];
- for (ll i = 0; i < nc; i++) {
- ll a, b;
- cin >> a >> b;
- adj[a].pb(b);
- adj[b].pb(a);
- nodeCount.insert(a);
- nodeCount.insert(b);
- }
- while (1) {
- ll a, b;
- cin >> a >> b;
- if (a == 0 && b == 0) break;
- for (auto it = nodeCount.begin(); it != nodeCount.end(); it++) {
- visited[*it] = 0;
- TTL[*it] = -1;
- }
- auto it = nodeCount.find(a);
- if (it != nodeCount.end()) {
- bfs(a, b, adj);
- ll count = 0;
- for (auto it = nodeCount.begin(); it != nodeCount.end(); it++) {
- if (visited[*it] == 0) count++;
- }
- cout << "Case " << tc++ << ": " << count << " nodes not reachable from node " << a << " with TTL = " << b << "." << endl;
- }
- else {
- cout << "Case " << tc++ << ": " << nodeCount.size() << " nodes not reachable from node " << a << " with TTL = " << b << "." << endl;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement