A. Game
time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

Furik and Rubik love playing computer games. Furik has recently found a new game that greatly interested Rubik. The game consists of n parts and to complete each part a player may probably need to complete some other ones. We know that the game can be fully completed, that is, its parts do not form cyclic dependencies.

Rubik has 3 computers, on which he can play this game. All computers are located in different houses. Besides, it has turned out that each part of the game can be completed only on one of these computers. Let's number the computers with integers from 1 to 3. Rubik can perform the following actions:

  • Complete some part of the game on some computer. Rubik spends exactly 1 hour on completing any part on any computer.
  • Move from the 1-st computer to the 2-nd one. Rubik spends exactly 1 hour on that.
  • Move from the 1-st computer to the 3-rd one. Rubik spends exactly 2 hours on that.
  • Move from the 2-nd computer to the 1-st one. Rubik spends exactly 2 hours on that.
  • Move from the 2-nd computer to the 3-rd one. Rubik spends exactly 1 hour on that.
  • Move from the 3-rd computer to the 1-st one. Rubik spends exactly 1 hour on that.
  • Move from the 3-rd computer to the 2-nd one. Rubik spends exactly 2 hours on that.

Help Rubik to find the minimum number of hours he will need to complete all parts of the game. Initially Rubik can be located at the computer he considers necessary.

Input

The first line contains integer n (1 ≤ n ≤ 200) — the number of game parts. The next line contains n integers, the i-th integer — ci (1 ≤ ci ≤ 3) represents the number of the computer, on which you can complete the game part number i.

Next n lines contain descriptions of game parts. The i-th line first contains integer ki (0 ≤ ki ≤ n - 1), then ki distinct integers ai, j (1 ≤ ai, j ≤ nai, j ≠ i) — the numbers of parts to complete before part i.

Numbers on all lines are separated by single spaces. You can assume that the parts of the game are numbered from 1 to n in some way. It is guaranteed that there are no cyclic dependencies between the parts of the game.

Output

On a single line print the answer to the problem.

Sample test(s)
Input
1
1
0
Output
1
Input
5
2 2 1 1 3
1 5
2 5 1
2 5 4
1 5
0
Output
7
Note

Note to the second sample: before the beginning of the game the best strategy is to stand by the third computer. First we complete part 5. Then we go to the 1-st computer and complete parts 3 and 4. Then we go to the 2-nd computer and complete parts 1 and 2. In total we get 1+1+2+1+2, which equals 7 hours.

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <set>
#include <vector>
#include <queue>
#include <stack>
#include <cmath>
#include <map>
using namespace std;
#define INF 0x73737373
#define EPS 1e-8
#define lson l, m, rt<<1
#define rson m+1, r, rt<<1|1
int pos[], n, ret;
vector<int> pre[];
bool vis[];
bool pre_check(int index)
{
for(int i = ; i < pre[index].size(); i++)
if(!vis[pre[index][i]])return false;
return true;
}
bool all_complete()
{
for(int i = ; i <= n; i++)if(!vis[i])return false;
return true;
}
void work(int now, int cost)
{
while(true)
{
bool find = false;
for(int i = ; i <= n; i++)
{
if(vis[i])continue;
if(pre_check(i) && pos[i] == now)
{
vis[i] = true;
find = true;
cost++;
}
}
if(!find)break;
}
if(all_complete())
{
ret = min(ret, cost);
return;
}
work((now % ) + , cost + );
}
int main()
{
scanf("%d", &n);
for(int i = ; i <= n; i++)scanf("%d", &pos[i]);
for(int i = ; i <= n; i++)
{
int num;
scanf("%d", &num);
for(int j = ; j <= num; j++)
{
int a;
scanf("%d", &a);
pre[i].push_back(a);
}
}
ret = INF;
for(int i = ; i <= ; i++)
{
memset(vis, false, sizeof(vis));
vis[] = true;
work(i, );
}
printf("%d\n", ret);
return ;
}

