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 ...
随机推荐
- [Linux]虚拟机无法安装deepin15.9的解决方案
虚拟机deepin15.9无法安装 sda assuming drive cache write through 显示内存不行,重启仍然无法安装 解决方案: 选择全盘安装方式 如果有全屏问题,需安装v ...
- 【SecureCRT】SecureCRT 护眼配色
终端有一个好的配色,不仅能保护自己的眼睛,也能给人一个好心情,本配色方案适合任意一种SSH客户端软件. 设置背景颜色 Options => Sessions options => ...
- python_re模块
正则表达式:http://www.regexlab.com/zh/regref.htm
- mybatis-puls 字段为null时候的更新问题
在mybatis-puls重设置的全局更新策略 为null的字段忽略更新.但是在某些业务需求下面,可能需要某些字段更新为null值.那么改如何设置 1, 在你的实体属性上面单独添加需要更新nu l l ...
- docker: Error response from daemon: OCI runtime create failed: container_linux.go:345: starting container process caused "exec: \"ping\": executable file not found in $PATH": unknown.
docker: Error response from daemon: OCI runtime create failed: container_linux.go:345: starting cont ...
- Raspberry Pi 4B基本设置
目录 一.SSH登录Raspberry Pi 二.开启VNC服务 三.将SD卡分区扩展 四.修改软件源 一.SSH登录Raspberry Pi 完成系统烧录后,就需要登录Raspberry Pi,此时 ...
- 什么是跨平台性?原理是什么?JVM
所谓跨平台性,是指java语言编写的程序,一次编译后,可以在多个系统平台上运行. 实现原理:Java程序是通过java虚拟机在系统平台上运行的,只要该系统可以安装相应的java虚拟机,该系统就可以运行 ...
- 【Redis 设置Redis使用LRU算法】
转自:http://ifeve.com/redis-lru/ 本文将介绍Redis在生产环境中使用的Redis的LRU策略,以及自己动手实现的LRU算法(php) 1.设置Redis使用LRU算法 L ...
- js反混淆
var esprima = require('esprima') var escodegen = require('escodegen') content = "function _0x35 ...
- 解决虚拟机上的tomcat无法被主机访问的问题
在wmware中安装linux后安装好数据库,JDK及tomcat后启动服务,虚拟机中可以访问,但是主机却无法访问,但是同时主机和虚拟机之间可以ping的通. 网上查阅资料后 第一种解决方法是 ...