CF1407E.Egor in the Republic of Dagestan

省选/NOI-

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

Egor is a famous Russian singer, rapper, actor and blogger, and finally he decided to give a concert in the sunny Republic of Dagestan.

There are nn cities in the republic, some of them are connected by mm directed roads without any additional conditions. In other words, road system of Dagestan represents an arbitrary directed graph. Egor will arrive to the city 11, travel to the city nn by roads along some path, give a concert and fly away.

As any famous artist, Egor has lots of haters and too annoying fans, so he can travel only by safe roads. There are two types of the roads in Dagestan, black and white: black roads are safe at night only, and white roads — in the morning. Before the trip Egor's manager's going to make a schedule: for each city he'll specify it's color, black or white, and then if during the trip they visit some city, the only time they can leave it is determined by the city's color: night, if it's black, and morning, if it's white. After creating the schedule Egor chooses an available path from 11 to nn, and for security reasons it has to be the shortest possible.

Egor's manager likes Dagestan very much and wants to stay here as long as possible, so he asks you to make such schedule that there would be no path from 11 to nn or the shortest path's length would be greatest possible.

A path is one city or a sequence of roads such that for every road (excluding the first one) the city this road goes from is equal to the city previous road goes into. Egor can move only along paths consisting of safe roads only.

The path length is equal to the number of roads in it. The shortest path in a graph is a path with smallest length.

叶戈尔是一位著名的俄罗斯歌手、说唱歌手、演员和博主,他最终决定在阳光明媚的达吉斯坦共和国举办一场演唱会。

达吉斯坦共和国有 nn 座城市,其中一些城市由 mm 条有向道路连接,且这些道路没有任何额外限制。换言之,达吉斯坦的道路系统构成一个任意的有向图。叶戈尔将抵达第 11 号城市,沿某条路径经由道路前往第 nn 号城市,在那里举办演唱会后离开。

作为任何一位著名艺术家,叶戈尔拥有大量黑粉和过于狂热的粉丝,因此他只能行走在“安全道路”上。达吉斯坦的道路分为两类:黑色道路与白色道路。黑色道路仅在夜间安全,白色道路仅在清晨安全。出发前,叶戈尔的经纪人将制定一份日程安排:为每座城市指定一种颜色(黑色或白色);之后,若旅行途中访问某座城市,则离开该城市的唯一允许时间即由该城市的颜色决定:若为黑色,则只能在夜间离开;若为白色,则只能在清晨离开。日程安排确定后,叶戈尔将选择一条从 11 到 nn 的可行路径;出于安全考虑,该路径必须是可能的最短路径。

叶戈尔的经纪人非常喜爱达吉斯坦,希望尽可能长时间地留在这里,因此他请你设计这样一份日程安排,使得:要么从 11 到 nn 根本不存在可行路径,要么所有可行路径中最短路径的长度达到最大可能值。

一条路径是指单个城市,或是一系列道路组成的序列,使得对于该序列中除第一条外的每条道路,其起点城市恰好等于前一条道路的终点城市。叶戈尔只能沿全部由安全道路构成的路径移动。

路径的长度定义为其中所含道路的数量。图中的最短路径即为长度最小的路径。

输入格式

The first line contains two integers nn, mm (1≤n≤5000001 \leq n \leq 500000, 0≤m≤5000000 \leq m \leq 500000) — the number of cities and the number of roads.

The ii-th of next mm lines contains three integers — uiu_i, viv_i and tit_i (1≤ui,vi≤n1 \leq u_i, v_i \leq n, ti∈0,1t_i \in {0, 1}) — numbers of cities connected by road and its type, respectively (00 — night road, 11 — morning road).

第一行包含两个整数 nn、mm(1≤n≤5000001 \leq n \leq 500000,0≤m≤5000000 \leq m \leq 500000)—— 分别表示城市的数量和道路的数量。

接下来的 mm 行中,第 ii 行包含三个整数 uiu_i、viv_i 和 tit_i(1≤ui,vi≤n1 \leq u_i, v_i \leq n,ti∈{0,1}t_i \in \{0, 1\})—— 分别表示该道路所连接的两座城市的编号及其类型(00 表示夜间道路,11 表示清晨道路)。

输出格式

In the first line output the length of the desired path (or −1-1, if it's possible to choose such schedule that there's no path from 11 to nn).

In the second line output the desired schedule — a string of nn digits, where ii-th digit is 00, if the ii-th city is a night one, and 11 if it's a morning one.

If there are multiple answers, print any.

第一行输出所求路径的长度(若存在一种调度方案使得从城市 11 到城市 nn 不存在路径,则输出 −1-1)。

第二行输出所求的调度方案——一个长度为 nn 的数字字符串,其中第 ii 位数字为 00 表示第 ii 个城市是“夜间城市”,为 11 表示第 ii 个城市是“清晨城市”。

若存在多个合法答案,输出任意一个即可。

输入输出样例

  • 输入#1

    3 4
    1 2 0
    1 3 1
    2 3 0
    2 3 1

    输出#1

    2
    011
  • 输入#2

    4 8
    1 1 0
    1 3 0
    1 3 1
    3 2 0
    2 1 0
    3 4 1
    2 4 0
    2 4 1

    输出#2

    3
    1101
  • 输入#3

    5 10
    1 2 0
    1 3 1
    1 4 0
    2 3 0
    2 3 1
    2 5 0
    3 4 0
    3 4 1
    4 2 1
    4 5 0

    输出#3

    -1
    11111

说明/提示

For the first sample, if we paint city 11 white, the shortest path is 1→31 \rightarrow 3. Otherwise, it's 1→2→31 \rightarrow 2 \rightarrow 3 regardless of other cities' colors.

For the second sample, we should paint city 33 black, and there are both black and white roads going from 22 to 44. Note that there can be a road connecting a city with itself.

对于第一个样例,如果我们把城市 11 涂成白色,则最短路径为 1→31 \rightarrow 3;否则,无论其他城市的颜色如何,最短路径均为 1→2→31 \rightarrow 2 \rightarrow 3。

对于第二个样例,我们应该把城市 33 涂成黑色,且从城市 22 到城市 44 同时存在黑色道路和白色道路。注意:可能存在连接某个城市与其自身的道路。

输入解题思路,AI测评打分。不知道怎么写?

首页