Codeforces Round #131 (Div. 1) A - Game的更多相关文章

  1. Codeforces Round #131 (Div. 1) B. Numbers dp

    题目链接: http://codeforces.com/problemset/problem/213/B B. Numbers time limit per test 2 secondsmemory ...

  2. Codeforces Round #131 (Div. 2) B. Hometask dp

    题目链接: http://codeforces.com/problemset/problem/214/B Hometask time limit per test:2 secondsmemory li ...

  3. Codeforces Round #131 (Div. 2) E. Relay Race dp

    题目链接: http://codeforces.com/problemset/problem/214/E Relay Race time limit per test4 secondsmemory l ...

  4. Codeforces Round #131 (Div. 2)

    A. System of Equations \(a\)的范围在\(\sqrt n\)内,所以暴力枚举即可. B. Hometask 需要被2.5整除,所以末位必然为0,如果0没有出现,则直接返回-1 ...

  5. Codeforces Round #131 (Div. 2) : B

    首先能被2,5整除的数结尾必须是0: 如果没有0肯定不行: 然后判断他们的和ans%3: 如果==0,直接从大到小输出就行: 如果==1,要么删除它们之间最小的那个%3==1的,要么删除两个小的并且% ...

  6. Codeforces Round #366 (Div. 2) ABC

    Codeforces Round #366 (Div. 2) A I hate that I love that I hate it水题 #I hate that I love that I hate ...

  7. Codeforces Round #354 (Div. 2) ABCD

    Codeforces Round #354 (Div. 2) Problems     # Name     A Nicholas and Permutation standard input/out ...

  8. Codeforces Round #368 (Div. 2)

    直达–>Codeforces Round #368 (Div. 2) A Brain’s Photos 给你一个NxM的矩阵,一个字母代表一种颜色,如果有”C”,”M”,”Y”三种中任意一种就输 ...

  9. cf之路,1,Codeforces Round #345 (Div. 2)

     cf之路,1,Codeforces Round #345 (Div. 2) ps:昨天第一次参加cf比赛,比赛之前为了熟悉下cf比赛题目的难度.所以做了round#345连试试水的深浅.....   ...

随机推荐

  1. wifi两种工作模式

    AP:即无线接入点,是一个无线网络的中心节点.通常使用的无线路由器就是一个AP,其它无线终端 可以通过AP相互连接. STA:即无线站点,是一个无线网络的终端.如笔记本电脑.PDA等. 1>工作 ...

  2. aarch64_j1

    JSCookMenu-2.0.4-13.fc26.noarch.rpm 2017-02-14 07:06 37K fedora Mirroring Project Java-WebSocket-1.3 ...

  3. Error: No resource found that matches the given name (at 'icon' with value '@mipmap/Icon')

    问题: error: Error: No resource found that matches the given name (at 'icon' with value '@mipmap/Icon' ...

  4. 利用vw+rem实现移动web适配布局

    基本概念 1.单位 Px(CSS pixels) 像素 (px) 是一种绝对单位(absolute units), 因为无论其他相关的设置怎么变化,像素指定的值是不会变化的 其实是相对于某个设备而言的 ...

  5. CentOS/Linux 网卡设置 IP地址配置

    CentOS/Linux下设置IP地址 1:临时修改:1.1:修改IP地址# ifconfig eth0 192.168.100.100 1.2:修改网关地址# route add default g ...

  6. pip3

    pip3 install django #安装rabbitmq连接模块 pip3 install pika pip3 install paramiko pip3 install ipython pip ...

  7. Postman接口&压力测试

    Postman接口与压力测试实例 Postman是一款功能强大的网页调试与发送网页HTTP请求的Chrome插件.它提供功能强大的 Web API & HTTP 请求调试. 1.环境变量和全局 ...

  8. Luogu P3384 【【模板】树链剖分】

    转载请注明出处,部分内容引自banananana大神的博客 ~~别说你不知道什么是树~~╮(─▽─)╭(帮你百度一下) 先来回顾两个问题:1,将树从x到y结点最短路径上所有节点的值都加上z 这也是个模 ...

  9. Linux 命令find、grep

    本文就向大家介绍find.grep命令,他哥俩可以算是必会的linux命令,我几乎每天都要用到他们.本文结构如下: find命令 find命令的一般形式 find命令的常用选项及实例 find与xar ...

  10. NOIP2015&2016普及组解题报告

    NOIP2015普及组题目下载 NOIP2016普及组题目下载 NOIP2015普及组题目: NOIP2018RP++ NOIP2016普及组题目 NOIP2018RP++ T1 金币\((coin. ...