互联网笔试——服务器启动题

某厂笔试题,居然瞎胡做出来了,先把不优雅得答案记录一下。

第一行n,p表示有n台机器,p次操作。

第 i 行第一个整数 c 表示第 i 个服务所依赖的服务数量,后面 c 个整数表示它所依赖的各个服务,保证这 c 个整数在 1~n 范围内,互不相等且都不等于 i。

x, y。x 为 1 或 0,1 表示启动服务,0 表示停止服务。y 表示启动或停止的服务的序号。

输入样例

3 2
1 2
1 3
0
1 1
0 2

输出:

3

1

def operate(machine_id,machine_state, rely_lst):
    if machine_state[machine_id]==1:
        return
    machine_state[machine_id] = 1
    # 依赖开
    for re in rely_lst[machine_id]:
        if machine_state[re]==1:
            continue
        else:
            operate(re,machine_state, rely_lst)

def close(machine_id, machine_state, rely_lst):
    if machine_state[machine_id]==0:
        return
    machine_state[machine_id] = 0
    for m_id in range(1,len(rely_lst)):
        if machine_id in rely_lst[m_id]:
            close(m_id, machine_state, rely_lst)

def main():
    n,q = list(map(int, input().strip().split(' ')))
    rely_lst = [[]]
    machine_state=[0] * (n+1)
    for i in range(n):
        tmp = list(map(int, input().strip().split(' ')))
        rely_num = tmp[0]
        if rely_num != 0:
            rely_lst.append(tmp[1:])
        else:
            rely_lst.append([])
    for j in range(q):
        x, y = list(map(int, input().strip().split(' ')))
        if x == 1:
            operate(y, machine_state,rely_lst)
        else:
            close(y, machine_state, rely_lst)
    num = 0
    for i in machine_state:
        if i==1:
            num += 1
    print(num)

main()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值