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 ≤ n; ai, 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
Sample Input
110 52 2 1 1 31 52 5 12 5 41 50
Sample Output
1 7
Note
#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(拓扑排序)的更多相关文章
-
CF 915 D 拓扑排序
#include <bits/stdc++.h> using namespace std; const int maxn = 1e5 + 10; const int mod = 14285 ...
-
[CF #290-C] Fox And Names (拓扑排序)
题目链接:http://codeforces.com/contest/510/problem/C 题目大意:构造一个字母表,使得按照你的字母表能够满足输入的是按照字典序排下来. 递归建图:竖着切下来, ...
-
CF Fox And Names (拓扑排序)
Fox And Names time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...
-
CF #CROC 2016 - Elimination Round D. Robot Rapping Results Report 二分+拓扑排序
题目链接:http://codeforces.com/contest/655/problem/D 大意是给若干对偏序,问最少需要前多少对关系,可以确定所有的大小关系. 解法是二分答案,利用拓扑排序看是 ...
-
CF 274D Lovely Matrix 拓扑排序,缩点 难度:2
http://codeforces.com/problemset/problem/274/D 这道题解题思路: 对每一行统计,以小值列作为弧尾,大值列作为弧头,(-1除外,不连弧),对得到的图做拓扑排 ...
-
CF思维联系--CodeForces -214C (拓扑排序+思维+贪心)
ACM思维题训练集合 Furik and Rubik love playing computer games. Furik has recently found a new game that gre ...
-
Java排序算法——拓扑排序
package graph; import java.util.LinkedList; import java.util.Queue; import thinkinjava.net.mindview. ...
-
CF1131D Gourmet choice(并查集,拓扑排序)
这题CF给的难度是2000,但我感觉没这么高啊…… 题目链接:CF原网 题目大意:有两个正整数序列 $a,b$,长度分别为 $n,m$.给出所有 $a_i$ 和 $b_j(1\le i\le n,1\ ...
-
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 ...
-
BZOJ1880:[SDOI2009]Elaxia的路线(最短路,拓扑排序)
Description 最近,Elaxia和w**的关系特别好,他们很想整天在一起,但是大学的学习太紧张了,他们 必须合理地安排两个人在一起的时间.Elaxia和w**每天都要奔波于宿舍和实验室之间, ...
随机推荐
-
今天遇到的点击添加按钮button_click代码段无法执行的问题
首先:本人小白一枚,刚入行,如有表述不当的地方,还请多多指教 网页界面如图: 当点击添加按钮后断点测试进入后台代码运行: 代码会先执行Page_Load页面,当加载完后Page_Load代码会跳转到m ...
-
Rainyday.js – 使用 JavaScript 实现雨滴效果
Rainyday.js 背后的想法是创建一个 JavaScript 库,利用 HTML5 Canvas 渲染一个雨滴落在玻璃表面的动画.Rainyday.js 有功能可扩展的 API,例如碰撞检测和易 ...
-
HBase with MapReduce (Read and Write)
上面一篇文章仅仅是介绍如何通过mapReduce来对HBase进行读的过程,下面将要介绍的是利用mapreduce进行读写的过程,前面我们已经知道map实际上是读过程,reduce是写的过程,然而ma ...
- 利用文本编辑器输入Hello.java,并在JDK环境下编译和运行。请将程序编译、运行的结果
-
详谈socket请求Web服务器过程
最开始我们需要明白一件事情,因为这是这篇文章的前提: HTTP协议只是一个应用层协议,它底层是通过TCP进行传输数据的.因此,浏览器访问Web服务器的过程必须先有“连接建立”的发生. 而有人或许会问: ...
-
Android 快捷方式相关操作
尽管现在少数手机不支持快捷方式,但是仍然有大部分手机是支持的.创建快捷方式,可以减少用户在应用列表繁多的应用程序中查找应用的时间,快速进入应用:或是应用中的某个功能使用频率较高,创建快捷方式,可以快速 ...
-
Linux文件系统的层级结构
Linux文件系统的层级结构 文件结构 倒置的树状结构 :Linux的哲学思想是一切皆文件,把几乎所有资源统统抽象为文件形式:包括硬件设备,甚至通信接口等 根目录 :linux的文件起始均从唯一的 ...
-
sed运用
流编辑器sed sed简介 sed是stream editor的缩写,一种流编辑器,它一次处理一行内容.处理时,把当前处理的行存储在临时缓冲区中,称为"模式空间"(pattern ...
-
在spring中实现quartz2.2.1的动态调度(开始、暂停、停止等)
参考原文地址: https://blog.csdn.net/fantasic_van/article/details/74942062 一.新建job1 package com.cvicse.ump. ...
-
e1000
http://blog.csdn.net/sdulibh/article/details/41826221 http://blog.csdn.net/evenness/article/details/ ...