[BOJ 7259] Monthly railway pass

View as PDF

Submit solution

Points: 4
Time limit: 2.0s
Memory limit: 1G

Problem types
Allowed languages
Assembly, Awk, C, C++, Java, Pascal, Perl, Python, Sed, Text

There are N cities in Bitlandia. Some cities are connected by train or bus, and connections work both ways. Marijonas is preparing for a month-long vacation in Bitlandia. He wants to use trains as much as possible, so he bought a monthly railway ticket. This ticket allows Marijonas unlimited train travel for a month, but it does not cover the cost of bus trips.</p>

Marijonas wants to stay in one city - but is not yet sure in which one. Marijonas would like to stay in a city which allows for a cheap travel to all other cities, because he intends to travel to some of them while staying in the chosen city.

For Marijonas, going from one city to another is cheap if there is a route on which he travels on an unlimited number of trains and at most one bus.

Calculate the number of Bitlandia cities Marijonas could stay in.

입력 형식

Two numbers are given in the first line: the number of cities N, and the number of connections between two cities M. Cities are labeled by 1 up to N, inclusive.</p>

Each of the next M lines consist of two numbers, ai and bi, and a character Ti. The i-th connection connects cities ai and bi, while the character Ti indicates the transportation type of the connection: if Ti is T, then the i-th connection is by train, and if Ti is A, it is by bus.

출력 형식

Output a single integer – the number of cities Marijonas could stay in.

예제 입력 1

5 5
3 1 A
3 5 A
4 5 T
2 3 T
2 1 A

예제 출력 1

2

예제 입력 2

7 8
7 5 A
3 7 A
3 1 A
3 4 T
3 5 A
7 1 A
5 6 T
2 7 T

예제 출력 2

4

Comments

There are no comments at the moment.