UVa live6525Attacking rooks(二分最大匹配之最大匹配)

本文介绍了如何通过二分匹配算法高效解决N-rooks问题,并详细解释了建图方法和代码实现。从文章中学习到的优化技巧包括避免不必要的初始化操作和采用前向星优化来提高运行速度。

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

题目地址:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=4536

Chess inspired problems are a common source of exercises in algorithms classes. Starting with the well
known 8-queens problem, several generalizations and variations were made. One of them is the N -rooks
problem, which consists of placing N rooks in an N by N chessboard in such a way that they do not
attack each other.
Professor Anand presented the N -rooks problem to his students. Since rooks only attack each other
when they share a row or column, they soon discovered that the problem can be easily solved by placing
the rooks along a main diagonal of the board. So, the professor decided to complicate the problem by
adding some pawns to the board. In a board with pawns, two rooks attack each other if and only if
they share a row or column and there is no pawn placed between them. Besides, pawns occupy some
squares, which gives an additional restriction on which squares the rooks may be placed on.
Given the size of the board and the location of the pawns, tell Professor Anand the maximum
number of rooks that can be placed on empty squares such that no two of them attack each other.
Input
The input le contains several test cases, each of them as described below.
The rst line contains an integer N (1 N 100) representing the number of rows and columns
of the board. Each of the next N lines contains a string of N characters. In the i-th of these strings,
the j -th character represents the square in the i-th row and j-th column of the board. The character
is either `.' (dot) or the uppercase letter `X', indicating respectively an empty square or a square
containing a pawn.
Output
For each test case, output a line with an integer representing the maximum number of rooks that can
be placed on the empty squares of the board without attacking each other.
Sample Input
5
X....
X....
..X..
.X...
....X
4
....
.X..
....
....
1
X
Sample Output
7
5
0

这个题也是以前做过的,当时用的暴搜。。不用说。。结果自然是该超的都超了。。超内存超时限。现在用二分匹配做真心简单啊。。从一篇文章中有这题的建图方法。。看了好长时间才发现这个建图方法跟我刚做过的一道题的建图方法本质上是一样的。(详情见我上一篇博客。。)由于上一篇博客中有建图方法也有原因,这里就不多说了。。。

这题20s的时限,第一次因为忘记初始化而TLE了好几次。。。sad。。(为什么每个题都要如此手残一番。。)后来加上了之后以19.369s的时间AC了。。。后来发现定的数组过大,光在初始化上就浪费很长时间,改成5000*5000后,时间立即来到了11秒。。。据说用前向星更快,下次再来个前向星版本的,这博文有待更新。上代码:

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <ctype.h>
#include <queue>
#include <map>
#include<algorithm>
using namespace std;
int n, mp1[101][101], mp2[101][101], mpp[101][101], mp[4001][4001], vis[4001], link[4001], k1, k2;
int dfs(int a)
{
    int i;
    for(i=1;i<=k2;i++)
    {
        if(!vis[i]&&mp[a][i])
        {
            vis[i]=1;
            if(link[i]==-1||dfs(link[i]))
            {
                link[i]=a;
                return 1;
            }
        }
    }
    return 0;
}
int hungary()
{
    int i, ans=0;
    memset(link,-1,sizeof(link));
    for(i=1;i<=k1;i++)
    {
        memset(vis,0,sizeof(vis));
        if(dfs(i))
            ans++;
    }
    return ans;
}
int main()
{
    int i, j, a;
    char s[200];
    while(scanf("%d",&n)!=EOF)
    {
        a=0;
        memset(mp,0,sizeof(mp));
        memset(mp1,0,sizeof(mp1));
        memset(mp2,0,sizeof(mp2));
        memset(mpp,0,sizeof(mpp));
        for(i=0;i<n;i++)
        {
            scanf("%s",s);
            for(j=0;j<n;j++)
            {
                if(s[j]=='.')
                    {
                        mpp[i][j]=1;
                        a++;
                    }
            }
        }
        k1=k2=0;
        for(i=0;i<n;i++)
        {
            for(j=0;j<n;j++)
            {
                if(mpp[i][j])
                {
                    if(j==0||(mpp[i][j-1]==0&&mpp[i][j]))
                    {
                        mp1[i][j]=++k1;
                    }
                    if(j!=0&&mpp[i][j-1]&&mpp[i][j])
                        mp1[i][j]=k1;
                }
            }
        }
        for(i=0;i<n;i++)
        {
            for(j=0;j<n;j++)
            {
                if(mpp[j][i])
                {
                    if(j==0||(mpp[j-1][i]==0&&mpp[j][i]))
                    {
                        mp2[j][i]=++k2;
                    }
                    if(j!=0&&mpp[j-1][i]&&mpp[j][i])
                        mp2[j][i]=k2;
                }
            }
        }
        for(i=0;i<n;i++)
        {
            for(j=0;j<n;j++)
            {
                mp[mp1[i][j]][mp2[i][j]]=1;
            }
        }
        printf("%d\n",hungary());
    }
    return 0;
}

