每次去掉一条边判断是否连通

本文介绍如何使用C++编程语言解决并行计算问题,通过实例展示并行算法的实现方法,包括多线程、并发编程和并行库的运用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#include<cstdio>
#include<cstring>
#define N 10005
int n,m,Q,l,r,x[N],y[N];
struct DSU{
	int f[505],c;
	int get(int x){return f[x]?f[x]=get(f[x]):x;}
	void Link(int x,int y){x=get(x),y=get(y); if (x!=y) f[x]=y,c++;}
}L[N],R[N],ans;
int main()
{
	scanf("%d%d",&n,&m);
	for (int i=1;i<=m;i++) scanf("%d%d",x+i,y+i),L[i]=L[i-1],L[i].Link(x[i],y[i]);
	for (int i=m;i;i--) R[i]=R[i+1],R[i].Link(x[i],y[i]);
	scanf("%d",&Q);
	while (Q--){
		scanf("%d%d",&l,&r); DSU ans=L[l-1];
		for (int i=1;i<=n;i++) if (R[r+1].f[i]) ans.Link(i,R[r+1].f[i]);
		printf("%d\n",n-ans.c);
		}
	return 0;
}






#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

int N;

struct dsu{
    int P[505],Sz[505],komp;

    void init(){
        for (int i=1; i<=N; i++){
            P[i] = i;
            Sz[i] = i;
        }
        komp = N;
    }

    int endparent(int x){
        while (x!=P[x]) x=P[x];
        return x;
    }

    bool spoji(int a,int b){
        a = endparent(a);
        b = endparent(b);
        if (a==b) return false;
        if (Sz[a] < Sz[b]){
            P[a] = b;
            Sz[b] += Sz[a];
        } else {
            P[b] = a;
            Sz[a] += Sz[b];
        }
        komp--;
        return true;
    }
};

int P1[10005],P2[10005];
bool LeftUseful[10005],RightUseful[10005];
int PrviLevoKoristan[10005],PrviDesnoKoristan[10005];
int P1lu[505],P2lu[505],M;
int P1ru[505],P2ru[505];
int Sol[505][505];

dsu samolevo,cepanje;

void ucitaj(){
    scanf("%d%d",&N,&M);
    for (int i=1; i<=M; i++){
        scanf("%d%d",P1+i,P2+i);
    }
}

void nadji_korisne(){
    int i,j;
    cepanje.init();
    for (i=1; i<=M; i++){
        if (cepanje.spoji(P1[i],P2[i])){
            LeftUseful[i] = true;
        }
    }
    cepanje.init();
    for (i=M; i>=1; i--){
        if (cepanje.spoji(P1[i],P2[i])){
            RightUseful[i] = true;
        }
    }
    j=0;
    for (i=1; i<=M; i++){
        if (LeftUseful[i]){
            j++;
            P1lu[j] = P1[i];
            P2lu[j] = P2[i];
        }
        PrviLevoKoristan[i] = j;
    }
    j=0;
    for (i=M; i>=1; i--){
        if (RightUseful[i]){
            j++;
            P1ru[j] = P1[i];
            P2ru[j] = P2[i];
        }
        PrviDesnoKoristan[i] = j;
    }
}

void napravi_sol(){
    samolevo.init();
    int i,j;
    for (i=0; i<N; i++){
        if (i>0) samolevo.spoji(P1lu[i],P2lu[i]);
        cepanje = samolevo;
        for (j=0; j<N; j++){
            if (j>0) cepanje.spoji(P1ru[j],P2ru[j]);
            Sol[i][j] = cepanje.komp;
        }
    }
}

void resi(){
    int i,j,Q,l,r;
    scanf("%d",&Q);
    while (Q--){
        scanf("%d%d",&l,&r);
        l--;
        r++;
        printf("%d\n",Sol[PrviLevoKoristan[l]][PrviDesnoKoristan[r]]);

    }
}

int main(){
    ucitaj();
    nadji_korisne();
    napravi_sol();
    resi();
    return 0;
}

import copy from copy import deepcopy class Graph: def __init__(self, vertices): self.V = vertices # 图中顶点的个数 self.graph = [[] for _ in range(self.V)] # 图中存储的二维列表,初始化为空 # 添加(u,v) def addEdge(self, u, v): self.graph[u].append(v) self.graph[v].append(u) # 去除(u,v) def removeEdge(self, u, v): self.graph[u].remove(v) self.graph[v].remove(u) # 深度优先搜索访问图中顶点 def DFS(self, v, visited): visited[v] = True for neighbor in self.graph[v]: if visited[neighbor] == False: self.DFS(neighbor, visited) # 计算图的连通分支数,使用到深度优先(DFS)算法 def connectedComponents(self): visited = [False] * (self.V) count = 0 for node in range(self.V): if visited[node] == False: self.DFS(node, visited) count += 1 return count # 判断图中(u,v)是否是桥 def isBridge(self, u, v): # 注意在程序结束时保持图不变化,图的变化将是桥的判定的副作用,需要避免 copy_g = deepcopy(self) # 原图的联通分支数 component_num = copy_g.connectedComponents() copy_g.removeEdge(u, v) aft_component_num = copy_g.connectedComponents() if aft_component_num - component_num > 0: return True else: return False # 欧拉图的判别 def isEulerian(self): # 判断是否连通 if self.connectedComponents() != 1: return False # 检查每个顶点的度数是否为偶数 for i in range(self.V): if len(self.graph[i]) % 2 != 0: return False return True # Fleury算法求欧拉回路 def fleuryAlgorithm(self, startVertex): if not self.isEulerian(): return None # 创图的深拷贝以避免修改原图 g = deepcopy(self) path = [] current_vertex = startVertex path.append(current_vertex) while len(path) > 0: # 检查当前顶点是否有未访问的 if len(g.graph[current_vertex]) == 0: break # 尝试找到非桥 non_bridge_found = False for neighbor in g.graph[current_vertex]: if not g.isBridge(current_vertex, neighbor): non_bridge_found = True # 移除该 g.removeEdge(current_vertex, neighbor) current_vertex = neighbor path.append(current_vertex) break # 如果没有找到非桥,则选择任意(桥) if not non_bridge_found: for neighbor in g.graph[current_vertex]: g.removeEdge(current_vertex, neighbor) current_vertex = neighbor path.append(current_vertex) break # 检查路径是否形成回路 if path[0] == path[-1] and len(path) == g.V + 1: return path else: return None解释每一步
06-17
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值