An SNS has N users - User 1, User 2, \cdots, User N.
Between these N users, there are some relationships - M friendships and K blockships.
For each i = 1, 2, \cdots, M, there is a bidirectional friendship between User A_i and User B_i.
For each i = 1, 2, \cdots, K, there is a bidirectional blockship between User C_i and User D_i.
We define User a to be a friend candidate for User b when all of the following four conditions are satisfied:
a \neq b.
There is not a friendship between User a and User b.
There is not a blockship between User a and User b.
There exists a sequence c_0, c_1, c_2, \cdots, c_L consisting of integers between 1 and N (inclusive) such that c_0 = a, c_L = b, and there is a friendship between User c_i and c_{i+1} for each i = 0, 1, \cdots, L - 1.
For each user i = 1, 2, ... N, how many friend candidates does it have?
Constraints
All values in
input are integers.
2 ≤ N ≤ 10^5
0 \leq M \leq 10^5
0 \leq K \leq 10^5
1 \leq A_i, B_i \leq N
A_i \neq B_i
1 \leq C_i, D_i \leq N
C_i \neq D_i
(A_i, B_i) \neq (A_j, B_j) (i \neq j)
(A_i, B_i) \neq (B_j, A_j)
(C_i, D_i) \neq (C_j, D_j) (i \neq j)
(C_i, D_i) \neq (D_j, C_j)
(A_i, B_i) \neq (C_j, D_j)
(A_i, B_i) \neq (D_j, C_j)
Input
Input is given from Standard Input in the following format:
N M K
A_1 B_1
\vdots
A_M B_M
C_1 D_1
\vdots
C_K D_K
Output
Print the answers in order, with space in between.
We are given an SNS with N users, M bidirectional friendships, and K bidirectional blockships.
Define: User a is a friend candidate for User b if all four conditions hold:
a != b.
There is no friendship between a and b.
There is no blockship between a and b.
There exists a path of friendships from a to b (i.e., they are in the same connected component of the friendship graph).
Essentially, we have an undirected graph G (friendship edges). Blockships are like prohibited direct connections: they don't affect connectivity; they only affect the "friend candidate" condition, together with direct friendships.
For each user i, we need to count how many other users j (j != i) satisfy:
i and j are in same friendship component (connected via friendship edges)
i and j are not directly friends (no edge in G between them)
i and j are not blocked (no blockship between them)
So basically, for each user i, within its component of G (let component size = comp_size(i)), we subtract:
number of direct friends of i (degree in G)
number of users blocked with i that are also in same component, but not already counted (blocked may include direct friends? But constraints ensure that there is no pair that is both friendship and blockship simultaneously. So disjoint sets)
also i itself is not counted.
But careful: The condition a != b is automatically not counted as friend or block, so we don't subtract i. So for each i, answer = (size_of_component_i - 1) - (number_of_friends_in_component) - (number_of_blocked_in_same_component).
Because any user in same component is reachable via friendship path (condition 4), and we must exclude those directly connected by friendship (condition 2) and those with blockship (condition 3). Also, note: If there is both a blockship and a friendship? Not possible due to constraints. So disjoint.
Thus answer for i = comp_size[comp(i)] - 1 - deg[i] - block_in_comp_count[i], where block_in_comp_count[i] is number of users in same component that have a blockship with i.
But careful: Blockships are bidirectional, so if we count block_in_comp_count[i] for each i, each blockship will be counted twice (once for each endpoint) in the overall sum, but that's fine because we subtract for each user separately.
Thus we need for each user i, number of blockships with users in same component as i.
Given that K up to 1e5, N up to 1e5, we can compute component ids via DSU (union find) on friendship edges.
Then we need for each user i, count of blocked neighbors in same component.
We can iterate over all blockship pairs (c,d). For each, find component id of c and d. If they are same, then for both c and d, we increment the count of block_in_comp. If they are in different components, then this blockship does not affect candidate counts because condition 4 fails (they are not connected via friendships), so they are not candidates anyway. So only count when same component.
Thus compute comp_sizes via DSU, then deg (friendship degree) from input.
Then for each user, answer = comp_size[comp[i]] - 1 - deg[i] - same_comp_block_count[i].
Edge Cases: There may be self? No.
Check examples.
Example 1:
N=4 M=4 K=1
Friendships: (2,1) (1,3) (3,2) (3,4) => Graph: component includes all 4? Actually edges: 1-2,1-3,3-2,3-4 => connected. So comp size=4.
Degrees: deg1=2 (friends:2,3), deg2=2 (1,3), deg3=3 (1,2,4), deg4=1 (3).
Blockship: (4,1) => block between 4 and 1, same component. So block_count: for 1: block_in_comp=1; for 4: block_in_comp=1.
Compute answers:
i=1: 4-1 -2 -1 =0
i=2: 4-1 -2 -0 =1 (block? none) => 1
i=3: 4-1 -3 -0 =0
i=4: 4-1 -1 -1 =1 => matches output: 0 1 0 1. Good.
Example2:
N=5 M=10 K=0. Graph is complete? Actually friendships: many edges. Likely all pairs are friends? Actually check edges: 1-2,1-3,1-4,1-5,3-2,2-4,2-5,4-3,5-3,4-5. That's all possible pairs? Let's list all pairs: (1,2),(1,3),(1,4),(1,5),(2,3),(2,4),(2,5),(3,4),(3,5),(4,5). Indeed all 10 edges. So component size =5, each deg=4. No blocks. Then answer per i = 5-1 -4 =0. Output all 0. Good.
Example3:
Let's test with given output to verify. N=10 M=9 K=3.
Friendships:
10-1
6-7
8-2
2-5
8-4
7-3
10-9
6-4
5-8
So edges:
1-10, 10-9 (so 1,10,9 connected)
6-7, 7-3 (so 6,7,3 connected)
8-2,2-5,5-8,8-4,6-4 (so 8,2,5,4,6 all connected? Let's check: 8-2, 8-5? Actually 5-8 is given, yes. 8-4, and 6-4. So 8,2,5,4,6 connected. Also 6 is also connected to 7 via 6-7? Yes 6-7 is an edge, so 6 connects to 7, and 7-3, so all nodes except maybe? Let's see: The edges listed:
1-10
6-7
8-2
2-5
8-4
7-3
10-9
6-4
5-8
So nodes:
1 connected to 10, 10 to 9 => component A: {1,10,9}
Now others: 6-7 connects 6 and 7; 7-3 connects 7 and 3; 6-4 connects 6 and 4; 8-2, 2-5, 5-8, 8-4 => This connects 8,2,5,4; and 4 is connected to 6, so 6 connects to 4, thus all these are connected: {2,4,5,6,7,3,8}? Let's see: 6-7, 7-3, so 6,7,3; 6-4, so 4; 8-4, so 8; 8-2,2-5,5-8, so 2,5. So component B: {2,3,4,5,6,7,8}. That's 7 nodes. So total N=10, component A size=3, component B size=7.
Degrees: compute each:
Edges: list:
(10,1) => deg10+1, deg1+1
(6,7) => deg6+1, deg7+1
(8,2) => deg8+1, deg2+1
(2,5) => deg2+1, deg5+1
(8,4) => deg8+1, deg4+1
(7,3) => deg7+1, deg3+1
(10,9) => deg10+1, deg9+1
(6,4) => deg6+1, deg4+1
(5,8) => deg5+1, deg8+1
So count deg:
1:1
2: (8,2) and (2,5) =>2
3: (7,3) =>1
4: (8,4) and (6,4) =>2
5: (2,5) and (5,8) =>2
6: (6,7) and (6,4) =>2
7: (6,7) and (7,3) =>2
8: (8,2) and (8,4) and (5,8) =>3
9: (10,9) =>1
10: (10,1) and (10,9) =>2
Now block pairs (K=3):
2 6
7 5
3 1
So:
(2,6): both in component B? 2 in B, 6 in B, same component. So block_count for 2++,6++.
(7,5): both in B, same component. So block_count for7++,5++.
(3,1): 3 in B, 1 in A, different components. So not counted for either.
Now compute comp sizes: comp of A: {1,10,9} size=3; comp of B: {2,3,4,5,6,7,8} size=7.
For each user i:
i=1: comp size=3, deg=1, block=0 => 3-1-1=1? Actually formula: size-1 - deg - block = 2 -1 -0=1? Wait 3-1=2, minus deg1=1 gives 1, minus block0=1. Output example first number is 1. Good.
i=2: size=7, deg=2, block=1 => 7-1-2-1=3 => output says 3? Actually example output: "1 3 5 4 3 3 3 3 1 0". So second number is 3. Good.
i=3: size=7, deg=1, block=0 => 7-1-1-0=5 => output says 5. Good.
i=4: size=7, deg=2, block=0 => 7-1-2=4 => output says 4. Good.
i=5: size=7, deg=2, block=1 => 7-1-2-1=3 => output says 3. Good.
i=6: size=7, deg=2, block=1 => 3 => output says 3. Good.
i=7: size=7, deg=2, block=1 => 3 => output says 3. Good.
i=8: size=7, deg=3, block=0 => 7-1-3=3 => output says 3. Good.
i=9: size=3, deg=1, block=0 => 3-1-1=1 => output says 1. Good.
i=10: size=3, deg=2, block=0 => 3-1-2=0 => output says 0. Good.
Matches.
Thus solution is straightforward: DSU for friendship graph, compute component sizes, degree array, block count per node (only if same component).
Implementation steps:
Read N, M, K.
Initialize DSU for N.
For each of M friendships: read a,b; union(a-1,b-1); increment deg[a-1]++, deg[b-1]++.
After processing all friendships, we have DSU with parents and we can compute comp size: For each node i, find root, and maintain an array comp_size[root] = count.
Better: after unions, for each node i, find its root, and increment comp_size[root] (or use DSU size array if we maintain during union). Standard DSU with size array.
Then process K blockships: For each (c,d) read c,d (convert to 0-index). Find root of c and d. If same root, then increment block_count[c]++, block_count[d]++.
Finally for each i: root = find(i); answer = comp_size[root] - 1 - deg[i] - block_count[i].
Print answers space-separated.
Complexities: O(N + M + K * α(N)). Good.
Potential pitfalls: The DSU must be sized to N. Note: M and K could be 0. Also ensure we use long or int? N up to 1e5, comp size up to 1e5, deg up to N-1, block_count up to maybe K but K up to 1e5, sum deg <= M*2 = 2e5, so comp_size-1-deg-block may be negative? Could it be negative? Let's see: In a component, each node i can have at most (comp_size - 1) potential others. Subtract deg (direct friends) and block (same component blocks). But block includes only those in same component. There might be overlapping? No overlapping as per constraints. So deg + block <= comp_size - 1? Possibly not: Could a node have deg + block > comp_size - 1? deg counts only direct friends in same component (since all friends are in same component because they are connected via direct edge; so definitely same component). block counts other nodes in same component with blockship. However it's possible that a node is directly friends with many nodes and also has blockships with many nodes, and the set of friends and blocks may overlap? No, because a pair cannot be both friend and block. So the sets are disjoint. However the union of friends and blocks might exceed the total other nodes in the component if there is also the node itself? It could be that deg + block > comp_size - 1? Let's check worst case: comp_size = N, deg can be up to N-1 (if fully connected), block could be 0, then deg = N-1, so comp_size-1 - (N-1) = 0. But if block >0, then deg + block would exceed N-1 because if deg = N-1, block must be 0 as no other nodes exist. If deg less, block could be up to (comp_size - 1 - deg) but not more because block pairs are distinct from friends and cannot include self. But is there any guarantee that the input doesn't include blockship with a node that is also a friend? No, constraints say (A_i,B_i) ≠ (C_j,D_j) and (A_i,B_i) ≠ (D_j,C_j). So no overlapping. But could a node have block count that plus deg exceed comp_size-1? Suppose comp_size=5, node i has deg=3, block=2 => total 5, but comp_size-1=4, so would be negative. But that would imply that node i has 3 friends and 2 blocked nodes, but total distinct other nodes is 4. If deg=3 and block=2, that's 5 distinct other nodes, impossible because there are only 4 other nodes. Since sets are disjoint, maximum distinct count is comp_size-1. So deg + block cannot exceed comp_size-1. But is it enforced by constraints? Input could theoretically give such a scenario? Let's think: In a component of size S, node i can have at most S-1 possible friends (direct edges). The blockships are also pairs, and they are disjoint from friendships. So if node i already has S-1 friends, then there are no other nodes left to block. But constraints do not prevent a node from having blockships with nodes that are also friends? They do: they say (A_i,B_i) ≠ (C_j,D_j) and (A_i,B_i) ≠ (D_j,C_j). So a pair cannot be both friendship and blockship. But that doesn't prevent node i from having degree S-1 and also having blockships with some nodes? That would require that node i has a blockship with some node j, but then node i also has friendship with node j? That's forbidden. So if deg=S-1, then node i is directly friends with every other node in component. Then there is no other node left to have a blockship with because all other nodes are already friends. So block_count must be 0. So indeed deg+block <= comp_size-1. However input could provide a blockship between i and j where i is in same component but j is not? That would not be counted in block_count because we only count same component blockships. But if j is in same component, then it would be a friend? Not necessarily, but if i already has S-1 friends, it is friends with every other node, so any other node j would be a friend, so blockship would be forbidden. So that case cannot happen because input ensures no overlap. So yes, answer non-negative.
Thus solution is safe.
Implementation details: DSU with path compression and union by size.
Edge Cases: Large N, ensure efficient input reading (use sys.stdin.buffer).
We'll implement in Python.
Now let's write code.
Plan:
import sys
def main():
import sys
input = sys.stdin.buffer.readline
N, M, K = map(int, input().split())
parent = list(range(N))
size = [1]*N
deg = [0]*N
code
def find(x):
while parent[x] != x:
parent[x] = parent[parent[x]]
x = parent[x]
return x
def union(x,y):
rx = find(x)
ry = find(y)
if rx == ry: return
if size[rx] < size[ry]:
rx, ry = ry, rx
parent[ry] = rx
size[rx] += size[ry]
# read friendships
for _ in range(M):
a,b = map(int, input().split())
a -= 1; b -= 1
deg[a] += 1
deg[b] += 1
union(a,b)
# after unions, we need comp sizes for each root.
# But note: size array in DSU might be only accurate for roots. So we need to get root for each node to get comp size.
# Actually size[root] holds component size. But after unions, some nodes may have parent not root. So we need to get root.
# We'll compute comp_size array: for each node i, root = find(i) and then comp_size[i] = size[root] (or store directly per root).
# We'll compute an array comp_size_for_node = [0]*N, then for i in range(N): root = find(i); comp_size_for_node[i] = size[root].
# However, if we call find on each node, it will compress path. That's fine.
# Alternatively we can keep a dict for root sizes.
block_cnt = [0]*N
for _ in range(K):
c,d = map(int, input().split())
c -= 1; d -= 1
rc = find(c)
rd = find(d)
if rc == rd:
block_cnt[c] += 1
block_cnt[d] += 1
# compute answer per node
ans = []
for i in range(N):
root = find(i) # after previous finds, path compressed
comp_sz = size[root] # size of component (root's size)
ans.append(str(comp_sz - 1 - deg[i] - block_cnt[i]))
print(' '.join(ans))
if name == "main":
main()
Check with examples.
Let's test quickly mentally.
Example 1: Works.
Potential issue: DSU size array might not be updated correctly if we use union that updates size[rx] only when merging. In find after union, root's size is correct. But if we call find on many nodes, we need to ensure that we don't use size[i] for non-roots; we always use size[find(i)]. So ans compute uses root = find(i); comp_sz = size[root]; correct.
But note: In Python recursion? We'll use iterative find.
Now about memory: fine.
Complexities: O((N+M+K) α(N)) time.
We must consider that for each blockship we call find twice. That's okay.
Double-check constraints: It's possible that K is large but still fine.
Thus solution is straightforward.
But wait: The problem statement says "there is not a blockship between User a and User b" condition. And also "there exists a sequence of friendships" i.e., they are in same connected component of friendship graph. So our approach is correct.
Thus answer is as above.
Let's also consider if there can be multiple edges? No, constraints guarantee no duplicate friendships, and no (A_i,B_i) = (B_j,A_j) etc.
Thus final solution.
But is there any nuance about self? No.
Thus we can produce final answer.
We should write a thorough explanation in the final output.
One more thing: Might there be a scenario where a and b are in same component but the path may include a blockship? No, path only uses friendships, blockships don't affect connectivity. So DSU on friendships is correct.
Thus final solution is as described.
Now produce final response with code and explanation. ```python
import sys
def main() -> None:
input = sys.stdin.buffer.readline
N, M, K = map(int, input().split())
code
# DSU
parent = list(range(N))
size = [1] * N # size of component for roots
def find(x: int) -> int:
while parent[x] != x:
parent[x] = parent[parent[x]] # path halving
x = parent[x]
return x
def union(x: int, y: int) -> None:
rx = find(x)
ry = find(y)
if rx == ry:
return
if size[rx] < size[ry]:
rx, ry = ry, rx
parent[ry] = rx
size[rx] += size[ry]
deg = [0] * N # number of direct friends
# read friendships
for _ in range(M):
a, b = map(int, input().split())
a -= 1
b -= 1
deg[a] += 1
deg[b] += 1
union(a, b)
# block counts only for pairs in the same friendship component
block_cnt = [0] * N
for _ in range(K):
c, d = map(int, input().split())
c -= 1
d -= 1
rc = find(c)
rd = find(d)
if rc == rd:
block_cnt[c] += 1
block_cnt[d] += 1
# compute answers
ans = []
for i in range(N):
root = find(i) # component root
comp_sz = size[root] # size of the component
val = comp_sz - 1 - deg[i] - block_cnt[i]
ans.append(str(val))
print(' '.join(ans))