更快的前向星写法新鲜出炉啦!!快了100倍!!从11秒马上变成了0.119秒。。。。太爽了。。。

前后对比:



简直炫酷。。。上代码:

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <ctype.h>
#include <queue>
#include <map>
#include <algorithm>
using namespace std;
int n, mp1[101][101], mp2[101][101], mpp[101][101], cnt,  vis[4001], link[4001], k1, k2, head[10000];
struct node
{
    int u, v;
    int next;
} edge[10001];
void add(int u, int v)
{
    edge[cnt].v=v;
    edge[cnt].next=head[u];
    head[u]=cnt++;
}
int dfs(int u)
{
    int i;
    for(i=head[u]; i!=-1; i=edge[i].next)
    {
        if(!vis[edge[i].v])
        {
            vis[edge[i].v]=1;
            if(link[edge[i].v]==-1||dfs(link[edge[i].v]))
            {
                link[edge[i].v]=u;
                return 1;
            }
        }
    }
    return 0;
}
int hungary()
{
    int i, ans=0;
    memset(link,-1,sizeof(link));
    for(i=1; i<=k1; i++)
    {
        memset(vis,0,sizeof(vis));
        if(dfs(i))
            ans++;
    }
    return ans;
}
int main()
{
    int i, j;
    char s[200];
    while(scanf("%d",&n)!=EOF)
    {
        cnt=0;
        memset(head,-1,sizeof(head));
        memset(mp1,0,sizeof(mp1));
        memset(mp2,0,sizeof(mp2));
        memset(mpp,0,sizeof(mpp));
        for(i=0; i<n; i++)
        {
            scanf("%s",s);
            for(j=0; j<n; j++)
            {
                if(s[j]=='.')
                {
                    mpp[i][j]=1;
                }
            }
        }
        k1=k2=0;
        for(i=0; i<n; i++)
        {
            for(j=0; j<n; j++)
            {
                if(mpp[i][j])
                {
                    if(j==0||(mpp[i][j-1]==0&&mpp[i][j]))
                    {
                        mp1[i][j]=++k1;
                    }
                    if(j!=0&&mpp[i][j-1]&&mpp[i][j])
                        mp1[i][j]=k1;
                }
            }
        }
        for(i=0; i<n; i++)
        {
            for(j=0; j<n; j++)
            {
                if(mpp[j][i])
                {
                    if(j==0||(mpp[j-1][i]==0&&mpp[j][i]))
                    {
                        mp2[j][i]=++k2;
                    }
                    if(j!=0&&mpp[j-1][i]&&mpp[j][i])
                        mp2[j][i]=k2;
                }
            }
        }
        for(i=0; i<n; i++)
        {
            for(j=0; j<n; j++)
            {
                if(mpp[i][j])
                    add(mp1[i][j],mp2[i][j]);
            }
        }
        printf("%d\n",hungary());
    }
    return 0;
}



### VRRP 攻击数据包的处理方案 为了有效应对VRRP协议可能遭遇的安全威胁,特别是针对攻击数据包的情况,采取一系列措施来增强系统的安全性至关重要。 #### 1. 数据来源验证 确保仅接受来自合法设备的数据包。这可以通过设置严格的ACL(访问控制列表),只允许特定网段内的主机向VRRP组发送消息[^4]。此外,在支持的情况下启用双向认证机制,进一步确认通信双方的身份合法性。 #### 2. 数据加密传输 对于敏感环境下的应用,考虑使用IPsec或其他安全隧道技术对整个VRRP流量进行封装保护,使得即使在网络层面上截获了这些报文也无法轻易解析其中的内容。 #### 3. 数据完整性校验 利用哈希算法计算并附加于每条发出的消息之后,接收端再次执行相同运算并与附带值对比,以此判断信息是否遭到中途篡改。任何发现异常的情形都将立即丢弃相应报文,并记录日志以便后续分析调查。 #### 4. 抗重放攻击策略 实施序列号检查逻辑,即每次成功接收到一个新的通告后更新本地存储的最大已知序号;一旦遇到小于等于此数值的新到来者则视为非法尝试而予以屏蔽。同时配合较短的有效期设定,超出时限范围外的一切重复提交均不予理会。 ```bash # 示例:配置抗重放功能 (具体命令取决于实际使用的操作系统/平台) set protocols vrrp group <group-name> authentication-type simple-password "secret" set protocols vrrp group <group-name> accept-data-mode strict ``` 以上方法综合运用可显著提升VRRP部署场景中的网络安全防护水平,减少因外部干扰因素造成的潜在风险隐患。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值