题目链接: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. django 与 mysql 勾结指南

  2. git批量恢复所有删除的文件

    git ls-files -d | xargs -i git checkout {}

  3. Vmware 虚拟机无法启动

    问题背景: 自己的电脑坏了,用的事小伙伴的电脑,安装VMware 软件,然后创建虚拟机(放在移动硬盘上).在操作虚拟主机的时候,中间不小心碰到了移动硬盘, 然后移动硬盘就掉线了.这个时候再去启动虚拟主 ...

  4. Python读取本地文档内容并发送邮件

    当需要将本地某个路径下的文档内容读取后并作为邮件正文发送的时候可以参考该文,使用到的模块包括smtplib,email. #! /usr/bin/env python3 # -*- coding:ut ...

  5. Source Insight小技巧:修改Symbol Window的默认宽度

    SI是个好东西,但是源代码窗口左边的符号窗口的默认宽度实在是太小,每次打开一个新的源码窗口都要重新拖放调整,很烦人.下面是一劳永逸调整Symbol Window宽度的方法. 打开一个源码窗口,将Sym ...

  6. 【交换机】交换机RLDP(环路检测&链路检测)功能介绍及配置说明

    功能简介RLDP 全称是Rapid Link Detection Protocol,是锐捷网络自主开发的一个用于快速检测以太网链路故障的链路协议.一般的以太网链路检测机制都只是利用物理连接的状态,通过 ...

  7. hdfs 安全模式介绍

    1. hdfs在启动的时候现将映像载入内存,并执行edits中的各项操作,一旦在内存中建立元数据的映像,则闯进啊一个新的fsimage文件和空的编辑日志.此时namenode开始监听datanode请 ...

  8. go: writing stat cache:, permission denied

    sudo chown -R $(whoami):admin /Users/zhushuyan/go/pkg && sudo chmod -R g+rwx /Users/zhushuya ...

  9. mac:Go安装和配置+GoLand安装和使用之完整教程

    前言 作为一个go语言程序员,觉得自己有义务为go新手开一条更简单便捷的上手之路.纵使网上教程很多,但总不尽人意.go的入门门槛还是非常低的,无论是安装还是使用. go安装 go 语言支持以下系统:  ...

  10. 揭秘Java热部署原理及JRebel(Hotcode)的实现原理

    基础知识:class卸载.热替换和Tomcat的热部署的分析HotSwap:HotSwap和JRebel原理成熟的热部署技术实现原理:深入探索 Java 热部署 java的热部署和热加载