题目链接: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. tmunx error:invalid option: status-utf8 invalid option: utf8

    修改为:set-window-option -gq mouse off set-window-option -gq mode-mouse off set-option -gq status-utf8 ...

  2. corda

    账本:corda 从每个节点的视角看待账本都是不一样的.并不是所有节点有所有账本信息的.valut视为SQL数据库,只保存 两两节点之间的数据库 状态:状态是不变的对象,代表共享的事实,比如特定时间的 ...

  3. Unity应用架构设计(10)——绕不开的协程和多线程(Part 1)

    在进入本章主题之前,我们必须要了解客户端应用程序都是单线程模型,即只有一个主线程(Main Thread),或者叫做UI线程,即所有的UI控件的创建和操作都是在主线程上完成的.而服务器端应用程序,也就 ...

  4. Mathematica新特性Inactive, 求解复杂微分方程

    Inactive阻止函数的计算, 求解微分方程有奇效 Block[{Integrate = Inactive[Integrate]}, DSolve[((H - h0)^(7/5) P0 (T - c ...

  5. npm安装教程

    一.使用之前,我们先来掌握3个东西是用来干什么的. npm: Nodejs下的包管理器. webpack: 它主要的用途是通过CommonJS的语法把所有浏览器端需要发布的静态资源做相应的准备,比如资 ...

  6. Fortran一个获取硬件系统信息的函数库

    此函数库提供了 Visual Fortran 上获取硬盘ID,CPU编号,windows安装时间及文件头部校验的四个函数.可用于简单的程序加密.(但切勿用于商业性很强的地方)              ...

  7. centos6 利用外部的smpt服务器计划任务发送邮件

    centos可通过修改配置文件以使用外部SMTP服务器,达到不使用sendmail而用外部的smtp服务器发送邮件的目的, 操作如下: 一.安装mailx与sendmail # yum -y inst ...

  8. [20170713] 无法访问SQL Server

    背景: 朋友的环境第二天突然访问不了SQL Server,远程SQL Server用户无法登陆,但是本地SQL Server用户登录正常. 报错: 用户XX登录失败(MicroSoft SQL Ser ...

  9. Openfire 单人聊天和多人聊天(发送消息、接收消息)

    Openfire 单人聊天和多人聊天(发送消息.接收消息) 一.单人聊天 1)发送消息: 首先要获取一个聊天窗口,getConnection()为获取连接connection的方法,调用getFrie ...

  10. python 中有趣的库tqdm

    Tqdm 是 Python 进度条库,可以在 Python 长循环中添加一个进度提示信息用法:tqdm(iterator) # 方法1: import time from tqdm import tq ...