商丘网站公司济南公共资源交易中心
商丘网站公司,济南公共资源交易中心,我的网站模板下载 迅雷下载 迅雷下载,网站建设是啥题意#xff1a;
给你一些联通关系#xff0c;问Bob先选择一些路径#xff08;1~n#xff09;联通#xff0c;Alice在路径上染色#xff0c;Bob的目的是选择一些路径使得染色变化最小#xff0c;对于Alice来说#xff0c;需要使得在Bob选择的#xff08;1−n1-n1−n
给你一些联通关系问Bob先选择一些路径1~n联通Alice在路径上染色Bob的目的是选择一些路径使得染色变化最小对于Alice来说需要使得在Bob选择的1−n1-n1−nd的路径上使得颜色变化最大。
题目 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}aiand 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,aibi)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代码
#includecstdio
#includecstring
#includealgorithm
#includevector
#includequeue
const int M1e510;
using namespace std;
bool vis[M];
int n,m,dist[M];
vectorintg[M];
void BFS()
{queueintq;q.push(1);vis[1]1;dist[1]0;while(!q.empty()){int iq.front();q.pop();for(int k0; kg[i].size(); k){int jg[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 i1; im; 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;
}
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/pingmian/90323.shtml
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!