题意:

给你一些联通关系,问Bob先选择一些路径(1~n)联通,Alice在路径上染色,Bob的目的是选择一些路径使得染色变化最小,对于Alice来说,需要使得在Bob选择的(1−n1-n1−n)d的路径上使得颜色变化最大。

题目:

Alice and Bob are playing a game on a simple connected graph with N nodes and M edges.
Alice colors each edge in the graph red or blue.
A path is a sequence of edges where each pair of consecutive edges have a node in common. If the first edge in the pair is of a different color than the second edge, then that is a ‘‘color change.’’
After Alice colors the graph, Bob chooses a path that begins at node 1 and ends at node N. He can choose any path on the graph, but he wants to minimize the number of color changes in the path. Alice wants to choose an edge coloring to maximize the number of color changes Bob must make. What is the maximum number of color changes she can force Bob to make, regardless of which path he chooses? changes she can force Bob to make, regardless of which path he chooses?

输入描述:

The first line contains two integer values N and M with 2≤N≤1000002≤N≤1000002≤N≤100000 and 1≤M≤1000001≤M≤1000001≤M≤100000. The next M lines contain two integers aia_{i}ai​and bib_{i}bi​ indicating an undirected edge between nodes aia_{i}ai​ and bib_{i}bi​ (1≤ai,bi≤N,ai≠bi)1≤a_{i},b_{i} ≤N, a_{i}\neq b_{i})1≤ai​,bi​≤N,ai​​=bi​)All edges in the graph are unique.

输出描述:

Output the maximum number of color changes Alice can force Bob to make on his route from node 1 to node N.

示例1

输入

3 3
1 3
1 2
2 3

输出

0

示例2

输入

7 8
1 2
1 3
2 4
3 4
4 5
4 6
5 7
6 7

输出

3

分析:

为了使得染色变化最小,那么就选择1~n最短路径即可,因为是无权路径,且题目是求染色变化,可以用BFS在一个无权图上求从起点到其他所有点的最短路径。最大染色变化即为最短路径长-1;

AC代码:

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#include<queue>
const int M=1e5+10;
using namespace std;
bool vis[M];
int n,m,dist[M];
vector<int>g[M];
void BFS()
{queue<int>q;q.push(1);vis[1]=1;dist[1]=0;while(!q.empty()){int i=q.front();q.pop();for(int k=0; k<g[i].size(); k++){int j=g[i][k];if(vis[j])continue;vis[j]=1;dist[j]=dist[i]+1;q.push(j);}}
}
int main()
{memset(vis,0,sizeof(vis));scanf("%d%d",&n,&m);for(int i=1; i<=m; i++){int x,y;scanf("%d%d",&x,&y);g[x].push_back(y);g[y].push_back(x);}BFS();printf("%d",dist[n]-1);return 0;
}

2021年度训练联盟热身训练赛第一场 H题On Average They‘re Purple(BFS)相关推荐

  1. 2021年度训练联盟热身训练赛第三场赛后补题

    2021年度训练联盟热身训练赛第三场赛后补题 A Circuit Math [题目分析] [代码展示] B Diagonal Cut [题目分析] [代码展示] C Gerrymandering [题 ...

  2. 2021年度训练联盟热身训练赛第四场 H - Rock Paper Scissors(字符串匹配,FFT)

    整理的算法模板合集: ACM模板 点我看算法全家桶系列!!! 实际上是一个全新的精炼模板整合计划 2021年度训练联盟热身训练赛第四场 H - Rock Paper Scissors(字符串匹配,FF ...

  3. 2021年度训练联盟热身训练赛第五场

    2021年度训练联盟热身训练赛第五场 链接:https://ac.nowcoder.com/acm/contest/13926 A Binary Seating #include<bits/st ...

  4. 2021年度训练联盟热身训练赛第八场

    目录 2021年度训练联盟热身训练赛第八场 A-Fire on Field 题意 思路 代码 B-Gene Tree 题意 思路 代码 I-Thread Knots 题意 思路 代码 J-Triang ...

  5. 2021年度训练联盟热身训练赛第三场(待补)

    文章目录 前言 一.Circuit Math(后缀表达式---栈&&fgets) 二.Diagonal Cut(gcd最大公因数,数论) 三.expected primary-expr ...

  6. Dream_Chaser队训练赛第一场 K题

    Dream_Chaser队训练赛第一场 K题 题目来自2012成都区域赛 K - Yet Another Multiple Problem Time Limit:20000MS     Memory ...

  7. Dream_Chaser队训练赛第一场 I题

    Dream_Chaser队训练赛第一场 I题 题目来自2012成都区域赛 I - Count Time Limit:1000MS     Memory Limit:32768KB     64bit ...

  8. 突击蓝桥杯嵌入式(十二)——第十二届省赛第一场真题 停车场

    突击蓝桥杯嵌入式(十二)--第十二届省赛第一场真题 停车场 一.题干 二.题目解析 所需: PA7 PWM LED(锁存器) 串口9600 LCD 按键4个,整体难在逻辑,我们先配置好硬件,进入工程 ...

  9. 突击蓝桥杯嵌入式(七)——第十三届省赛第一场真题

    突击蓝桥杯嵌入式(七)--第十三届省赛第一场真题 一.题目概览 二.思路梳理 我们直接在LCD例程的基础上,改需增加的外设如下: LED灯(配置锁存器PD2),串口(波特率9600,带中断),按键4个 ...

最新文章

  1. PowerDesigner打开设计文件后提示failed to read the fileXXX的解决办法
  2. python系统-python 系统相关操作
  3. 分布式系统理论基础,以及选举,多数派,租约
  4. Block相关内容梳理
  5. php_中替换换行符
  6. 计算机视觉之OpenCV教程 --- Mat图像类基础(二)
  7. 程序员的数学笔记3--迭代法
  8. DevOps看起来很美,实现起来却很难?
  9. 精通Python网络爬虫:核心技术、框架与项目实战.1.4 网络爬虫的类型
  10. 9个月一迭代,比特大陆量产AI芯片想落地于这三个场景
  11. nginx重定向规则入门
  12. Android-7.0-Nuplayer流程图
  13. linux单用户可以删除文件么,Linux单用户模式详解 及应用场景
  14. 英伟达控制面板打不开的解决办法
  15. a as as big rat_12句英语绕口令,你能一口气读完几句?
  16. 删除下拉框只找23火星软件_下拉词删除都选24火星软件
  17. Intel 处理器发展年历
  18. ChatGPT 体验和思考
  19. 基于VB.Net的图书馆管理系统的设计与实现(用户登录界面)
  20. ZigBee协调器向子节点发消息

热门文章

  1. Kotlin之?和!!最简单的理解
  2. Tree的前序序列化
  3. RC4加密解密java算法
  4. Hibernate之悲观锁与乐观锁
  5. linux如何获取网卡计数信息,Linux下如何获取网卡信息
  6. php 字符串进行计算_怎么在php中利用eval对字符串格式进行计算
  7. java 矩阵题目_java练习本(原每日一练)(20190517)
  8. 豆瓣评分9.4!这一部纪录片,探秘中国的未至之境!
  9. 每日一笑 | 你知道你爸妈当年是怎么在一起的吗?
  10. mysql数据库属性_mysql - 数据库操作和数据属性