51NOD 1076 2条不相交的路径 【点双连通分量】

图算法实战
本文介绍了一种利用图算法解决实际问题的方法,通过寻找无向图中两个顶点间是否存在两条不相交路径的问题,展示了如何使用Tarjan算法进行点双连通分量的计算。
1076 2条不相交的路径
基准时间限制:1 秒 空间限制:131072 KB 分值: 40 难度:4级算法题 收藏  关注
给出一个无向图G的顶点V和边E。进行Q次查询,查询从G的某个顶点V[s]到另一个顶点V[t],是否存在2条不相交的路径。(两条路径不经过相同的边)
(注,无向图中不存在重边,也就是说确定起点和终点,他们之间最多只有1条路)
Input1行:2个数M N,中间用空格分开,M是顶点的数量,N是边的数量。(2 <= M <= 25000, 1 <= N <= 50000)
第2 - N + 1行,每行2个数,中间用空格分隔,分别是N条边的起点和终点的编号。例如2 4表示起点为2,终点为4,由于是无向图,所以从42也是可行的路径。
第N + 2行,一个数Q,表示后面将进行Q次查询。(1 <= Q <= 50000)
第N + 3 - N + 2 + Q行,每行2个数s, t,中间用空格分隔,表示查询的起点和终点。
Output
共Q行,如果从s到t存在2条不相交的路径则输出Yes,否则输出No。
Input示例
4 4
1 2
2 3
1 3
1 4
5
1 2
2 3
3 1
2 4
1 4
Output示例
Yes
Yes
Yes
No
No
相关问题
3条不相交的路径 1280
李陶冶 (题目提供者)

画画图就不难发现 满足条件的s和t必然在一个环中
求点双连通分量 判断s,t判断是否属于同一分量即可


#include<iostream>
#include<stdlib.h>
#include<stdio.h>
#include<string>
#include<vector>
#include<deque>
#include<queue>
#include<algorithm>
#include<set>
#include<map>
#include<stack>
#include<time.h>
#include<math.h>
#include<list>
#include<cstring>
#include<fstream>
#include<queue>
#include<sstream>
//#include<memory.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define pii pair<int,int>
#define INF 1000000007
#define pll pair<ll,ll>
#define pid pair<int,double>


const int N=25000+5;
const int M=50000+5;

struct Edge
{
    int to,next;
}edge[2*M];
int head[N];

inline void addEdge(int k,int u,int v){
    edge[k].to=v;
    edge[k].next=head[u];
    head[u]=k;
}

int low[N],dfn[N];
int sta[N],top;

int belong[N];//记录点i属于哪个联通块

//vector<vector<int> >ans;//ans[i][j]为第i个联通块的点

void tarjanBfs(int cur,int&sig,int&tcc,int from){
    sta[++top]=cur;
    low[cur]=dfn[cur]=++sig;
    for(int i=head[cur];i!=-1;i=edge[i].next){
        int to = edge[i].to;
        if(to==from){
            continue;
        }
        if(!dfn[to]){//未遍历过
            tarjanBfs(to,sig,tcc,cur);
            low[cur]=min(low[cur],low[to]);

            if(dfn[cur]<=low[to]){//疑似发现联通块
                int cow = 1;
                int tmpTop=top;
                do{
                    ++cow;
                }while(sta[tmpTop--]!=to);
                if(cow>2){//超过2个点
                    belong[cur]=tcc;
                    /*
                    ans.push_back(vector<int>());
                    ans.back().push_back(cur);
                    */
                    do{
                        belong[sta[top]]=tcc;
                        //ans.back().push_back(sta[top]);
                    }while(sta[top--]!=to);
                    ++tcc;
                }
                else{
                    top=tmpTop;
                }
            }

        }
        else{
            low[cur]=min(low[cur],low[to]);
        }

    }
}

int tarjan(int n){
    int tcc=0,sig=0;
    //ans.clear();
    fill(low,low+n+1,0);
    fill(dfn,dfn+n+1,0);
    fill(belong,belong+n+1,-1);
    top = -1;
    for(int i=1;i<=n;++i){
        if(dfn[i]==0){
            tarjanBfs(i,sig,tcc,-1);
        }
    }
    return tcc;//联通块个数
}

