首页 > 代码库 > 缩点(有待题目检验,代码不可靠)

缩点(有待题目检验,代码不可靠)

#include <bits/stdc++.h>
using namespace std;

const int maxn = 2e6;
int head[maxn];
int bel[maxn];
int w[maxn];
bool vis[maxn];
int cnt = 0, n;

struct node{
    int to, nxt;
}e[maxn];

void init(){
    memset(head, -1, sizeof head);
    memset(vis, false, sizeof vis);
    cnt = 0;
}

void addedge(int u, int v){
    e[cnt].to = u;
    e[cnt].nxt = head[u];
    head[u] = cnt++;
}

void bfs(){//缩点
    queue<int>q;
    q.push(1);
    memset(vis, false, sizeof vis);
    while(!q.empty()){
        int u = q.front();
        q.pop();
        if(vis[u]) continue;
        vis[u] = true;
        for(int i = head[u]; ~i; i = e[i].nxt){
            int v = e[i].to;
            if(!vis[v] && w[u] == w[v]){
                bel[v] = bel[u];
                q.push(v);
            }
        }
    } 
    for(int i = 1; i <= n; ++i){
        for(int j = head[i]; ~j; j = e[j].nxt){
            int v = e[j].to;
            if(bel[v] != bel[i])
                addedge(bel[i], bel[v]);
        }
    }
}

int main(){
     cin >> n;
    for(int i = 1; i <= n; ++i) bel[i] = i;
}

 

缩点(有待题目检验,代码不可靠)