编程介的小学生 2019-10-04 17:58 采纳率: 0.4%
浏览 105

Sightseeing tour 履行的问题

Description

The city executive board in Lund wants to construct a sightseeing tour by bus in Lund, so that tourists can see every corner of the beautiful city. They want to construct the tour so that every street in the city is visited exactly once. The bus should also start and end at the same junction. As in any city, the streets are either one-way or two-way, traffic rules that must be obeyed by the tour bus. Help the executive board and determine if it's possible to construct a sightseeing tour under these constraints.
Input

On the first line of the input is a single positive integer n, telling the number of test scenarios to follow. Each scenario begins with a line containing two positive integers m and s, 1 <= m <= 200,1 <= s <= 1000 being the number of junctions and streets, respectively. The following s lines contain the streets. Each street is described with three integers, xi, yi, and di, 1 <= xi,yi <= m, 0 <= di <= 1, where xi and yi are the junctions connected by a street. If di=1, then the street is a one-way street (going from xi to yi), otherwise it's a two-way street. You may assume that there exists a junction from where all other junctions can be reached.
Output

For each scenario, output one line containing the text "possible" or "impossible", whether or not it's possible to construct a sightseeing tour.
Sample Input

4
5 8
2 1 0
1 3 0
4 1 1
1 5 0
5 4 1
3 4 0
4 2 1
2 2 0
4 4
1 2 1
2 3 0
3 4 0
1 4 1
3 3
1 2 0
2 3 0
3 2 0
3 4
1 2 0
2 3 1
1 2 0
3 2 0
Sample Output

possible
impossible
impossible
possible

  • 写回答

1条回答 默认 最新

  • 你知我知皆知 2024-07-30 01:00
    关注

    以下回答参考 皆我百晓生券券喵儿 等免费微信小程序相关内容作答,并由本人整理回复。

    # 读取数据
    n = readLines()[1]
    m = as.integer(readLines()[[1]])
    s = as.integer(readLines()[[2]])
    
    for(i in 1:s){
        lines = readLines()[[(i+1):((i+1)+s)]]
    
        for(j in 1:m){
            x = as.integer(strsplit(lines[j], ", ")[[1]][1])
            y = as.integer(strsplit(lines[j], ", ")[[1]][2])
            d = as.integer(strsplit(lines[j], ", ")[[1]][3])
    
            if(d == 0) { # 一条单向街
                if(x < y) {
                    print("impossible")
                    break
                }
            } else { # 两条街
                if(x < y && x > y-d || x > y+d) {
                    print("impossible")
                    break
                }
            }
        }
    
        if(i == s-1) {
            print("possible")
        }
    }
    
    评论

报告相同问题?