拓扑排序(好题)hdu3231
来源:互联网 发布:linux虚拟机和双系统 编辑:程序博客网 时间:2024/05/21 15:39
Online JudgeOnline ExerciseOnline TeachingOnline ContestsExercise AuthorF.A.Q
Hand In Hand
Online Acmers
Forum | Discuss
Statistical ChartsBest Coder beta
VIP | STD Contests
Virtual Contests
DIY | Web-DIY beta
Recent Contests
[BestCoder Round #5]冠军的奖励是小米3手机一部
恭喜福州大学杨楠获得[BestCoder Round #4]冠军(iPad Mini一部)
《BestCoder用户手册》下载
Total Submission(s): 974 Accepted Submission(s): 356
Special Judge
Hand In Hand
Online Acmers
Forum | Discuss
Statistical ChartsBest Coder beta
VIP | STD Contests
Virtual Contests
DIY | Web-DIY beta
Recent Contests
[BestCoder Round #5]冠军的奖励是小米3手机一部
恭喜福州大学杨楠获得[BestCoder Round #4]冠军(iPad Mini一部)
《BestCoder用户手册》下载
Box Relations
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 974 Accepted Submission(s): 356
Special Judge
Problem Description
There are n boxes C1, C2, ..., Cn in 3D space. The edges of the boxes are parallel to the x, y or z-axis. We provide some relations of the boxes, and your task is to construct a set of boxes satisfying all these relations.
There are four kinds of relations (1 <= i,j <= n, i is different from j):
There are four kinds of relations (1 <= i,j <= n, i is different from j):
- I i j: The intersection volume of Ci and Cj is positive.
- X i j: The intersection volume is zero, and any point inside Ci has smaller x-coordinate than any point inside Cj.
- Y i j: The intersection volume is zero, and any point inside Ci has smaller y-coordinate than any point inside Cj.
- Z i j: The intersection volume is zero, and any point inside Ci has smaller z-coordinate than any point inside Cj.
Input
There will be at most 30 test cases. Each case begins with a line containing two integers n (1 <= n <= 1,000) and R (0 <= R <= 100,000), the number of boxes and the number of relations. Each of the following R lines describes a relation, written in the format above. The last test case is followed by n=R=0, which should not be processed.
Output
For each test case, print the case number and either the word POSSIBLE or IMPOSSIBLE. If it's possible to construct the set of boxes, the i-th line of the followingn lines contains six integers x1, y1, z1, x2, y2, z2, that means the i-th box is the set of points (x,y,z) satisfying x1 <= x <= x2, y1 <= y <= y2, z1 <= z <= z2. The absolute values of x1, y1, z1, x2, y2, z2 should not exceed 1,000,000.
Print a blank line after the output of each test case.
Print a blank line after the output of each test case.
Sample Input
3 2I 1 2X 2 33 3Z 1 2Z 2 3Z 3 11 00 0
Sample Output
Case 1: POSSIBLE0 0 0 2 2 21 1 1 3 3 38 8 8 9 9 9Case 2: IMPOSSIBLECase 3: POSSIBLE0 0 0 1 1 1
思路:每个盒子有六个面,i表示上表面,i+n表示下表面,然后根据给的关系建图,I表示a的上表面在b的下表面上面,a的下表面在b的上表面下面,x,y,z的操作相同,然后把x,y,z三维分别进行拓扑排序,如果能够进行得话,就说明存在这样的顺序。
#include<iostream>#include<cstdio>#include<cstring>#include<vector>#include<cmath>#include<queue>#include<stack>#include<map>#include<set>#include<algorithm>using namespace std;const int maxn=2010;const int dimension=3;int n,R;vector<int> g[3][maxn];int in[dimension][maxn];void init(){ memset(in,0,sizeof(in)); for(int i=0;i<dimension;i++) for(int j=0;j<=2*n;j++)g[i][j].clear(); for(int i=0;i<dimension;i++) for(int j=1;j<=n;j++) { g[i][j].push_back(j+n); in[i][j+n]++; }}bool topo(int x[],int d){ queue<int> q; for(int i=1;i<=2*n;i++) if(!in[d][i]) q.push(i); int cnt=0; while(!q.empty()) { int b=q.front(); q.pop(); x[b]=cnt++; int len=g[d][b].size(); for(int i=0;i<len;i++) if(--in[d][g[d][b][i]]==0)q.push(g[d][b][i]); } for(int i=1;i<=2*n;i++) if(in[d][i])return false; return true;}void solve(){ int ans[dimension][maxn]; for(int i=0;i<dimension;i++) if(!topo(ans[i],i)){printf("IMPOSSIBLE\n");return;} printf("POSSIBLE\n"); for(int i=1;i<=n;i++) printf("%d %d %d %d %d %d\n",ans[0][i],ans[1][i],ans[2][i],ans[0][i+n],ans[1][i+n],ans[2][i+n]);}int main(){ int cas=1; while(scanf("%d%d",&n,&R)!=EOF,n||R) { init(); while(R--) { char op[5]; int x,y; scanf("%s%d%d",op,&x,&y); if(op[0]=='I') { for(int i=0;i<dimension;i++) { g[i][x].push_back(y+n); in[i][y+n]++; g[i][y].push_back(x+n); in[i][x+n]++; } } else { g[op[0]-'X'][x+n].push_back(y); in[op[0]-'X'][y]++; } } printf("Case %d: ",cas++); solve(); printf("\n"); } return 0;}
0 0
- 拓扑排序(好题)hdu3231
- Hdu3231【拓扑排序】
- 【拓扑排序】【HDU3231】【Box Relations】
- Hdu3231 step5.2.7 Box Relations(拓扑排序)
- HDU3231 Box Relations(拓扑排序)经典
- 分层拓扑排序好题
- poj1094 好一个拓扑排序
- Gym 101503I Just Matrix【思维+拓扑排序】好题~
- 拓扑排序题集
- 拓扑排序题集
- 拓扑排序题集
- 拓扑排序简单题
- 拓扑排序题集
- 【训练题】拓扑排序
- Codeforces 516B Drazil and Tiles【类拓扑排序】好题!
- LightOJ 1251 Forming the Council【2-Sat+逆向拓扑排序输出可行解】好题!
- Hrbust 2048 Thrall’s Dream【强连通+拓扑排序+思维】好题!
- 2017 四川省赛 D.Dynamic Graph【拓扑排序+bitset优化】好题!
- 13,LayoutInflater
- ext+模态窗口+下载
- 计算机操作系统--读写者问题
- 思科linksys-wap54g无线ap
- oracle导入导出exp/imp 及imp-00037错误
- 拓扑排序(好题)hdu3231
- ssh 连接的时候 Host key verification failed.
- Sonatype Nexus 搭建Maven 私服
- ModelAndView详解
- poj 2178 Beauty Contest[最远点对]
- measureChildren的工作原理
- [mysql]mac在命令行使用mysql
- Ubuntu 14.04输入法安装
- HashSet原理