2 条题解

  • 5
    @ 2025-9-1 16:52:37

    本題的題意可以轉化為求一個圖的補圖的連通塊個數

    首先我們最開始想到的就是一個暴力,即對於任意兩個沒有邊的兩個點進行連邊,然後dfs查找連通塊

    但是這樣時間複雜度是 O(n2)O(n^2) 的,過不了

    那我們可以先把每一個點 uu 對於其他點 vv 連邊 然後我們考慮對於每一個點 uu 與其他沒有連邊的點 pp ,我們肯定是要將他們放入一個連通塊的,那然後我們就可以把它删除了對吧,因為後面肯定不會再用到他們了,也就是說我們找到每一個點 pp 然後再dfs(p)找到 pp 的所有沒連邊的點,再進行蒐索連過邊的點

    但是這樣太慢了,對於每一個點遍歷,時間複雜度還是 O(n2)O(n^2)

    所以我們需要一個可以快速支持删除的東西,那就是鏈表! 使用鏈表就可以成成功通過了

    • @ 2025-9-1 16:54:15

      兩岸一家親

  • 3
    @ 2025-9-1 16:55:27

    The meaning of this question can be transformed into finding the number of connected blocks in a supplementary graph of a graph

    The first thing we initially thought of was a brute force, which involves connecting any two points without edges, and then DFS searches for connected blocks But this time complexity is O(n2)O (n ^ 2) , it cannot pass

    Then we can first connect each point uu to the edges of other points vv

    Then we consider that for each point u and other non connected points pp, we must put them into a connected block, and then we can delete it, right? Because we will definitely not use them again in the future. That is to say, we find each point pp and then use DFS (p) to find all non connected points of pp, and then search for connected points

    But this is too slow. For each point traversal, the time complexity is still O(n2)O (n ^ 2)

    So we need something that can quickly support deletion, and that is a linked list! By using a linked list, it can be successfully passed

  • 1

信息

ID
371
时间
1000ms
内存
256MiB
难度
9
标签
(无)
递交数
65
已通过
4
上传者