int main()
{
    //freopen("/home/lu/Documents/r.txt","r",stdin);
    //freopen("/home/lu/Documents/w.txt","w",stdout);
    int n,m,u,v;
    scanf("%d%d",&n,&m);
    fill(head,head+n+1,-1);
    for(int i =0;i<m;++i){
        scanf("%d%d",&u,&v);
        addEdge(2*i,u,v);
        addEdge(2*i+1,v,u);
    }
    tarjan(n);
    int q;
    scanf("%d",&q);
    while(q--){
        scanf("%d%d",&u,&v);
        puts((belong[u]==belong[v]&&belong[u]!=-1)?"Yes":"No");
    }
    return 0;
}
题目 51nod 3478 涉及一个矩阵问题,要求通过最少的操作次数,使得矩阵中至少有 `RowCount` 行 `ColumnCount` 列是回文的。解决这个问题的关键在于如何高效地枚举所有可能的行列组合,并计算每种组合所需的操作次数。 ### 解法思路 1. **预处理每一行每一列变为回文所需的最少操作次数**: - 对于每一行,计算将其变为回文所需的最少操作次数。这可以通过比较每对对称位置的值是否相同来完成。 - 对于每一列,计算将其变为回文所需的最少操作次数,方法同上。 2. **枚举所有可能的行列组合**: - 由于 `N` `M` 的最大值为 8,因此可以枚举所有可能的行组合列组合。 - 对于每一种组合,计算其所需的最少操作次数,并取最小值。 3. **计算操作次数**: - 对于每一种组合,需要计算哪些行列需要修改,并且注意行列的交叉可能会重复计算,因此需要去重。 ### 代码实现 以下是一个可能的实现方式,使用了枚举位运算来处理组合问题: ```python def min_operations_to_palindrome(matrix, row_count, col_count): import itertools N = len(matrix) M = len(matrix[0]) # Precompute the cost to make each row a palindrome row_cost = [] for i in range(N): cost = 0 for j in range(M // 2): if matrix[i][j] != matrix[i][M - 1 - j]: cost += 1 row_cost.append(cost) # Precompute the cost to make each column a palindrome col_cost = [] for j in range(M): cost = 0 for i in range(N // 2): if matrix[i][j] != matrix[N - 1 - i][j]: cost += 1 col_cost.append(cost) min_total_cost = float('inf') # Enumerate all combinations of rows and columns rows = list(range(N)) cols = list(range(M)) from itertools import combinations for row_comb in combinations(rows, row_count): for col_comb in combinations(cols, col_count): # Calculate the cost for this combination cost = 0 # Add row costs for r in row_comb: cost += row_cost[r] # Add column costs for c in col_comb: cost += col_cost[c] # Subtract the overlapping cells for r in row_comb: for c in col_comb: # Check if this cell is part of the palindrome calculation if r < N // 2 and c < M // 2: if matrix[r][c] != matrix[r][M - 1 - c] and matrix[N - 1 - r][c] != matrix[N - 1 - r][M - 1 - c]: cost -= 1 min_total_cost = min(min_total_cost, cost) return min_total_cost # Example usage matrix = [ [0, 1, 0], [1, 0, 1], [0, 1, 0] ] row_count = 2 col_count = 2 result = min_operations_to_palindrome(matrix, row_count, col_count) print(result) ``` ### 代码说明 - **预处理成本**:首先计算每一行每一列变为回文所需的最少操作次数。 - **枚举组合**:使用 `itertools.combinations` 枚举所有可能的行列组合。 - **计算成本**:对于每一种组合,计算其成本,并考虑行列交叉的重复计算问题。 ### 复杂度分析 - **时间复杂度**:由于 `N` `M` 的最大值为 8,因此枚举所有组合的时间复杂度为 $ O(N^{RowCount} \times M^{ColCount}) $,这在实际中是可接受的。 - **空间复杂度**:主要是存储预处理的成本,空间复杂度为 $ O(N + M) $。 ### 相关问题 1. 如何优化矩阵中行列的枚举组合以减少计算时间? 2. 在计算行列的交叉时,如何更高效地处理重复计算的问题? 3. 如果矩阵的大小增加到更大的范围,如何调整算法以保持效率? 4. 如何处理矩阵中行列的回文件不同时的情况? 5. 如何扩展算法以支持更多的操作类型,例如翻转某个区域的值?
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值