Skip to content

Commit 756b344

Browse files
committed
Solved Issue #548: The Tag Game (Graph BFS)
1 parent 9a1edc4 commit 756b344

1 file changed

Lines changed: 118 additions & 0 deletions

File tree

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
/*
2+
Submission Link:
3+
https://codeforces.com/contest/813/submission/356727323
4+
5+
6+
7+
Problem: The Tag Game
8+
Link: https://codeforces.com/contest/813/problem/C
9+
Author: Krishna200608
10+
11+
Approach:
12+
13+
1. The game is played on a tree with Alice starting at node 1 and Bob at node x.
14+
2. Alice wants to catch Bob as quickly as possible, while Bob wants to delay.
15+
3. Bob’s optimal strategy is to move to a node that he can reach before Alice
16+
and that is as far from the root as possible.
17+
4. Once Bob reaches such a node, Alice is forced to travel down that path
18+
and back, increasing the total moves.
19+
5. Use two BFS traversals to calculate distances from Alice (node 1)
20+
and Bob (node x) to all nodes.
21+
6. For every node Bob reaches earlier than Alice, compute 2 × distance from root.
22+
7. The maximum such value is the number of moves Alice makes.
23+
24+
Complexity:
25+
Time: O(N)
26+
Space: O(N)
27+
*/
28+
29+
#include<bits/stdc++.h>
30+
using namespace std;
31+
32+
#define MOD 1000000007
33+
const int M = 1e9+7;
34+
const double PI = acos(-1.0);
35+
#define INF 1e18
36+
#define pb push_back
37+
#define ppb pop_back
38+
#define ll long long
39+
#define no cout << "NO" << endl;
40+
#define yes cout << "YES" << endl;
41+
#define ff first
42+
#define ss second
43+
#define inn(x) int x; cin >> x;
44+
#define ill(x) ll x; cin >> x;
45+
#define all(x) x.begin(),x.end()
46+
#define in(a) for(int i = 0; i < (int)a.size(); i++) cin >> a[i];
47+
#define out(a) for(int i = 0; i < (int)a.size(); i++) cout << a[i] << " ";
48+
typedef vector<int> vi;
49+
typedef vector<ll> vll;
50+
#define ceil_div(n, x) (((n) % (x) == 0) ? ((n) / (x)) : ((n) / (x) + 1))
51+
#define debug(x) cout << "x -> " << x << endl;
52+
#define outt(x) cout << x << endl;
53+
#define endl "\n"
54+
55+
vi adj[200005];
56+
int d1[200005];
57+
int d2[200005];
58+
59+
void bfs(int start, int *dist, int n) {
60+
for(int i = 1; i <= n; i++) dist[i] = -1;
61+
dist[start] = 0;
62+
63+
queue<int> q;
64+
q.push(start);
65+
66+
while(!q.empty()) {
67+
int u = q.front(); q.pop();
68+
for(int v : adj[u]) {
69+
if(dist[v] == -1) {
70+
dist[v] = dist[u] + 1;
71+
q.push(v);
72+
}
73+
}
74+
}
75+
}
76+
77+
void solve() {
78+
79+
inn(n) inn(x)
80+
81+
for(int i = 1; i <= n; i++)
82+
adj[i].clear();
83+
84+
for(int i = 0; i < n - 1; i++) {
85+
inn(u)
86+
inn(v)
87+
adj[u].pb(v);
88+
adj[v].pb(u);
89+
}
90+
91+
bfs(1, d1,n);
92+
bfs(x, d2, n);
93+
94+
int ans = 0;
95+
for(int i = 1; i <= n; i++) {
96+
if(d2[i] < d1[i]) {
97+
ans = max(ans, 2 * d1[i]);
98+
}
99+
}
100+
101+
outt(ans);
102+
}
103+
104+
signed main() {
105+
ios_base::sync_with_stdio(false);
106+
cin.tie(nullptr);
107+
108+
auto begin = chrono::high_resolution_clock::now();
109+
110+
int t = 1;
111+
while(t--) solve();
112+
113+
auto end = chrono::high_resolution_clock::now();
114+
auto elapsed = chrono::duration_cast<chrono::nanoseconds>(end - begin);
115+
cerr << "Time measured: " << elapsed.count() * 1e-6 << "ms";
116+
117+
return 0;
118+
}

0 commit comments

Comments
 (0)