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 ...
随机推荐
- Maximum GCD(UVA 11827)
Problem:Given the N integers, you have to find the maximum GCD (greatest common divisor) of every po ...
- poj1275
Cashier Employment POJ - 1275 A supermarket in Tehran is open 24 hours a day every day and needs a n ...
- 利用简单的有限状态机(FSM)来实现一个简单的LED流水灯
有限状态机,(英语:Finite-state machine, FSM),又称有限状态自动机,简称状态机,是表示有限个状态以及在这些状态之间的转移和动作等行为的数学模型. 有限状态机是指输出取决于过去 ...
- lua.c:82:10: fatal error: readline/readline.h: No such file or directory #include <readline/readline.h>
make linuxcd src && make linuxmake[1]: Entering directory `/root/lua/lua-5.3.2/src'make all ...
- 1 - ImageNet Classification with Deep Convolutional Neural Network (阅读翻译)
ImageNet Classification with Deep Convolutional Neural Network 利用深度卷积神经网络进行ImageNet分类 Abstract We tr ...
- LINUX 字体查看 字体更改mkfontdir
Linux下字体查看: #fc-list :lang=zh 字体更改: 首先找到相应的字体库:simsun.ttf 宋体 #mkdir -p /usr/share/fonts/truetype //创 ...
- tensorflow feeddict问题unhashable type: 'numpy.ndarray'
https://stackoverflow.com/a/49134587 求argmax.加newaxis.转变data类型时尽量用tf自带的函数: tf.argmax.[tf.newaxis, :] ...
- 【软件工程】Beta版本演示
团队信息 队名:女生都队 组长博客: 博客链接 成员 学号 史恩泽(组长) 031702122 施金海 031702121 阮君曦 031702116 陈银山 031702137 李季城 031702 ...
- 提高组刷题营 DAY 2
1.滞空(jump/1s/64M) #include<bits/stdc++.h> using namespace std; typedef long long LL; ; inline ...
- 在编译内核之前到底应该使用make mrproper,make distclean,make clean中的哪个命令呢?
1. 先找到描述这三个命令的相关信息 在内核目录下使用make help命令可以获取相关信息,信息如下: Cleaning targets: clean - Remove most generated ...