POJ 1778 All Discs Considered(拓扑排序)
题意 :其实题意我也说不清楚,因为比赛的时候我盯着看了1个小时也没看懂。。。。就是两个磁盘,第一个有n1的安装包,编号为1~n1,第二个有n2个安装包,编号为n1~n2。给你d对关系,(x,y)代表着安装x之前要先安装y。然后让你往里插入这两个磁盘,因为一次只能插一次,所以要满足他给的条件的时候要频繁的来回换。还有,要注意的一点是,无论先往里边插第一个还是第2个,第一次插的时候算一次,最后一次拔的时候算一次。
思路 :其实我真不知道这是拓扑排序,,,,,后来才知道的。。。。。
//#include <iostream>
//#include <string.h>
//#include <stdio.h>
//#include <algorithm>
//
//const int maxn = 424567 ;
//char sh[maxn] ;
//char ch[maxn] ;
//
//using namespace std;
//
//int main()
//{
// while(gets(sh))
// {
// int x = 0,j ;
// int len = strlen(sh) ;
// // printf("%d",len);
// for(int i = 0 ; i < len ; )
// {
// j = i + 1;
// if(sh[i] == sh[j])
// {
// while(sh[i] == sh[j])
// {
// j++ ;
// if(j - i >= 9)
// break ;
// }
// ch[x++] = j-i+'0' ;
// ch[x++] = sh[i] ;
// i += (j-i) ;
// }
// else
// {
// while((sh[j] != sh[j+1] && j+1 < len) || j == len-1)
// j++ ;
// ch[x++] = '1' ;
// for(int ii = i ; ii < j ; ii++)
// {
// ch[x++] = sh[ii] ;
// if(sh[ii] == '1' )
// ch[x++] = '1' ;
// //if(j == 0 && sh[j] != sh[j+1])
// //ch[x++] = '1' ;
// }
// ch[x++] = '1' ;
// i += (j-i) ;
// }
// }
// ch[x] = '\0' ;
// printf("%s\n",ch) ;
// }
// return 0;
//}
//
//#include <stdio.h>
//#include <string.h>
//#include <iostream>
//
//using namespace std ;
//int work(int m,int n)
//{
// int sum = 1;
// for(int i = 1 ; i <= n ; i++)
// sum *= 10 ;
// return m*sum ;
//}
//int main()
//{
// char ch[5] ;
// while(scanf("%s",ch) != EOF)
// {
// if(strcmp(ch,"00e0") == 0) break ;
// int s = (ch[0]-'0')*10+ch[1]-'0' ;
// int x = ch[3]-'0' ;
//
// int sum = work(s,x) ;
// int i = 1,num = 0 ;
// while(i <= sum)
// {
// i *= 2 ;
// num++ ;
// }
// printf("%d\n",2*(sum - i/2)+1) ;
// }
// return 0 ;
//}
#include <iostream>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <algorithm>
#include <queue> using namespace std ; const int maxn = ;
const int INF = ; int n,n1,n2,d;
int head[maxn],cnt,ans,deg[maxn],degr[maxn] ; struct node
{
int u,v,w ;
int next ;
} Edge[maxn] ;
queue<int>Q[] ; void addedge(int u,int v)
{
Edge[cnt].u = u ;
Edge[cnt].v = v ;
Edge[cnt].next = head[u] ;
head[u] = cnt++ ;
} void toposort(int u)
{
int sum = ;
for(int i = ; i <= n ; i++)
{
if(deg[i] == )
{
if(i <= n1) Q[].push(i) ;
else Q[].push(i) ;
}
}
int v,w ;
while(!Q[u].empty() || !Q[u^].empty())
{
while(!Q[u].empty())
{
v = Q[u].front() ;
Q[u].pop() ;
for(int i = head[v] ; i + ; i = Edge[i].next)
{
w = Edge[i].v ;
deg[w]-- ;
if(!deg[w])
{
if(w <= n1) Q[].push(w) ;
else Q[].push(w) ;
}
}
}
u = u^ ;
sum++ ;
}
if(sum < ans) ans = sum ;
} void Init()
{
n = n1+n2 ;
memset(head,-,sizeof(head)) ;
memset(degr,,sizeof(degr)) ;
cnt = ;
ans = INF ;
}
int main()
{
while(~scanf("%d %d %d",&n1,&n2,&d))
{
if(n1 == && n2 == && d == ) break ;
Init() ;
int x,y ;
for(int i = ; i <= d ; i++)
{
scanf("%d %d",&x,&y) ;
degr[x]++ ;
addedge(y,x) ;
}
for(int i = ; i <= n ; i++)
deg[i] = degr[i] ;
toposort() ;
for(int i = ; i <= n ; i++)
deg[i] = degr[i] ;
toposort() ;
printf("%d\n",ans+) ;
}
return ;
}
POJ 1778 All Discs Considered(拓扑排序)的更多相关文章
- All Discs Considered(拓扑排序)
http://poj.org/problem?id=1778 题意:有两个DVD,第一个DVD上有编号为1~n1的安装包,第二个DVD上有编号为n1+1~n1+n2的安装包,给出m组关系(a,b) 表 ...
- POJ 3249 Test for Job (拓扑排序+DP)
POJ 3249 Test for Job (拓扑排序+DP) <题目链接> 题目大意: 给定一个有向图(图不一定连通),每个点都有点权(可能为负),让你求出从源点走向汇点的路径上的最大点 ...
- poj 3687 Labeling Balls - 贪心 - 拓扑排序
Windy has N balls of distinct weights from 1 unit to N units. Now he tries to label them with 1 to N ...
- POJ 3687 Labeling Balls【拓扑排序 优先队列】
题意:给出n个人,m个轻重关系,求满足给出的轻重关系的并且满足编号小的尽量在前面的序列 因为输入的是a比b重,但是我们要找的是更轻的,所以需要逆向建图 逆向建图参看的这一篇http://blog.cs ...
- poj 2367 Genealogical tree (拓扑排序)
火星人的血缘关系很奇怪,一个人可以有很多父亲,当然一个人也可以有很多孩子.有些时候分不清辈分会产生一些尴尬.所以写个程序来让n个人排序,长辈排在晚辈前面. 输入:N 代表n个人 1~n 接下来n行 第 ...
- poj 3683 2-sat建图+拓扑排序输出结果
发现建图的方法各有不同,前面一题连边和这一题连边建图的点就不同,感觉这题的建图方案更好. 题意:给出每个婚礼的2个主持时间,每个婚礼的可能能会冲突,输出方案. 思路:n个婚礼,2*n个点,每组点是对称 ...
- poj 2762 强连通缩点+拓扑排序
这题搞了好久,先是拓扑排序这里没想到,一开始自己傻乎乎的跑去找每层出度为1的点,然后才想到能用拓扑排序来弄. 拓扑排序的时候也弄了挺久的,拓扑排序用的也不多. 题意:给一个图求是否从对于任意两个点能从 ...
- POJ 1270 Following Orders(拓扑排序)
题意: 给两行字符串,第一行为一组变量,第二行时一组约束(每个约束包含两个变量,x y 表示 x <y).输出满足约束的所有字符串序列. 思路:拓扑排序 + 深度优先搜索(DFS算法) 课本代码 ...
- 【POJ 2585】Window Pains 拓扑排序
Description . . . and so on . . . Unfortunately, Boudreaux's computer is very unreliable and crashes ...
随机推荐
- 破解C#的readonly只读字段
破解C#的readonly只读字段 目录 请允许我再唠叨几句const和readonly 修改readonly字段: 计策1:反间计 -- 反射修改 计策2:借刀杀人--调节字段偏移位置的结构体来修改 ...
- Linux基础命令(三)
一.常用命令—文件目录类命令 1.ls 列出指定或默认目录的文件信息 使用形式: ls [选项] [目录名] 实例: $ls $ls –als $ls /home/sq/Desktop $ls ./D ...
- C++学习(四)
一.拷贝构造函数和拷贝赋值运算符1.拷贝构造:用一个已有的对象,构造和它同类型的副本对象——克隆.2.形如class X { X (const X& that) { ... }};的构造函数 ...
- window nodejs 版本切换 nvmw
参考文档:https://cnodejs.org/topic/5338c5db7cbade005b023c98 nvmw 下载到本地 git clone https://github.com/hako ...
- SQL Server delete、truncate、drop
在T-SQL中这三个命令符,相信很多朋友都不会陌生的,我自己在工作也会常常使用到它们,虽然我们清除的知道用这三个命令符可以达到怎样的预期效果. 但是却很少深入的去了解它们,知道它们有什么区别,又各有什 ...
- 在VM虚拟机中安装centos7
- Android adapter适配器的使用
说起Adapter的使用,首先想到的就是listview或各种各样的Adapter.下面我们对常用的一些Adapter进行简单的使用讲解. 这是Adapter的关系图: 下面的所有例子均使用同一个布局 ...
- hadoop_集群安装_1
这篇文章中主要介绍的是,如何基于VM安装Linux,以及如何在安装好Linux之后,基于操作系统安装VMTools. 在安装之前,应该先规划好 每个node*的IP地址,以及 hostname: no ...
- 数组做为参数传入Oracle存储过程操作数据库
p { margin-bottom: 0.25cm; direction: ltr; color: rgb(0, 0, 0); line-height: 120%; text-align: justi ...
- React-Native牛刀小试仿京东砍啊砍砍到你手软
React-Native牛刀小试仿京东砍啊砍砍到你手软 React-Native基础教程 *React-Native基础篇作者git *React-Native官方文档 *Demo 几个月前faceb ...