[2020 Niuke Multi-School] 2020 Niuke Summer Multi-School Training Camp (the third session) G-Operating on a Graph-Stupid Violence Problem

G-Operating on a Graph

Topic link

General meaning

Give you a picture with nnn points,mmm edges, the subscript of the point is from0 → n − 1 0 \rightarrow n-10n1
for pointiii , it belongs toi − group i-groupat the beginningig r o u p
total operationsqqq times, giving annn foreach operationn , combine all withn − group n-groupng r o u p directly connectedgroup groupg r o u p joinsn − group n-groupnIn g r o u p ,
after all operations are over, find thegroup groupwhere each point is locatedgroup

Simple thinking direction

Using STLthe listconnection, listanalog queue, and then do with disjoint-set

Specific ideas

First, group groupG R & lt O U P ............ not that it ............ disjoint-set
except that it may not provide side suddenly broughtuniteonly one layerunite(theBFS BFSConsidering from the perspective of B F S , the meaning of this layer)
then it can be for eachgroup groupG R & lt O U P on a savequeue, and then each time thequeueto layer ofBFS BFSBFS

But considering two group groupsAfter the union of g r o u p leads to one of thegroup groupsG R & lt O U P ofqueuedata to be merged with a further, andqueuethe combined efficiency is too low, it is usedlistto simulatequeue, sincelistthere aresplicea method, the efficiency is very high

Secondly, to avoid duplication of BFS BFSB F. S , thus increasing thevisitarray

AC code

#include <bits/stdc++.h>

using namespace std;

const int MAXN = 8e5 + 100;

int f[MAXN];
list<int> lists[MAXN];
bool visit[MAXN];
vector<int> node[MAXN];

int finds(int x) {
    
    
    return x == f[x] ? x : f[x] = finds(f[x]);
}

void unite(int x, int y) {
    
    
    int rx = finds(x);
    int ry = finds(y);
    if (rx != ry) {
    
    
        f[rx] = ry;
        lists[ry].splice(lists[ry].end(), lists[rx]);
    }
}

void init(int b, int e) {
    
     // 初始化函数,范围为 [b, e)
    for (int i = b; i < e; i++)
        f[i] = i;
}

void bfs(int cur) {
    
    
    if (finds(cur) != cur) return;
    int size = lists[cur].size();

    for (int i = 0; i < size; ++i) {
    
    
        auto explorer = lists[cur].front();
        for (auto item : node[explorer]) {
    
    
            unite(item, cur);
            if (visit[item]) continue;
            lists[cur].push_back(item);
            visit[item] = true;
        }
        lists[cur].pop_front();
    }
}

void solve() {
    
    
    int T;
    cin >> T;
    for (int ts = 0; ts < T; ++ts) {
    
    
        int n, m;
        cin >> n >> m;
        memset(visit, false, sizeof(bool) * (n + 5));
        init(0, n + 5);
        for (int i = 0; i < n + 5; ++i) {
    
    
            node[i].clear();
            lists[i].clear();
            lists[i].push_back(i);
        }

        int u, v;
        for (int i = 0; i < m; ++i) {
    
    
            cin >> u >> v;
            node[u].push_back(v);
            node[v].push_back(u);
        }

        int q;
        cin >> q;
        for (int i = 0; i < q; ++i) {
    
    
            cin >> u;
            bfs(u);
        }

        for (int i = 0; i < n; ++i)
            cout << finds(i) << " \n"[i == n - 1];
    }
}

signed main() {
    
    
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
#ifdef ACM_LOCAL
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
    int test_index_for_debug = 1;
    char acm_local_for_debug;
    while (cin >> acm_local_for_debug) {
    
    
        if (acm_local_for_debug == '$') exit(0);
        cin.putback(acm_local_for_debug);
        if (test_index_for_debug > 20) {
    
    
            throw runtime_error("Check the stdin!!!");
        }
        auto start_clock_for_debug = clock();
        solve();
        auto end_clock_for_debug = clock();
        cout << "Test " << test_index_for_debug << " successful" << endl;
        cerr << "Test " << test_index_for_debug++ << " Run Time: "
             << double(end_clock_for_debug - start_clock_for_debug) / CLOCKS_PER_SEC << "s" << endl;
        cout << "--------------------------------------------------" << endl;
    }
#else
    solve();
#endif
    return 0;
}

Guess you like

Origin blog.csdn.net/m0_43448982/article/details/107432046