We are given a problem: There is a party with A persons (numbered 0 to A-1). Big P is 0. There are B dancing couples, edges between persons. For each person (except Big P) we need to compute luckiness based on the described rules. Let's restate:
- Big P luckiness = 0.
- If a girl (person) gets to dance with Big P, she is 1-Lucky => luckiness=1.
- A person that dances with someone who has danced with a person who has danced with Big P is 2-Lucky, and so on. That is, essentially, shortest path distance from Big P? But careful: It says "If a person A is not Big P himself, and has danced with someone with luckiness X, and has not danced with anyone with Luckiness smaller than X, then A has luckiness X+1." So it sounds like the luckiness of a node is the minimum over all neighbors' luckiness plus 1, given that Big P has luckiness 0. However, there is also note: "Note3: If a person's luckiness cannot be determined from the above rules (he/she has not danced with anyone with finite luckiness), his/her luckiness is INF (infinity)." and then output -1 for such person.
Thus basically, we need to compute the shortest distance from node 0 to all other nodes in an undirected graph (since dancing couples are undirected). But wait: The rule: "If a person A ... has danced with someone with luckiness X, and has not danced with anyone with Luckiness smaller than X, then A has luckiness X+1." That implies that for each node, we consider the minimum luckiness among its neighbors, and then set its own luckiness to that minimum + 1. This is like a BFS distance: each node's distance from 0 is the smallest neighbor distance + 1. However, there could be cycles, but BFS works. So indeed the luckiness of a node i is the length (in edges) of the shortest path from 0 to i, if such a path exists; otherwise, it is INF (and we output -1). That's typical.
But there is nuance: The rule says "If a person's luckiness cannot be determined from the above rules (he/she has not danced with anyone with finite luckiness), his/her luckiness is INF". So if there is no path to 0, then it's INF -> output -1.
Also "No one has negative luckiness." Good.
So the problem reduces to: given an undirected graph with A nodes, edges given B times. Compute shortest distance from node 0 to all other nodes. Output distance for each i from 1 to A-1. If unreachable, output -1.
But check sample: Input:
5 6
0 1
0 2
3 2
2 4
4 3
1 2
Graph: nodes 0-4. Edges: 0-1,0-2,2-3,2-4,4-3,1-2. So distances: 0->0=0, 0->1 distance=1 (direct), 0->2 distance=1, 0->3: can go 0-2-3 => distance=2, also 0-2-4-3 => 3, so min=2. 0->4: 0-2-4 =>2. So output for nodes 1,2,3,4: 1,1,2,2. Sample output:
1
1
2
2. Matches.
Thus it's straightforward: BFS from 0.
Constraints: A ≤ 1000, B ≤ A*(A-1)/2, so up to ~500k edges. That's fine for BFS with adjacency list.
Input: first line: A B. Then B lines each with two numbers.
Output: A-1 lines, each luckiness (distance) for i=1..A-1, -1 if unreachable.
Edge cases: There might be multiple edges? Probably not, but we can ignore duplicates if they appear. But since B ≤ max edges, duplicates unlikely but we can handle.
We should implement BFS using queue, initial distances array set to -1 (or INF). Set dist[0]=0. Then BFS.
Complexities: O(A+B). Memory O(A+B).
Potential pitfalls: The graph is undirected; treat edges accordingly.
Thus answer: use BFS.
But wait: There's nuance: The rule says: "If a person A is not Big P himself, and has danced with someone with luckiness X, and has not danced with anyone with Luckiness smaller than X, then A has luckiness X+1." This suggests that we need to compute luckiness in a way that ensures that if a node is reachable via multiple paths, we take the minimum X. That's exactly the shortest path.
Also, note that there could be negative luckiness? No.
Hence solution is BFS.
But hold on: In problem description, it says "Output A-1 lines , ith line containing the luckiness of ith person. (1 ≤ i ≤ A-1) If luckiness cannot be calculated for a person - print "-1"(without the quotes)." So output for each i from 1 to A-1.
Implementation in Python:
def main():
import sys
from collections import deque
data = sys.stdin.read().strip().split()
if not data: return
it = iter(data)
A = int(next(it))
B = int(next(it))
adj = [[] for _ in range(A)]
for _ in range(B):
u = int(next(it))
v = int(next(it))
adj[u].append(v)
adj[v].append(u)
dist = [-1]*A
dist[0]=0
q = deque([0])
while q:
u = q.popleft()
for v in adj[u]:
if dist[v]==-1:
dist[v] = dist[u]+1
q.append(v)
# output for i from 1 to A-1
out_lines = [str(dist[i]) for i in range(1,A)]
sys.stdout.write("\n".join(out_lines))
if name=="main":
main()
That's it.
But perhaps there is a twist: If there is no path, but note that "If a person's luckiness cannot be determined from the above rules (he/she has not danced with anyone with finite luckiness), his/her luckiness is INF (infinity)." So we output -1. Good.
Potential issues: Node numbers 0 to A-1, so A-1 is inclusive.
Edge Cases: If A=1? Then there is only node 0, and we need to output A-1=0 lines. So nothing to output. But problem says A ≤ 1000, so A can be 1? Possibly, but B=0. We'll handle gracefully: if A==1, output nothing. Our code will produce an empty list and join results yields "" which we write, fine.
Thus answer.
But we need to produce a complete explanation as per "analysis" and "final". Since the user asked for "Big P" problem, but likely they want solution. However, it's phrased as a problem statement and might be from coding contest. They might expect code. But as ChatGPT, I should provide solution. However, the platform: The user gave a problem statement and sample input/output. They might be asking for solution. So we should output code and explanation. However, typically we output final answer with explanation.
Since instruction: "We need to respond with a solution." So I'll provide explanation and code.
Thus final answer will include approach, algorithm, complexity, and code in Python.
Let's produce that. ### Problem Understanding
We are given a party with A persons numbered from 0 to A-1. Person 0 is Big P, whose luckiness is defined as 0. Two persons that have danced together are connected by an undirected edge. For any other person:
- If they danced directly with Big P, they have luckiness
1.
- If they danced with someone whose luckiness is
X, and they have no neighbor with a smaller luckiness, then their own luckiness becomes X+1.
- In essence, the luckiness of a person is one more than the minimum luckiness among all their dance partners (if such a partner exists).
- If a person has no path to Big P (i.e., no neighbor with finite luckiness), their luckiness is
INF – we output -1.
Thus, the luckiness of each node is exactly the shortest path distance from node 0 in the undirected graph.
Solution Approach
We can compute the distances using Breadth-First Search (BFS) starting from node 0. BFS naturally gives the shortest number of edges to every reachable node. Unreachable nodes remain with distance -1 (treated as INF). After the BFS, we output the distance for nodes 1 to A-1 (one per line), printing -1 for those unreachable.
Complexity
- Time Complexity:
O(A + B) – each node and edge is processed at most once.
- Space Complexity:
O(A + B) for the adjacency list and BFS queue.
Edge Cases
- If
A = 1, there are no persons besides Big P, so no output lines are required.
- Graph may be disconnected; unreachable nodes yield
-1.
- Duplicate edges (if any) can be ignored because they do not affect distances; BFS will still work.
Implementation in Python
import sys
from collections import deque
def main() -> None:
data = sys.stdin.read().strip().split()
if not data:
return
it = iter(data)
A = int(next(it))
B = int(next(it))
# adjacency list
adj = [[] for _ in range(A)]
for _ in range(B):
u = int(next(it))
v = int(next(it))
adj[u].append(v)
adj[v].append(u)
# distances, -1 means not visited / INF
dist = [-1] * A
dist[0] = 0
q = deque([0])
while q:
u = q.popleft()
for v in adj[u]:
if dist[v] == -1:
dist[v] = dist[u] + 1
q.append(v)
# output for persons 1 .. A-1
out = "\n".join(str(dist[i]) for i in range(1, A))
sys.stdout.write(out)
if __name__ == "__main__":
main()
Sample Test
Input:
5 6
0 1
0 2
3 2
2 4
4 3
1 2
BFS distances:
- Node 1 → 1
- Node 2 → 1
- Node 3 → 2
- Node 4 → 2
Output:
1
1
2
2
This matches the sample.