poj3687Labeling Balls
http://poj.org/problem?id=3687
样例太唬人了,求得是从1到N重量 而不是排好序的标签
逆向建图 把最重的赋给第一个入度为0的标签 如有多个赋给标签大的 这样能保证重量大的在后面
1 #include <iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 using namespace std; 6 int to[301],w[301][301],de[301],tt[301]; 7 int topo(int n) 8 { 9 int i,j,k,g,f; 10 for(i = n; i >= 1 ; i--) 11 { 12 f = 1; 13 for(j = n; j >= 1 ; j--) 14 { 15 if(de[j]==0) 16 { 17 f = 0 ; 18 de[j]--; 19 k = j; 20 to[k] = i; 21 for(g = 1; g <= n; g++) 22 if(w[k][g]) 23 de[g]--; 24 break; 25 } 26 } 27 if(f) 28 break; 29 } 30 return f; 31 } 32 int main() 33 { 34 int t,n,m,i,j,k,a,b; 35 cin>>t; 36 while(t--) 37 { 38 int flag = 1; 39 memset(de,0,sizeof(de)); 40 memset(w,0,sizeof(w)); 41 cin>>n>>m; 42 for(i = 1; i <= m ; i++) 43 { 44 cin>>a>>b; 45 if(!w[b][a]) 46 { 47 w[b][a] = 1; 48 de[a]++; 49 } 50 if(a==b) 51 flag = 0; 52 } 53 if(!flag) 54 { 55 puts("-1"); 56 continue; 57 } 58 if(topo(n)) 59 { 60 puts("-1"); 61 continue; 62 } 63 else 64 { 65 for(i = 1;i < n ; i++) 66 cout<<to[i]<<" "; 67 cout<<to[n]<<endl; 68 } 69 } 70 return 0; 71 }
转载于:https://www.cnblogs.com/shangyu/archive/2012/12/06/2805228.html
poj3687Labeling Balls相关推荐
- POJ-3687-Labeling Balls
链接:https://vjudge.net/problem/POJ-3687 题意: 给n个求再个m个条件,a比b轻,找出n个求每个对应的最小重量, 同时保证1尽量小.2尽量小... 思路: 拓扑排序 ...
- 例题6-6 小球下落(Dropping Balls, UVa 679)
例题6-6 小球下落(Dropping Balls, UVa 679) 许多的小球一个一个的从一棵满二叉树上掉下来组成一个新满二叉树,每一时间,一个正在下降的球第一个访问的是非叶子节点.然后继续下降时 ...
- AC日记——Red and Blue Balls codeforces 399b
399B - Red and Blue Balls 思路: 惊讶的发现,所有的蓝球的消除都是独立的: 对于在栈中深度为i的蓝球消除需要2^i次操作: 代码: #include <cstdio&g ...
- HEU 4048 Picking Balls
1/**//************************************** 2Problem: HEU 4048 Picking Balls 3Time: 0.0000 s 4M ...
- POJ 3687 Labeling Balls(拓扑序列)
Labeling Balls 大意:n个重量分别为1-n的小球,给定一些小球间的重量关系. 在符合重量关系的前提下,先输出编号小的球. 思路:也是一道很简单的拓扑排序,不过要倒着来,注意一下要判重 ...
- IndiaHacks 2016 - Online Edition (Div. 1 + Div. 2) A. Bear and Three Balls 水题
A. Bear and Three Balls 题目连接: http://www.codeforces.com/contest/653/problem/A Description Limak is a ...
- HDU 4611 Balls Rearrangement 数学
Balls Rearrangement 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=4611 Description Bob has N balls ...
- SGU 183. Painting the balls( dp )
dp..dp(i, j)表示画两个点为i-j, i的最优答案. dp(i, j) = min{ dp(i-j, k) } + cost[i] (1≤k≤M-j) 令f(i, j) = min{dp(i ...
- CF755G PolandBall and Many Other Balls(多项式/倍增fft)
CF755G PolandBall and Many Other Balls 倍增fft模版题 这种问题关键在于两个状态转移,一个是单点加1,还有一个是乘2,这样我们就相当于拥有了二进制下的左移操作和 ...
最新文章
- springcloud 子项目怎么导入_Spring Cloud基础面试题大集合
- eeglab教程系列(14)-Working with ICA Components
- Local Response Normalization作用——对局部神经元的活动创建竞争机制,使得其中响应比较大的值变得相对更大,并抑制其他反馈较小的神经元,增强了模型的泛化能力...
- 如何命令行远程连接centos_如何使用windows远程控制centOS桌面
- 得到IOleInPlaceActiveObject接口,IOleInPlaceActiveObject::TranslateAccelerator(msg);
- servlet ---- 请求对象
- bzoj 2707: [SDOI2012]走迷宫(Trajan+高斯消元+Dp)
- 问题-关于 in []使用过程中报错 Constant expression violates subrange bounds
- 计算机犀牛建人体模型步骤,Clayoo加Rhino如何建模卡通人物2
- matlab2010b无法启动,MATLAB2010b simulink 启动警告问题
- java——LinkedList
- 进不去系统rpc服务器不可用,rpc服务器不可用_详细解决方法,彻底修复
- Android之BLE(低功耗)蓝牙开发
- Excel如何简单快速的建立二级下拉菜单?
- 如何查看mysql的gtid_mode_配置MHA开启主从同步的时候会提示从库gtid_mode为ON的状态...
- 图论专题1(网络流)
- pandas, dataframe获取最后一行的三种方法
- 杭电多校-Loop-(不确定性贪心+线段树)
- 企业防病毒体系建设的探讨
- 新手男司机准备开车,搞基的小伙伴们跟上不要掉队
热门文章
- 利用OpenCV的库函数Sobel和Scharr作图像的边缘检测
- c++心得之struct和class(结构体和类)
- java kafka api_kafka java API的使用
- Tomcat User 配置
- client-go入门之3:解析 yaml 文件并创建 k8s 资源对象
- Docker无法修改devicemapper(四)
- idea spring 中没有标识_Spring 优雅的国际化实现
- git如何添加远程主机_Git远程操作详解
- sqlplus command
- 陈一舟:雷军马云在通讯领域也做不过马化腾 为何我就该挨骂