POJ - 3687 Labeling Balls (拓扑)
题意
此处有n盏灯,编号为1~n,每盏灯的亮度都是唯一的,且在1~n范围之间,现已知m对灯之间的关系:a b ,说明灯a的亮度比灯b小,求出每盏灯的亮度,要求字典序最小(编号小的灯亮度尽量小),使之满足m对关系,如果不存在,输出-1
解题思路
每对灯的关系:a b ,说明灯a的亮度比灯b小,同时也说明a的完成时间比b早(AOV网中的概念),这种关系可以用拓扑排序很好地处理,而由于这个题目要求字典序最小,为此将队列改为优先队列,使得编号大的灯先出队,并为其赋予较大的拓扑序,这样便可以使得字典序最小了,最后我们将所有点都赋予了拓扑序,随后和m对关系一一比较,判断是否满足要求,满足则输出所得拓扑序,否则输出-1
代码区
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<queue>
#include<string>
#include<fstream>
#include<vector>
#include<stack>
#include <map>
#include <iomanip> #define bug cout << "**********" << endl
#define show(x, y) cout<<"["<<x<<","<<y<<"] "
#define LOCAL = 1;
using namespace std;
typedef long long ll;
const ll inf = 1e18 + ;
const ll mod = 1e9 + ;
const int Max = 1e6 + ;
const int Max2 = 3e2 + ; int n, m;
vector<int> edge[Max2]; //邻接表建图
pair<int, int> way[Max]; //存储m对关系
map<pair<int,int>,bool>mp; //用于判断重边
int in_d[Max2]; //记录入度
int id[Max2], now; //记录拓扑序 void init()
{
for (int i = ; i < Max2; i++)
edge[i].clear();
memset(id, , sizeof(id));
now = n;
memset(in_d,,sizeof(in_d));
mp.clear();
} void topoSort()
{
priority_queue<int> q;
for (int i = n; i >= ; i--)
{
if (in_d[i] == )
q.push(i);
}
while (!q.empty())
{
int u = q.top();
q.pop();
id[u] = now--;
for(int i = ;i < (int)edge[u].size() ;i ++)
{
int v = edge[u][i];
if(id[v]) continue;
in_d[v]--;
if(in_d[v] == )
{
q.push(v);
}
}
}
} int main()
{
#ifdef LOCAL
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
#endif
int T;
scanf("%d", &T);
while (T--)
{
scanf("%d%d", &n, &m);
init();
bool ok = true;
for (int i = , u, v; i <= m; i++)
{
scanf("%d%d", &u, &v);
if(u == v)
{
ok = false;
continue;
}
if(!mp[make_pair(u,v)])
{
edge[v].push_back(u);
in_d[u]++;
}
way[i] = make_pair(u, v);
mp[way[i]] = true;
}
topoSort(); for (int i = n; i >= ; i--)
{
if (id[i] == )
{
id[i] = now--;
}
}
for (int i = ; i <= m; i++)
{
if (id[way[i].first] > id[way[i].second])
{
ok = false;
break;
}
}
if (!ok)
{
printf("-1\n");
}
else
{
for (int i = ; i <= n; i++)
{
printf("%d%c", id[i], i != n ? ' ' : '\n');
}
}
}
return ;
}
POJ - 3687 Labeling Balls (拓扑)的更多相关文章
- [ACM] POJ 3687 Labeling Balls (拓扑排序,反向生成端)
Labeling Balls Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10161 Accepted: 2810 D ...
- poj 3687 Labeling Balls(拓扑排序)
题目:http://poj.org/problem?id=3687题意:n个重量为1~n的球,给定一些编号间的重量比较关系,现在给每个球编号,在符合条件的前提下使得编号小的球重量小.(先保证1号球最轻 ...
- POJ 3687 Labeling Balls(反向拓扑+贪心思想!!!非常棒的一道题)
Labeling Balls Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 16100 Accepted: 4726 D ...
- poj 3687 Labeling Balls【反向拓扑】
Labeling Balls Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 12246 Accepted: 3508 D ...
- poj——3687 Labeling Balls
Labeling Balls Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 14835 Accepted: 4346 D ...
- POJ 3687 Labeling Balls()
Labeling Balls Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9641 Accepted: 2636 Descri ...
- POJ 3687 Labeling Balls (top 排序)
Labeling Balls Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 15792 Accepted: 4630 D ...
- 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 ...
- POJ 3687 Labeling Balls 逆向建图,拓扑排序
题目链接: http://poj.org/problem?id=3687 要逆向建图,输入的时候要判重边,找入度为0的点的时候要从大到小循环,尽量让编号大的先入栈,输出的时候注意按编号的顺序输出重量, ...
- POJ 3687 Labeling Balls(拓扑排序)题解
Description Windy has N balls of distinct weights from 1 unit to N units. Now he tries to label them ...
随机推荐
- layer提示带文字
直接撸代码: //加载层-风格4 layer.msg('加载中', { icon: 16 ,shade: 0.01 });
- Flask-Response
Flask中的HTTPResponse from flask import Flask,redirect app = Flask(__name__) @app.route("/index&q ...
- Lombok Pojo默认初始值问题
Lombok以注解形式来简化java代码,提高开发效率.比如我们常用的@Builder.@Data.@AllArgsConstructor.@NoArgsConstructor.@ToString等. ...
- Python 寻找文件夹里以特定格式结尾的文件
代码: import os, re, time name = 'linuxday01' flags = True# 文件夹bi_test中的文件列表 print os.listdir('E:\\bi_ ...
- POJ 1837 -- Balance(DP)
POJ 1837 -- Balance 转载:優YoU http://user.qzone.qq.com/289065406/blog/1299341345 提示:动态规划,01背包 初看此题第 ...
- 影响mysql性能的因素
一.服务器硬件. CPU不够快,内存不够多,磁盘IO太慢. 对于计算密集型的应用,CPU越可能去影响系统的性能,此时,CPU和内存将越成为系统的瓶颈. 当热数据大小远远超过系统可用内存大小时,IO资源 ...
- UIGestureRecongnizer 手势拦截 对于特殊需求很有用
手势其实也有代理方法的,通过代理方法可以做到更多关于手势方面的功能 比如在下面的方法中,如果是UIButton的点击就阻止手势的点击事件. // called before touchesBegan: ...
- UML 2.5版本与UML分类概述
UML 2.5版本与UML分类概述 转 http://www.umlstudy.com/uml-25-diagrams.html UML简述 UML图是设计.实现或已经存在的系统模型的部分图形表示(视 ...
- springmvc快速入门(XML版本)
1)springmvc快速入门(传统版) 步一:创建springmvc-day01这么一个web应用 步二:导入springioc,springweb , springmvc相关的jar包 步三:在/ ...
- URL编码和解码
1. 为什么需要编码 当数据不利于处理.存储的时候,就需要对它们进行编码.如对字符进行编码是因为自然语言中的字符不利于计算机处理和存储.对图片信息.视频信息.声音信息进行压缩.优化,将其“格式化”,是 ...