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 ...
随机推荐
- tcp_wraper&xinetd 和telnet
一.xinetd简介 1.什么是xinetd xinetd:eXtended InterNET Daemon 扩展的互联网守护程序 xinetd是新一代的网络守护进程服务程序,又叫超级守护进程, ...
- ie中兼容性问题
由于项目要要兼容到ie8原本没有问题的代码一但用ie8打开js的报错找不到对象就都来了,其实总结起来就是ie越老的版本就越多方法名识别不到,那就少什么方法添加什么,比如说我的项目就要引入<scr ...
- HTML语义化是什么?为什么要语义化?
HTML语义化HTML的语义化总结为: 用最恰当的标签来标记内容. 该如何理解呢?比如需要加入一个标题,这个标题的字体比正文的要大写,还要加粗.能够实现这种效果的方法有很多,比如用CSS样式进行渲染. ...
- HTML5 烟花系统
需求:在一个虚拟烟花球中,在不同的部位填充不同颜色.质地的烟花火药:观看试放效果.最好能结合点物理学定律(以便展现火药粉被爆炸扩散到一定程度再爆炸的效果) 这是这学期的一个作业,感觉挺无聊的,纯粹是老 ...
- cmd ora-12560协议适配器错误
这个问题我解决了很久,其实问题的关键在于我在本机安装了一个Oracle11g数据库以及安装了一个Oracle Client,导致在使用sqlplus / as sysdba链接时出现报协议适配器的错误 ...
- HearthBuddy 突袭 rush
https://hearthstone.gamepedia.com/Rush Rush is an ability allowing a minion to attack other minions ...
- leetcode-hard-ListNode-Copy List with Random Pointer-NO
mycode 报错:Node with val 1 was not copied but a reference to the original one. 其实我并没有弄懂对于ListNode而言咋样 ...
- [MyBatis]向MySql数据库插入一千万条数据 批量插入用时6分 之前时隐时现的异常不见了
本例代码下载:https://files.cnblogs.com/files/xiandedanteng/InsertMillionComparison20191012.rar 这次实验的环境仍然和上 ...
- 性能测试 | Web端性能测试
这篇文章想跟大家分享关于Web性能测试的一些知识点.在分享之前,我想先跟大家分享一组数据,关于为什么要做性能测试?具体如下: 71%用户希望在手机上打开网页能跟电脑一样快: 5秒钟被认为是用户能忍受的 ...
- Kbengine游戏引擎-【1】kbengine安装
本文主要介绍如何在Linux上安装 官网环境要求:Centos >= 5.x, Debian >= 5.x GCC版本: >= 4.4.x 官网链接 本文的安装环境介绍:Centos ...