传送门

Description

Furik and Rubik love playing computer games. Furik has recently found a new game that greatly interested Rubik. The game consists ofn 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 Input

110

52 2 1 1 31 52 5 12 5 41 50

Sample Output

1

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<cstring>
#include<queue>
#include<vector>
#include<algorithm>
using namespace std;
vector<int>itv[5],edge[205];
int Indegree[205];
int In[205];
int id[205];

int solve(int x)
{
	int res = 0;
	queue<int>que[5];
	for (int i = 1;i < 205;i++)
	{
		In[i] = Indegree[i];
	}
	for (int i = 1;i <= 3;i++)
	{
		for (int j = 0;j < itv[i].size();j++)
		{
			if (In[itv[i][j]] == 0)
			{
				que[i].push(itv[i][j]);
			}
		}
	}
	for (int i = x;;i = (i+1)%3)
	{
		if (i == 0)
		{
			i = 3;
		}
		while (!que[i].empty())
		{
			int val = que[i].front();
			que[i].pop();
			res++;
			for (int j = 0;j < edge[val].size();j++)
			{
				if (--In[edge[val][j]] == 0)
				{
					que[id[edge[val][j]]].push(edge[val][j]);
				}
			}
		}
		if (que[1].empty() && que[2].empty() && que[3].empty())	break;
		res++;
	}
	return res;
}

int main()
{
	int N,tmp,cnt;
	memset(Indegree,0,sizeof(Indegree));
	memset(id,0,sizeof(id));
	for (int i = 0;i < 5;i++)
	{
		itv[i].clear();
	}
	for (int i = 0;i < 205;i++)
	{
		edge[i].clear();
	}
	scanf("%d",&N);
	for (int i = 1;i <= N;i++)
	{
		scanf("%d",&tmp);
		itv[tmp].push_back(i);
		id[i] = tmp;
	}
	for (int i = 1;i <= N;i++)
	{
		scanf("%d",&cnt);
		while (cnt--)
		{
			scanf("%d",&tmp);
			edge[tmp].push_back(i);
			Indegree[i]++;
		}
	}
	int res = 0x3f3f3f3f;
	for (int i = 1;i <= 3;i++)
	{
		res = min(res,solve(i));
	}
	printf("%d\n",res);
	return 0;
}

  

 

 

CF 213A Game(拓扑排序)的更多相关文章

  1. CF 915 D 拓扑排序

    #include <bits/stdc++.h> using namespace std; const int maxn = 1e5 + 10; const int mod = 14285 ...

  2. [CF #290-C] Fox And Names (拓扑排序)

    题目链接:http://codeforces.com/contest/510/problem/C 题目大意:构造一个字母表,使得按照你的字母表能够满足输入的是按照字典序排下来. 递归建图:竖着切下来, ...

  3. CF Fox And Names (拓扑排序)

    Fox And Names time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...

  4. CF #CROC 2016 - Elimination Round D. Robot Rapping Results Report 二分+拓扑排序

    题目链接:http://codeforces.com/contest/655/problem/D 大意是给若干对偏序,问最少需要前多少对关系,可以确定所有的大小关系. 解法是二分答案,利用拓扑排序看是 ...

  5. CF 274D Lovely Matrix 拓扑排序,缩点 难度:2

    http://codeforces.com/problemset/problem/274/D 这道题解题思路: 对每一行统计,以小值列作为弧尾,大值列作为弧头,(-1除外,不连弧),对得到的图做拓扑排 ...

  6. Java排序算法——拓扑排序

    package graph; import java.util.LinkedList; import java.util.Queue; import thinkinjava.net.mindview. ...

  7. CF1131D Gourmet choice(并查集,拓扑排序)

    这题CF给的难度是2000,但我感觉没这么高啊…… 题目链接:CF原网 题目大意:有两个正整数序列 $a,b$,长度分别为 $n,m$.给出所有 $a_i$ 和 $b_j(1\le i\le n,1\ ...

  8. Codeforces Round #397 by Kaspersky Lab and Barcelona Bootcamp (Div. 1 + Div. 2 combined) E. Tree Folding 拓扑排序

    E. Tree Folding 题目连接: http://codeforces.com/contest/765/problem/E Description Vanya wants to minimiz ...

  9. BZOJ1880:[SDOI2009]Elaxia的路线(最短路,拓扑排序)

    Description 最近,Elaxia和w**的关系特别好,他们很想整天在一起,但是大学的学习太紧张了,他们 必须合理地安排两个人在一起的时间.Elaxia和w**每天都要奔波于宿舍和实验室之间, ...

  10. 算法与数据结构(七) AOV网的拓扑排序

    今天博客的内容依然与图有关,今天博客的主题是关于拓扑排序的.拓扑排序是基于AOV网的,关于AOV网的概念,我想引用下方这句话来介绍: AOV网:在现代化管理中,人们常用有向图来描述和分析一项工程的计划 ...

随机推荐

  1. SpringMVC对日期类型的转换

    在做web开发的时候,页面传入的都是String类型,SpringMVC可以对一些基本的类型进行转换,但是对于日期类的转换可能就需要我们配置. 1.如果查询类使我们自己写,那么在属性前面加上@Date ...

  2. 关于Jquery学习的几点反思

    1.应用的API插件时,先看说明文档,仔细看,多花点时间也没关系. 2.写出你要完成工作的几个步骤,细化你的工作.保证每一步结果都是正确(特别是在用你不会的东西的时候),这样看起来是在浪费时间,但是最 ...

  3. Socket编程基础——无连接UDP

    与面向连接的网络连接相比,无连接的网络通信不需要在服务器与客户端之间建立连接.面向非连接的Socket通信是基于UDP的,服务器端不需要调用listen()和accept()函数来等待客户端的连接:客 ...

  4. 在Linux下搭建SVN服务器

    svn不仅仅可以用于程序开发,还可以做很多事情,例如备份文档. CentOS下:安装 这样同一台服务器便可以运行多个svnserver了 检查端口 注:如果修改了svn配置,需要重启svn服务 -j ...

  5. /Users/alamps/AndroidStudioProjects/Demo10ScrollView

    .define xml <ScrollView xmlns:android="http://schemas.android.com/apk/res/android" andr ...

  6. Sdut 2416 Fruit Ninja II(山东省第三届ACM省赛 J 题)(解析几何)

    Time Limit: 5000MS Memory limit: 65536K 题目描述 Haveyou ever played a popular game named "Fruit Ni ...

  7. oracle中 connect by prior 递归算法 -- 理解

    oracle中 connect by prior 递归算法 -- 理解 http://blog.163.com/xxciof/blog/static/7978132720095193113752/  ...

  8. Web API CSRF保护实现

    Web API CSRF保护实现 这次自己实现了类似jQuery中ajax调用的方法,并且针对RESTFul进行了改造和集成,实现的A2D AJAX接口如下: $.ajax.RESTFulGetCol ...

  9. 刨根问底儿 -- intVal($str) 跟 (int) $str 的运算结果有什么区别

    intVal($str) 跟 (int) $str 都是把其他类型的变量转化为int型变量的方式,这么多年来我一直森森滴怀疑它们的运算结果在某些条件下会有区别.对于我的疑问,文档里也没有多说(或者我没 ...

  10. 关于怎么快速学好Android应用程序开发及其其他编程语言(大牛和高手勿喷,此篇文章也适合刚入门小师弟和小师妹)

    无论你是从.NET转过来的也好 还是从PHP转过来的等等等,能看到这篇文章的人一般都是想快速转行到Android应用程序开发,希望我的这篇文章能勉励到各位的同时,也能勉励我自己. 1.编程语言基本都会 ...