2018EC-Final-Eventual … Journey

L题

题目描述
LCR is really an incredible being.
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.
But before all, home is the best.
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.
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:

· 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.
输入描述:
The first line contains two integers n, m~(1 \leq n \leq 10^5, 0 \leq m \leq 10^5)n,m (1≤n≤10
5
,0≤m≤10
5
), the numbers of stations and public railways.

The next line contains n integers A_i~(A_i \in {0,1}, i = 1, 2, \dots, n)A
i

(A
i

∈{0,1},i=1,2,…,n), separated by spaces, describing the owner of each station. A_i = 0A
i

=0 if the station i belongs to JR west, and vice versa.

The following m lines describe all the public railways, each of which contains two integers u, v~(1 \leq u, v \leq n, u \neq v)u,v (1≤u,v≤n,u


=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.
输出描述:
Output n integers in one line, separated by spaces. The i-th integer is \sum_{j=1}^n D(i, j)∑
j=1
n

D(i,j) , where D(u, v) is the minimal number of tickets needed to travel from u to v.

题意:n个车站,车站分0,1两种属性,同种属性可以直达,不同的属性不能直达。问你在每个站台遍历其他所有站台的总花费。

我们分析了可以知道,其实我们的花费只有三种情况,就是1,2,3。对于所有相同属性的车站我们直接n-1的代价就可以遍历完。其他的看看代码就懂了。

#include<bits/stdc++.h>
using namespace std;
const int MAXN = 1e5+50;
int tag[MAXN],in[MAXN];
int main(){
    int n,m; scanf("%d%d",&n,&m);
    int o=0,z=0;
    for(int i=1;i<=n;i++){
        scanf("%d",&tag[i]);
        if(tag[i]==0) z++; else o++;
    }
    for(int i=0;i<m;i++){
        int u,v; scanf("%d%d",&u,&v);
        if(tag[u]!=tag[v]) in[u]++,in[v]++;
    }
    int oo=0,zz=0;
    for(int i=1;i<=n;i++){
        if(in[i]==0){
            if(tag[i]==0) zz++;
            else oo++;
        }
    }
    for(int i=1;i<=n;i++){
        if(tag[i]==0){
            if(in[i]>0){
                int ans=z-1;
                ans += in[i];
                ans += (o-in[i])*2;
                printf("%d ",ans);
            }else{
                int ans=z-1;
                ans += (o-oo)*2+oo*3;
                printf("%d ",ans);
            }
        }else{
            if(in[i]>0){
                int ans=o-1;
                ans += in[i];
                ans += (z-in[i])*2;
                printf("%d ",ans);
            }else{
                int ans=o-1;
                ans += (z-zz)*2+zz*3;
                printf("%d ",ans);
            }
        }
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值