题目链接: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. 每天一个linux命令:du

    1.命令简介 du (Disk usage) 用来计算每个文件的磁盘用量,目录则取总用量. 2.用法 用法:du [选项]... [文件]... 或:du [选项]... --files0-from= ...

  2. 【PMP】项目管理ITTO概述

    1.项目整合管理

  3. R语言之Random Forest随机森林

    什么是随机森林? 随机森林就是通过集成学习的思想将多棵树集成的一种算法,它的基本单元是决策树,而它的本质属于机器学习的一大分支——集成学习(Ensemble Learning)方法.随机森林的名称中有 ...

  4. HighLight.js 使用Demo

    复制下面代码,保存为html,浏览器打开预览即可. <!DOCTYPE html> <html> <head> <meta charset="utf ...

  5. [elk]kafka集群

    kafka高可用 并发写 每一个分区都是一个顺序的.不可变的消息队列, 并且可以持续的添加.分区中的消息都被分了一个序列号,称之为偏移量(offset),在每个分区中此偏移量都是唯一的. 并发读 数据 ...

  6. grokking deep learning

    https://www.manning.com/books/grokking-deep-learning?a_aid=grokkingdl&a_bid=32715258

  7. Docker容器启动lnmp环境下的mysql服务时报"MySQL server PID file could not be found"错误解决办法

    我在自己的mac笔记本上装了一个docker,并在docker容器中安装了lnmp环境,经常会遇到在使用"lnmp restart"命令启动lnmp服务的时候,mysql服务启动失 ...

  8. Open Cygwin at a specific folder

    转自:https://stackoverflow.com/questions/9637601/open-cygwin-at-a-specific-folder# When you install Cy ...

  9. 【nodejs】初识 NodeJS(四)

    上节我们把服务器.路由和请求处理程序结合在一起了,下面就编写一个具体的 web 应用. 上传图片的 web 应用 服务器模块(server.js) var http = require('http') ...

  10. NPS - 数字化营销 - 净推荐值

    在获客成本高涨的时代,拥有一批超级用户,让企业更有本钱专注在提升产品及体验,创造更多的超级用户,形成良性循环.超级用户究竟要如何创造?超级用户可以定义成“忠诚用户当中最忠诚的一群人”,因此创造超级用户 ...