題目連接:https://codeforces.com/gym/102056/problem/Lc++
LCR is really an incredible being.app
Thinking so, sitting on the plane and looking at the sea, Rikka savours the fantastic journey. A fire never happened. It must be interesting to share it with her mates, and another travel is also fascinating.ui
But before all, home is the best.spa
She travels by the JR lines. There are n stations in total, and m public bidirectional railway lines are built among them. Each station belongs to either JR West or JR East. Both JR West and JR East have their own private railways connecting all stations owned by themselves.rest
Rikka has some through tickets and two types of special passes: ICOCA for JR West and Suica for JR East. She pays a through ticket each time and does one of the following:code
Travel from one terminal to another via a public railway line.
Travel to any station which has the same owner as the current one, using one of her special passes. A pass can be used for multiple times.
Rikka wonders, for each start station, the sum of the minimal numbers of tickets she needs to pay to reach every possible one of the other stations.blog
Input
The first line contains two integers $n,m (1 \le n \le 10^5,0 \le m \le 10^5)$, the numbers of stations and public railways.ip
The next line contains $n$ integers $A_i (A_i \in {0,1},i=1,2,…,n)$, separated by spaces, describing the owner of each station. $A_i=0$ if the station $i$ belongs to JR west, and vice versa.ci
The following $m$ lines describe all the public railways, each of which contains two integers $u,v (1 \le u,v \le n,u \neq v)$, representing a bidirectional railway connecting $u$ and $v$. It is guaranteed that no two public railways connect the same pair of stations, and Rikka is able to travel between every pair of stations. Notice that the private railways are not given directly in the input, and Rikka may have to use her passes rather than traveling only through the public railways.terminal
Output
Output $n$ integers in one line, separated by spaces. The $i$-th integer is $\sum_{j=1}^{n}D(i,j)$ , where $D(u,v)$ is the minimal number of tickets needed to travel from $u$ to $v$.
Examples
input
3 2
1 0 0
1 2
1 3
output
2 2 2
input
5 3
1 0 1 0 1
1 2
2 3
4 5
output
5 5 5 6 5
題意:
有 $n$ 個車站,相互之間有 $m$ 條公共的雙向鐵路連通,車站分紅 $0,1$ 兩種類別,任意兩個 $0$ 類車站之間都有一條私有的鐵路相互鏈接,$1$ 類車站也相似。
如今,從每車站 $i$ 出發,到車站 $j$ 的最少走過的鐵路數目爲 $D(u,v)$,要求你對每一個固定的車站 $i$,都輸出到其餘全部車站的 $D(i,j)$ 之和。
題解:
首先,任意兩個同類車站之間確定只須要走一步便可到達。而異類車站則有三種可能:一種是和當前車站有公共鐵路鏈接可一步到達、一種是走兩步到達、一種是走三步到達(以下圖)。
不妨設我如今出發點是個 $1$ 類車站,統計一下同類車站的數目,這些車站是一步直達的;
其次,我分紅三類統計異類車站:
一、從當前車站出發,經過公共鐵路一步直達;
二、有另一個 $1$ 類車站和其有公共鐵路相連,這種必然是兩步到達;
三、沒有 $1$ 類車站和其之間存在公共鐵路,(這種車站,走三步到達仍是走兩步到達要取決於當前車站,看當前車站有沒有公共鐵路直連異類車站)。
AC代碼:
#include<bits/stdc++.h> using namespace std; const int maxn=1e5+5; int n,m,type[maxn],tag[maxn]; //type[i]:車站類型 //tag[i]:記錄有多少個異類車站和其直接相連 int cnt[2][2]; //cnt[1][0]:有0類車站和其相連的1類車站數目 //cnt[1][1]:無0類車站和其相連的1類車站數目 //cnt[0][0]:無1類車站和其相連的0類車站數目 //cnt[0][1]:有1類車站和其相連的0類車站數目 int main() { cin>>n>>m; for(int i=1;i<=n;i++) scanf("%d",&type[i]); memset(tag,0,sizeof(tag)); for(int i=1,u,v;i<=m;i++) { scanf("%d%d",&u,&v); if(type[u]!=type[v]) tag[u]++, tag[v]++; } memset(cnt,0,sizeof(cnt)); for(int i=1;i<=n;i++) { if(type[i]) { if(tag[i]) cnt[1][0]++; else cnt[1][1]++; } else { if(tag[i]) cnt[0][1]++; else cnt[0][0]++; } } //printf("cnt10=%d cnt11=%d cnt01=%d cnt00=%d\n",cnt[1][0],cnt[1][1],cnt[0][1],cnt[0][0]); for(int i=1,ans;i<=n;i++) { if(i>1) printf(" "); if(type[i]) { ans=(cnt[1][0]+cnt[1][1]-1)+tag[i]; //一步 ans+=(cnt[0][1]-tag[i])*2; //兩步 ans+=cnt[0][0]*(tag[i]?2:3); //兩步or三步 printf("%d",ans); } else { ans=(cnt[0][0]+cnt[0][1]-1)+tag[i]; //一步 ans+=(cnt[1][0]-tag[i])*2; //兩步 ans+=cnt[1][1]*(tag[i]?2:3); //兩步or三步 printf("%d",ans); } } }