日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

發(fā)布時間:2023/12/4 编程问答 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 2021年度训练联盟热身训练赛第一场 H题On Average They‘re Purple(BFS) 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

題意:

給你一些聯(lián)通關(guān)系,問Bob先選擇一些路徑(1~n)聯(liá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≤1000002N100000 and 1≤M≤1000001≤M≤1000001M100000. 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})1ai?,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最短路徑即可,因為是無權(quán)路徑,且題目是求染色變化,可以用BFS在一個無權(quán)圖上求從起點(diǎn)到其他所有點(diǎn)的最短路徑。最大染色變化即為最短路徑長-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; }

總結(jié)

以上是生活随笔為你收集整理的2021年度训练联盟热身训练赛第一场 H题On Average They‘re Purple(BFS)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。