点我看题目

题意 :其实题意我也说不清楚,因为比赛的时候我盯着看了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(拓扑排序)的更多相关文章

  1. All Discs Considered(拓扑排序)

    http://poj.org/problem?id=1778 题意:有两个DVD,第一个DVD上有编号为1~n1的安装包,第二个DVD上有编号为n1+1~n1+n2的安装包,给出m组关系(a,b) 表 ...

  2. POJ 3249 Test for Job (拓扑排序+DP)

    POJ 3249 Test for Job (拓扑排序+DP) <题目链接> 题目大意: 给定一个有向图(图不一定连通),每个点都有点权(可能为负),让你求出从源点走向汇点的路径上的最大点 ...

  3. 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 ...

  4. POJ 3687 Labeling Balls【拓扑排序 优先队列】

    题意:给出n个人,m个轻重关系,求满足给出的轻重关系的并且满足编号小的尽量在前面的序列 因为输入的是a比b重,但是我们要找的是更轻的,所以需要逆向建图 逆向建图参看的这一篇http://blog.cs ...

  5. poj 2367 Genealogical tree (拓扑排序)

    火星人的血缘关系很奇怪,一个人可以有很多父亲,当然一个人也可以有很多孩子.有些时候分不清辈分会产生一些尴尬.所以写个程序来让n个人排序,长辈排在晚辈前面. 输入:N 代表n个人 1~n 接下来n行 第 ...

  6. poj 3683 2-sat建图+拓扑排序输出结果

    发现建图的方法各有不同,前面一题连边和这一题连边建图的点就不同,感觉这题的建图方案更好. 题意:给出每个婚礼的2个主持时间,每个婚礼的可能能会冲突,输出方案. 思路:n个婚礼,2*n个点,每组点是对称 ...

  7. poj 2762 强连通缩点+拓扑排序

    这题搞了好久,先是拓扑排序这里没想到,一开始自己傻乎乎的跑去找每层出度为1的点,然后才想到能用拓扑排序来弄. 拓扑排序的时候也弄了挺久的,拓扑排序用的也不多. 题意:给一个图求是否从对于任意两个点能从 ...

  8. POJ 1270 Following Orders(拓扑排序)

    题意: 给两行字符串,第一行为一组变量,第二行时一组约束(每个约束包含两个变量,x y 表示 x <y).输出满足约束的所有字符串序列. 思路:拓扑排序 + 深度优先搜索(DFS算法) 课本代码 ...

  9. 【POJ 2585】Window Pains 拓扑排序

    Description . . . and so on . . . Unfortunately, Boudreaux's computer is very unreliable and crashes ...

随机推荐

  1. 百度编辑器umeditor使用总结

    百度编辑器是一个功能很全.很强大. 百度单张图片上传只能存储在项目下面,而不能独立自定义存储位置,因此重写上传代码 百度文章中的图片是通过base64实现的,直接存储在数据库中 tomcat通过虚拟路 ...

  2. ios NSHashTable & NSMapTable

    在ios开发中大家用到更多的集合类可能是像NSSet或者NSDictionary,NSArray这样的.这里要介绍的是更少人使用的两个类,一个是NSMapTable,另一个是NSHashTable. ...

  3. [C#][转][string 字符串截取

    C#几个经常用到的字符串截取 一. 1.取字符串的前i个字符 (1)string str1=str.Substring(0,i); (2)string str1=str.Remove(i,str.Le ...

  4. mysql 断电 启动不了 start: Job failed to start

    公司内部服务器,突然断电,造成无法启动的解决办法 把my.cnf中配置的datadir路径下的ib_logfile* (比如ib_logfile0, lb_logfile1....)文件移到另外一个目 ...

  5. cakephp recursive -1,0,1,2 速查

    -1  : model本身 0   :model本身 + belongTo + hasOne 1   :model本身 + belongTo + hasOne + hasMany 2:  :model ...

  6. u-boot和linux的机器码

    先看u-boot的机器码和linux的机器码是在什么地方决定的. 1. u-boot的机器码是在u-boot的board/fs2410/fs2410.c文件里决定的:     /* arch numb ...

  7. sql server 数据库正在使用该文件的解决办法

    今天在帮朋友还原数据库时遇到了一个问题.朋友用的是sql server 2008数据库,本身有一个数据库,他在修改程序的时候,想修改数据库的内容.但是又不想在原数据库中修改.想备份还原出一个数据库然后 ...

  8. jquery中的 .html(),.val().text()

    .html(),.text(),.val(),.html()用为读取和修改元素的HTML标签,包括标签内的内容.text()用来读取或修改元素的纯文本内容,去除 html 标签.val()用来读取或修 ...

  9. XML3_XML元素和节点的具体解释

    就像一个树状的目录.可以把第一行当作它扎根的“土地”.XML文件是由节点构成的.它的第一个节点为“根节点”.一个XML文件必须有且只能有一 个根节点,其他节点都必须是它的子节点.我们在FLASH里使用 ...

  10. php缓存相关

    在php运行期间,php引擎要对php源码进行处理,(词法分析,语法分析等)然后生成opcode. 然后再运行.在这个阶段可以把opcode缓存起来,当下次需要运行这段程序的时候,就避免了再次 进行词 ...