poj3687(Labeling Balls)

题目大意:

给你N个球的重量比较,输出1->N位置球的重量(记住是球的重量,不是按照球重量大小输出序号,球的重量大小也是1->n)。如果无法判断输出-1.

解题思路:

拓扑排序,记录较小的编号球的入度,依次n--赋值入度为零的编号球。

代码:

  1 #include <algorithm>
  2 #include <iostream>
  3 #include <sstream>
  4 #include <cstdlib>
  5 #include <cstring>
  6 #include <cstdio>
  7 #include <string>
  8 #include <bitset>
  9 #include <vector>
 10 #include <queue>
 11 #include <stack>
 12 #include <cmath>
 13 #include <list>
 14 //#include <map>
 15 #include <set>
 16 using namespace std;
 17 /***************************************/
 18 #define ll long long
 19 #define int64 __int64
 20 /***************************************/
 21 const int INF = 0x7f7f7f7f;
 22 const double eps = 1e-8;
 23 const double PIE=acos(-1.0);
 24 const int d1x[]= {0,-1,0,1};
 25 const int d1y[]= {-1,0,1,0};
 26 const int d2x[]= {0,-1,0,1};
 27 const int d2y[]= {1,0,-1,0};
 28 const int fx[]= {-1,-1,-1,0,0,1,1,1};
 29 const int fy[]= {-1,0,1,-1,1,-1,0,1};
 30 /***************************************/
 31 void openfile()
 32 {
 33     freopen("data.in","rb",stdin);
 34     freopen("data.out","wb",stdout);
 35 }
 36 /**********************华丽丽的分割线,以上为模板部分*****************/
 37 int cnt1[205],cnt2[205];
 38 int topol[205];
 39 int adj[205][205],adi[205][205];
 40 int dui[205],re[205];
 41 int topo(int n)
 42 {
 43     int i,j,k,d=0,z=-1,x,m;
 44     m=n;
 45     for(i=1; i<=n; i++)
 46         for(j=n; j>=1; j--)
 47         {
 48             if(cnt2[j]==0)
 49             {
 50                 cnt2[j]=-1;
 51                 re[j]=m--;
 52                 topol[d++]=j;
 53                 for(k=1; k<=n; k++)
 54                     if(adi[k][j])
 55                     {
 56                         adi[k][j]=0;
 57                         cnt2[k]--;
 58                     }
 59                 break;
 60             }
 61         }
 62     if (d!=n)
 63         return 0;
 64     for(j=1; j<=n; j++)
 65     {
 66         if(j!=n)
 67             printf("%d ",re[j]);
 68         else
 69             printf("%d\n",re[j]);
 70     }
 71     return 1;
 72 }
 73 int main()
 74 {
 75     int cas;
 76     scanf("%d",&cas);
 77     while(cas--)
 78     {
 79         int i,a,b;
 80         int m,n,c=1;
 81         scanf("%d%d",&m,&n);
 82         memset(topol,0,sizeof(topol));
 83         memset(cnt1,0,sizeof(cnt1));
 84         memset(cnt2,0,sizeof(cnt2));
 85         memset(adj,0,sizeof(adj));
 86         memset(adi,0,sizeof(adi));
 87         while(n--)
 88         {
 89             scanf("%d%d",&a,&b);
 90             if (!adj[a][b])
 91             {
 92                 adj[a][b]=1;
 93                 adi[a][b]=1;
 94                 cnt1[b]++;
 95                 cnt2[a]++;
 96             }
 97         }
 98         c=topo(m);
 99         if (c==0)
100             printf("-1\n");
101     }
102     return 0;
103 }

poj3687(Labeling Balls),布布扣,bubuko.com

时间: 2024-10-27 05:36:32

poj3687(Labeling Balls)的相关文章

Uva 679(Dropping Balls)小球下落

题目描述有一棵二叉树,最大深度为D,且所有叶子的深度都相同.所有结点从上到下从左到右编号为1,2,3,…,2D-1.在结点1处放一个小球,它会往下落.每个内结点上都有一个开关,初始全部关闭,当每次有小球落到一个开关上时,它的状态都会改变.当小球到达一个内结点时,如果该结点上的开关关闭,则往左走,否则往右走,直到走到叶子结点,如图所示. 一些小球从结点1处依次开始下落,最后一个小球将会落到哪里呢?输入叶子深度D和小球个数I,输出第I个小球最后所在的叶子编号.假设I不超过整棵树的叶子个数.D≤20.

POJ3687 Labeling Balls (拓扑排序)经典

Labeling Balls Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11469   Accepted: 3295 Description Windy has N balls of distinct weights from 1 unit to N units. Now he tries to label them with 1 to N in such a way that: No two balls share

Labeling Balls(拓扑排序wa)

Labeling Balls Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 12466   Accepted: 3576 Description Windy has N balls of distinct weights from 1 unit to N units. Now he tries to label them with 1 to N in such a way that: No two balls share

POJ 3687:Labeling Balls(优先队列+拓扑排序)

id=3687">Labeling Balls Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10178 Accepted: 2815 Description Windy has N balls of distinct weights from 1 unit toN units. Now he tries to label them with 1 toN in such a way that: No two balls

[ACM] POJ 3687 Labeling Balls (拓扑排序,反向生成端)

Labeling Balls Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10161   Accepted: 2810 Description Windy has N balls of distinct weights from 1 unit to N units. Now he tries to label them with 1 toN in such a way that: No two balls share

[ACM] POJ 3687 Labeling Balls (拓扑排序,逆向建边)

Labeling Balls Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10161   Accepted: 2810 Description Windy has N balls of distinct weights from 1 unit to N units. Now he tries to label them with 1 to N in such a way that: No two balls share

Codeforces A. Kyoya and Colored Balls(分步组合)

题目描述: Kyoya and Colored Balls time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output Kyoya Ootori has a bag with n colored balls that are colored with k different colors. The colors are labeled f

Dropping Balls(小球下落)

紫书P148,例题6-6 Sample Input 4 2 3 4 10 1 2 2 8 128 Sample Output 12 7 512 3 255 这应该不仅仅是一棵完全二叉树,题目中说保证所有叶子节点的深度都相同,所以这是一颗满二叉树. 这里要弄清满二叉树的一些概念和性质,首先,对于一颗满二叉树来说,他每一层的节点数都达到最大,那么对于一个K层的满二叉树来说,他的节点数有(2^k)-1个 而且研究满二叉树和完全二叉树的一个好处在于他可以实现顺序存储,如图中的可以表示为 值   :1 2

图像分割之(二)Graph Cut(图割)

[email protected] http://blog.csdn.net/zouxy09 上一文对主要的分割方法做了一个概述.那下面我们对其中几个比较感兴趣的算法做个学习.下面主要是Graph Cut,下一个博文我们再学习下Grab Cut,两者都是基于图论的分割方法.另外OpenCV实现了Grab Cut,具体的源码解读见博文更新.接触时间有限,若有错误,还望各位前辈指正,谢谢. Graph cuts是一种十分有用和流行的能量优化算法,在计算机视觉领域普遍应用于前背景分割(Image se