题解:AT_abc197_f [ABC197F] Construct a Palindrome

· · 题解

ABC197F

前言

update 24/5/28:添加前言部分,修改 bfs 部分代码定义问题,增加 vis 数组的定义及作用。

分析

从头开始分析一遍。

回文串要满足头对头,尾对尾的特性,并且题目要求以 1 开始,n 节点结尾。加上要求最短路,我们就可以使用广搜进行处理。

思路

广搜是从 1 节点和 n 节点开始搜索,如果发现以 a 与 b 为出发节点的两条边 l_1,l_2 颜色相同时,就继续搜索。于是 bfs 程序就是这样的:

const int N = 1e3 + 5;
struct O {
    int u;// 节点编号 
    char r;// 节点颜色 
};
vector<O> g[N];// vector存图
struct node {
    int front;// 从1出发的搜索路线 
    int end;// 从n出发的搜索路线 
    int stp;// 搜索步数 
};
int n, m;// 如题

bool cg[N][N];// 邻接表存图
bool vis[N][N];// rtj

void bfs() {
    queue<node> q;
    q.push({1, n, 0});
    while(!q.empty()) {
        node t = q.front();
        q.pop();
        int s = t.stp + 1;
        for(auto x : g[t.front]) {
            for(auto y : g[t.end]) {
                if(x.r == y.r && !vis[x.u][y.u]) { // 是同一个颜色
                    vis[x.u][y.u] = 1;
                    q.push(node {x.u, y.u, s});
                }
            }
        }
    }
}

其中,node 数据结构的定义比较讲究。懂的人知道是双端广搜,不懂的人像我一样就当是普通的广搜就好了。front 表示从 1 开始搜索的路线 l_1 的末端点(通俗的讲就是搜到哪了),end 表示从 n 开始搜索的路线 l_2 的末端点。stp 表示从两端开始搜的步数。

关于 vis 数组的设定,我们发现当左端搜索路线和右端搜索路线已经重复时,肯定已经处理过了,所以要保证搜索路线最短(因为是 bfs),就不用再重复搜索。

里面的 vis[x][y] 数组表示点 x 和点 y 有没有同时作为结尾搜索过。所以,对于搜索,一共有 n^2 个点。

再来考虑答案。看这个图:

其中,n=8。

很显然,对于左边,当从 1 和 n 开始搜索搜到同一个点时,它肯定是一个答案。对于右边,我们发现当步数为奇时,front 与 end 连通也是一种情况。

所以就有了答案判断:

if(t.front == t.end)    ans = min(ans, t.stp * 2);
if(cg[t.front][t.end])  ans = min(ans, t.stp * 2 + 1);

cg[x][y] 是邻接表存图法。

时间复杂度

对于思路,肯定不难想,也最多只有绿的难度。真正的时间复杂度证明才让题目成了蓝题。

设 \text{deg}(x) 表示 x 的出边, 时间复杂度暂设 ans。

\therefore ans= \sum_{1 \le a \le n} \sum_{1 \le b \le n} \text{deg}(a) \times \text{deg}(b) ans = \text{deg}(1)\times \text{deg}(1) + \text{deg}(1) \times\text{deg}(2)+\dots = \text{deg}(2)\times \text{deg}(1) + \text{deg}(2)\times\text{deg}(2) \dots \dots \therefore ans = \text{deg}(1) \times(\text{deg}(1) + \text{deg}(2) + \dots) + \text{deg}(2) \times(\text{deg}(1) + \text{deg}(2) + \dots) + \dots \therefore ans = \sum_{1 \le a \le n} \text{deg}(a) \times \sum_{1 \le b \le n}\text{deg}(b) = m^2

所以,这题的时间复杂度就是 O(m^2) 了。

Code

#include <bits/stdc++.h>
using namespace std;
const int N = 1e3 + 5;
struct O {
    int u;// 节点
    char r;// 颜色
};
struct node {
    int front;// rtj
    int end;
    int stp;
};

int n, m;
vector<O> g[N];// vector存图
bool cg[N][N];// 邻接表
bool vis[N][N];
int ans = INT_MAX;

void bfs() {
    queue<node> q;
    q.push({1, n, 0});
    while(!q.empty()) {
        node t = q.front();
        q.pop();
        if(t.front == t.end)    ans = min(ans, t.stp * 2);
        if(cg[t.front][t.end])  ans = min(ans, t.stp * 2 + 1);
        int s = t.stp + 1;
        for(auto x : g[t.front]) {
            for(auto y : g[t.end]) {
                if(x.r == y.r && !vis[x.u][y.u]) { // 是同一个颜色
                    vis[x.u][y.u] = 1;
                    q.push(node {x.u, y.u, s});
                }
            }
        }
    }
}

int main() {
    cin>>n>>m;
    for(int i = 1; i <= m; i++) {
        int x, y;
        char r;
        cin>>x>>y>>r;
        g[x].push_back({y,r});
        g[y].push_back({x,r});
        cg[x][y] = cg[y][x] = 1;
    }
    bfs();
    if(ans == INT_MAX)  cout<<-1;
    else cout<<ans<<"\n";
    return 0;
}

关于 c++ 的 auto 语法,可以自行百度一下。