PAT 1013求解——记一次艰难的AC过程

本文探讨了解决城市间高速公路连接性问题的算法优化,通过对比邻接矩阵、邻接表与并查集三种方法,揭示了输入输出方式对性能的影响。

1013 Battle Over Cities (25 分)

It is vitally important to have all the cities connected by highways in a war. If a city is occupied by the enemy, all the highways from/toward that city are closed. We must know immediately if we need to repair any other highways to keep the rest of the cities connected. Given the map of cities which have all the remaining highways marked, you are supposed to tell the number of highways need to be repaired, quickly.

For example, if we have 3 cities and 2 highways connecting city​1​​-city​2​​ and city​1​​-city​3​​. Then if city​1​​ is occupied by the enemy, we must have 1 highway repaired, that is the highway city​2​​-city​3​​.

Input Specification:

Each input file contains one test case. Each case starts with a line containing 3 numbers N (<1000), M and K, which are the total number of cities, the number of remaining highways, and the number of cities to be checked, respectively. Then M lines follow, each describes a highway by 2 integers, which are the numbers of the cities the highway connects. The cities are numbered from 1 to N. Finally there is a line containing K numbers, which represent the cities we concern.

Output Specification:

For each of the K cities, output in a line the number of highways need to be repaired if that city is lost.

Sample Input:

3 2 3
1 2
1 3
1 2 3

Sample Output:

1
0
0

此道题目看起来比较简单,思路也比较简单,本质上只要求出连通图的个数,那么问题就迎刃而解,但是最终解决问题花费了大量的时间 。

Vision 1:

建立一个邻接矩阵,再用DFS算法求解连通图个数

#include <iostream>
#include<string.h>
using namespace std;
 
int G[1000][1000]={0};
int visit[1000]={0};
int GC[1000][1000]={0};
int N,M,K;
 
void DFS(int u ){
    visit[u]=1;
    for(int i=1;i<=N;i++){
        if (GC[u][i]==1&&visit[i]==0)
            DFS(i);
    }
}
int main()
{
    cin>>N>>M>>K;
    for(int i=0;i<M;i++){
        int m,n;
        cin>>m>>n;
        G[m][n]=G[n][m]=1;
    }
    for(int k=0;k<K;k++){
        int l,gnum=0;
        cin>>l;
        memset(visit,0,sizeof(visit));
        for(int i=1;i<=N;i++){
            for(int j=1;j<=N;j++){
                GC[i][j]=G[i][j];
            }
        }
        for(int i=1;i<=N;i++){
            if (GC[l][i]==1) GC[l][i]=GC[i][l]=0;
        }
        for(int i=1;i<=N;i++){
            if (!visit[i]) DFS(i),gnum++;
        }
        cout<<gnum-2<<endl;
    }
}

结果出现了超时问题。

Version 2:

分析数据结构对其改良,邻接表可能无关的遍历次数过多,造成时间浪费,改用连接表:

#include <iostream>
#include<string.h>
#include<vector>
using namespace std;

//DFS计算连通图,最后一个用例超时
vector<int> V[1000];
int visit[1000]={0};
int N,M,K;

void DFS(int u ){
    visit[u]=1;
    for(int i=0;i<V[u].size();i++){
        int c=V[u][i];
        if (visit[c]==0)
            DFS(c);
    }
}
int main()
{
    cin>>N>>M>>K;
    for(int i=0;i<M;i++){
        int m,n;
        cin>>m>>n;
        V[m].push_back(n);
        V[n].push_back(m);
    }
    for(int k=0;k<K;k++){
        int l,gnum=0;
        cin>>l;
        memset(visit,0,sizeof(int)*(N+1));
        visit[l]=1;
        for(int i=1;i<=N;i++){
            if (!visit[i]) DFS(i),gnum++;
        }
        if(gnum==0) cout<<0<<endl;
        else cout<<gnum-1<<endl;
    }
}

结果还是超时

Version 3:

改用并查集来解决问题:

#include <iostream>
#include<vector>
#include<stdio.h>
using namespace std;
struct Edge{
int u,v;
};
vector<Edge> E;
int tribe[1000];

int find_tribe(int r){
    int p=r;
    while(tribe[p]!=p)
        p=tribe[p];
    while(tribe[r]!=p){
        r=tribe[r];
        tribe[r]=p;
    }
    return p;
}

void merge_tribe(int u,int v){
    int fx=find_tribe(u);
    int fy=find_tribe(v);
    tribe[fx]=fy;
}
int main()
{
    int N,M,K;
    cin>>N>>M>>K;
    for(int i=0;i<M;i++){
        int m,n;
        cin>>m>>n;
        Edge e;
        e.u=m,e.v=n;
        E.push_back(e);
    }
    for(int k=0;k<K;k++){
        for(int i=1;i<=N;i++)
            tribe[i]=i;
        int lost,gnum=0;
        cin>>lost;
        for(int i=0;i<M;i++){
            if(E[i].u!=lost&&E[i].v!=lost)
                merge_tribe(E[i].u,E[i].v);
        }
        for(int i=1;i<=N;i++)
            if (tribe[i]==i)
                gnum++;
        if (gnum>2)
            cout<<gnum-2;
        else
            cout<<0;
    }
}

 

为什么还是超时?已经想不出来改进的方法了。

后来发现上面的3个版本只要输入输出都换用scanf,prinf就能通过。

scanf输入和cin输入的时间差别: https://blog.youkuaiyun.com/soul_97/article/details/79416985

通过代码:

#include <iostream>
#include<vector>
#include<stdio.h>
using namespace std;
struct Edge{
int u,v;
};
vector<Edge> E;
int tribe[1000];

int find_tribe(int r){
    int p=r;
    while(tribe[p]!=p)
        p=tribe[p];
    while(tribe[r]!=p){
        r=tribe[r];
        tribe[r]=p;
    }
    return p;
}

void merge_tribe(int u,int v){
    int fx=find_tribe(u);
    int fy=find_tribe(v);
    tribe[fx]=fy;
}
int main()
{
    int N,M,K;
    scanf("%d%d%d",&N,&M,&K);
    for(int i=0;i<M;i++){
        int m,n;
        scanf("%d%d",&m,&n);
        Edge e;
        e.u=m,e.v=n;
        E.push_back(e);
    }
    for(int k=0;k<K;k++){
        for(int i=1;i<=N;i++)
            tribe[i]=i;
        int lost,gnum=0;
        scanf("%d",&lost);
        for(int i=0;i<M;i++){
            if(E[i].u!=lost&&E[i].v!=lost)
                merge_tribe(E[i].u,E[i].v);
        }
        for(int i=1;i<=N;i++)
            if (tribe[i]==i)
                gnum++;
        if (gnum>2)
            printf("%d\n",gnum-2);
        else
            printf("0\n");
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值