poj 3687 Labeling Balls 【拓扑排序】
2015-08-25 20:49
246 查看
题目链接:http://poj.org/problem?id=3687
注意重边
代码:
注意重边
代码:
[code]#include <stdio.h> #include <ctime> #include <math.h> #include <limits.h> #include <complex> #include <string> #include <functional> #include <iterator> #include <algorithm> #include <vector> #include <stack> #include <queue> #include <set> #include <map> #include <list> #include <bitset> #include <sstream> #include <iomanip> #include <fstream> #include <iostream> #include <ctime> #include <cmath> #include <cstring> #include <cstdio> #include <time.h> #include <ctype.h> #include <string.h> #include <assert.h> using namespace std; int in[210]; int is[210][210]; int ans[210]; int n, m; int main() { int t; int n, m; scanf("%d", &t); while (t--) { memset(in, 0, sizeof(in)); memset(is, 0, sizeof(is)); memset(ans, 0, sizeof(ans)); scanf("%d%d", &n, &m); int ok = 1; int a, b; while (m--) { scanf("%d%d", &a, &b); if (a == b) ok = 0; if (is[a][b] == 1) ok = 0; if (is[b][a] == 0) in[a]++; is[b][a] = 1; } int num = 1; int cnt = 0; if (!ok) puts("-1"); else { for (int k = n;k >= 1 ;k--) { int i = n; while (in[i] != 0 && i>=1) i--; in[i] = -1;ans[i] = k; if (i >= 1) cnt++; for (int j = 1;j <= n;j++) { if (is[i][j]) in[j]--; } } if (cnt != n) puts("-1"); else { for (int i = 1;i < n;i++) printf("%d ", ans[i]); printf("%d\n", ans ); } } } return 0; }
相关文章推荐
- [solr] - solr5.2.1环境搭建 - 使用solr自带的jetty服务器
- 剑指offer 算法 (时间空间效率的平衡)
- 1712: 编程语言 (规律题)
- Appium源码分析(六)-find(上)
- 程序内存分布
- 【C++】单链表的实现
- How can i specify the resource to get from R.res.drawable dynamically?
- UVA699落叶,二叉树递归左右偏移
- SQL Join —— 左联接、右联接、内联接等
- 用两个不规则容器倒出指定容量的水
- 第 16 章 工具栏
- Android中asset文件夹和raw文件夹区别
- [Android]代码实现ColorStateList及StateListDrawable
- request.META里包含了哪些数据?
- hdoj 1286 找新朋友
- String.intern()-字符串常量池
- hdu 2475 Box splay, lct
- Android学习 之 ColorStateList按钮文字变色
- python的matplotlib画图时无法显示中文的解决方法
- 单例模式-Singleton