题目链接:https://codeforces.com/gym/102056/problem/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.

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.

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.

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.

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、从当前车站出发,通过公共铁路一步直达;

  2、有另外一个 $1$ 类车站和其有公共铁路相连,这种必然是两步到达;

  3、没有 $1$ 类车站和其之间存在公共铁路,(这种车站,走三步到达还是走两步到达要取决于当前车站,看当前车站有没有公共铁路直连异类车站)。

AC代码:

#include<bits/stdc++.h>
using namespace std;
const int maxn=1e5+;
int n,m,type[maxn],tag[maxn];
//type[i]:车站类型
//tag[i]:记录有多少个异类车站和其直接相连
int cnt[][];
//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=;i<=n;i++) scanf("%d",&type[i]); memset(tag,,sizeof(tag));
for(int i=,u,v;i<=m;i++)
{
scanf("%d%d",&u,&v);
if(type[u]!=type[v]) tag[u]++, tag[v]++;
} memset(cnt,,sizeof(cnt));
for(int i=;i<=n;i++)
{
if(type[i])
{
if(tag[i]) cnt[][]++;
else cnt[][]++;
}
else
{
if(tag[i]) cnt[][]++;
else cnt[][]++;
}
}
//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=,ans;i<=n;i++)
{
if(i>) printf(" ");
if(type[i])
{
ans=(cnt[][]+cnt[][]-)+tag[i]; //一步
ans+=(cnt[][]-tag[i])*; //两步
ans+=cnt[][]*(tag[i]?:); //两步or三步
printf("%d",ans);
}
else
{
ans=(cnt[][]+cnt[][]-)+tag[i]; //一步
ans+=(cnt[][]-tag[i])*; //两步
ans+=cnt[][]*(tag[i]?:); //两步or三步
printf("%d",ans);
}
}
}

Gym 102056L - Eventual … Journey - [分类讨论][The 2018 ICPC Asia-East Continent Final Problem L]的更多相关文章

  1. 2018-2019 ACM-ICPC, Asia East Continent Final L Eventual … Journey

    #include<iostream> using namespace std; ; int cnt[MAX]; int ans[MAX]; int a[MAX]; int main() { ...

  2. 2018 ICPC Asia Singapore Regional A. Largest Triangle (计算几何)

    题目链接:Kattis - largesttriangle Description Given \(N\) points on a \(2\)-dimensional space, determine ...

  3. CodeForces 788B - Weird journey [ 分类讨论 ] [ 欧拉通路 ]

    题意: 给出无向图. good way : 仅有两条边只经过一次,余下边全经过两次的路 问你共有多少条不同的good way. 两条good way不同仅当它们所经过的边的集合中至少有一条不同 (很关 ...

  4. Gym 102056I - Misunderstood … Missing - [DP][The 2018 ICPC Asia-East Continent Final Problem I]

    题目链接:https://codeforces.com/gym/102056/problem/I Warm sunshine, cool wind and a fine day, while the ...

  5. Gym - 101981K The 2018 ICPC Asia Nanjing Regional Contest K.Kangaroo Puzzle 暴力或随机

    题面 题意:给你1个20*20的格子图,有的是障碍有的是怪,你可以每次指定上下左右的方向,然后所有怪都会向那个方向走, 如果2个怪撞上了,就融合在一起,让你给不超过5w步,让所有怪都融合 题解:我们可 ...

  6. Gym - 101981M The 2018 ICPC Asia Nanjing Regional Contest M.Mediocre String Problem Manacher+扩增KMP

    题面 题意:给你2个串(长度1e6),在第一个串里找“s1s2s3”,第二个串里找“s4”,拼接后,是一个回文串,求方案数 题解:知道s1和s4回文,s2和s3回文,所以我们枚举s1的右端点,s1的长 ...

  7. Gym - 101981G The 2018 ICPC Asia Nanjing Regional Contest G.Pyramid 找规律

    题面 题意:数一个n阶三角形中,有多少个全等三角形,n<=1e9 题解:拿到题想找规律,手画开始一直数漏....,最后还是打了个表 (打表就是随便定个点为(0,0),然后(2,0),(4,0), ...

  8. Gym - 101981I The 2018 ICPC Asia Nanjing Regional Contest I.Magic Potion 最大流

    题面 题意:n个英雄,m个怪兽,第i个英雄可以打第i个集合里的一个怪兽,一个怪兽可以在多个集合里,有k瓶药水,每个英雄最多喝一次,可以多打一只怪兽,求最多打多少只 n,m,k<=500 题解:显 ...

  9. Gym - 101981D The 2018 ICPC Asia Nanjing Regional Contest D.Country Meow 最小球覆盖

    题面 题意:给你100个三维空间里的点,让你求一个点,使得他到所有点距离最大的值最小,也就是让你找一个最小的球覆盖掉这n个点 题解:红书模板题,这题也因为数据小,精度也不高,所以也可以用随机算法,模拟 ...

随机推荐

  1. 自建k8s集群日志采集到阿里云日志服务

    自建k8s集群 的master 节点安装 logtail 采集工具 wget http://logtail-release-cn-hangzhou.oss-cn-hangzhou.aliyuncs.c ...

  2. mysql5.6 sql_mode设置为宽松模式

    最近遇到一个很奇怪的事情 由于数据人员的需求,现在需要修改mysql的sql_mode sql_mode默认是sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_ ...

  3. 全面理解Java内存模型(JMM)及volatile关键字(转载)

    关联文章: 深入理解Java类型信息(Class对象)与反射机制 深入理解Java枚举类型(enum) 深入理解Java注解类型(@Annotation) 深入理解Java类加载器(ClassLoad ...

  4. 安装APK失败,错误代码:INSTALL_FAILED_INVALID_APK 解决方案

    错误代码:installation failed with message failed to finalize session:INSTALL_FAILED_INVALID_APK 解决方法如下:

  5. jquery.cookie.js写入的值没有定义

    这个是插件的基本语法,你写的没错,错就错在你肯定是在本地测试的,cookie是基于域名来储存的.意思您要放到测试服务器上或者本地localhost服务器上才会生效.cookie具有不同域名下储存不可共 ...

  6. Github上 10 个开源免费且优秀的后台控制面板(转)

    https://github.com/ant-design/ant-design-pro https://mp.weixin.qq.com/s/Hn6hI-ubGw6N16nFzPdVLA

  7. 记录一下idea自动生成Entity

    最近在鼓捣spring -boot ,真好用,学习到jpa. 通过生成Entity 文件,能够快速的生成数据库,并且使用 JpaRepository 的基本增删查改 方法,好用的一批. 可是随之,问题 ...

  8. 【iCore4 双核心板_ARM】例程二十五:LWIP_DNS实验——域名解析

    实验现象: 核心代码: int main(void) { system_clock.initialize(); led.initialize(); adc.initialize(); delay.in ...

  9. iOS 更改状态栏颜色和隐藏状态栏

    更改状态栏颜色 iOS7以后 状态栏的字体为黑色:UIStatusBarStyleDefault 状态栏的字体为白色:UIStatusBarStyleLightContent 解决方案 1.在info ...

  10. 【规范】前端编码规范——html 规范

    文档类型 推荐使用 html5 的文档类型申明: <!DOCTYPE html> 语言属性 根据 html5 规范: 强烈建议为 html 根元素指定 lang 属性,从而为文档设置正确的 ...