点我看题目

题意 :其实题意我也说不清楚,因为比赛的时候我盯着看了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. HTTP请求、响应报文格式

    HTTP请求报文格式: HTTP请求报文主要由请求行.请求头部.空行以及请求正文4部分组成 1,请求行由3部分组成,分别为:请求方式,URI(注意这里不是URL)以及协议版本组成,之间由空格分隔 请求 ...

  2. 在masterpage中添加对usercontrol的引用

    在masterpage中添加对usercontrol的引用的方式: <%@ Register Src="/_controltemplates/15/Excellent Employee ...

  3. c语言学习之基础知识点介绍(十三):枚举的介绍和使用

    一.枚举的介绍 /* 枚举:限制的待选项. 语法: enum 枚举名{ 选项1, 选项2, 选项3, ........ 选项n }; 注意:枚举中,选项之间用 , 隔开,最后一个不用加 , :并且枚举 ...

  4. 有理数类 Java

    public class Rational extends Number implements Comparable { private long numerator;// 分子 private lo ...

  5. spring mvc源码解析

    1.从DispatcherServlet开始 与很多使用广泛的MVC框架一样,SpringMVC使用的是FrontController模式,所有的设计都围绕DispatcherServlet 为中心来 ...

  6. Http上传文件

    public class UpLoadFile { public static void UpLoadFiles(string fileName) { string fileType = Path.G ...

  7. javascript document对象 第21节

    <html> <head> <title>DOM对象</title> <style type="text/css"> t ...

  8. java新手笔记5 类

    1.进制转换 /* 企业发放的奖金根据利润提成. 利润(I)低于或等于10万元时,奖金可提10%: 利润高于10万元,低于20万元时, 低于10万元的部分按10%提成,高于10万元的部分,可提成7.5 ...

  9. 国庆第三天2014年10月3日10:21:39,Nutz,WebCollector,jsoup

    (1)做得好,做得快,只能选择一样. (2)时间过得很快,你没法在假期的一天里完成更多的计划.假期全部由自己支配,相对长一点的睡眠,新加入的娱乐(视频或者游戏),你不比在工作中更有效率. (3)每天练 ...

  10. 新建DragonBones动画文件

    本篇文章由:http://www.sollyu.com/new-dragonbones-animation-file/ 说明 我在网上找了很久都没找到关于怎么创建一个DragonBones动画的文章, ...