POJ 3687 Labeling Balls
题目链接:https://vjudge.net/problem/POJ-3687
题目大意
分析
代码如下
1 #include <cmath> 2 #include <ctime> 3 #include <iostream> 4 #include <string> 5 #include <vector> 6 #include <cstdio> 7 #include <cstdlib> 8 #include <cstring> 9 #include <queue> 10 #include <map> 11 #include <set> 12 #include <algorithm> 13 #include <cctype> 14 #include <stack> 15 #include <deque> 16 #include <list> 17 #include <sstream> 18 #include <cassert> 19 using namespace std; 20 21 #define INIT() ios::sync_with_stdio(false);cin.tie(0);cout.tie(0); 22 #define Rep(i,n) for (int i = 0; i < (n); ++i) 23 #define For(i,s,t) for (int i = (s); i <= (t); ++i) 24 #define rFor(i,t,s) for (int i = (t); i >= (s); --i) 25 #define ForLL(i, s, t) for (LL i = LL(s); i <= LL(t); ++i) 26 #define rForLL(i, t, s) for (LL i = LL(t); i >= LL(s); --i) 27 #define foreach(i,c) for (__typeof(c.begin()) i = c.begin(); i != c.end(); ++i) 28 #define rforeach(i,c) for (__typeof(c.rbegin()) i = c.rbegin(); i != c.rend(); ++i) 29 30 #define pr(x) cout << #x << " = " << x << " " 31 #define prln(x) cout << #x << " = " << x << endl 32 33 #define LOWBIT(x) ((x)&(-x)) 34 35 #define ALL(x) x.begin(),x.end() 36 #define INS(x) inserter(x,x.begin()) 37 #define UNIQUE(x) x.erase(unique(x.begin(), x.end()), x.end()) 38 #define REMOVE(x, c) x.erase(remove(x.begin(), x.end(), c), x.end()); // 删去 x 中所有 c 39 #define TOLOWER(x) transform(x.begin(), x.end(), x.begin(),::tolower); 40 #define TOUPPER(x) transform(x.begin(), x.end(), x.begin(),::toupper); 41 42 #define ms0(a) memset(a,0,sizeof(a)) 43 #define msI(a) memset(a,0x3f,sizeof(a)) 44 #define msM(a) memset(a,-1,sizeof(a)) 45 46 #define MP make_pair 47 #define PB push_back 48 #define ft first 49 #define sd second 50 51 template<typename T1, typename T2> 52 istream &operator>>(istream &in, pair<T1, T2> &p) { 53 in >> p.first >> p.second; 54 return in; 55 } 56 57 template<typename T> 58 istream &operator>>(istream &in, vector<T> &v) { 59 for (auto &x: v) 60 in >> x; 61 return in; 62 } 63 64 template<typename T1, typename T2> 65 ostream &operator<<(ostream &out, const std::pair<T1, T2> &p) { 66 out << "[" << p.first << ", " << p.second << "]" << "\n"; 67 return out; 68 } 69 70 inline int gc(){ 71 static const int BUF = 1e7; 72 static char buf[BUF], *bg = buf + BUF, *ed = bg; 73 74 if(bg == ed) fread(bg = buf, 1, BUF, stdin); 75 return *bg++; 76 } 77 78 inline int ri(){ 79 int x = 0, f = 1, c = gc(); 80 for(; c<48||c>57; f = c=='-'?-1:f, c=gc()); 81 for(; c>47&&c<58; x = x*10 + c - 48, c=gc()); 82 return x*f; 83 } 84 85 template<class T> 86 inline string toString(T x) { 87 ostringstream sout; 88 sout << x; 89 return sout.str(); 90 } 91 92 inline int toInt(string s) { 93 int v; 94 istringstream sin(s); 95 sin >> v; 96 return v; 97 } 98 99 //min <= aim <= max 100 template<typename T> 101 inline bool BETWEEN(const T aim, const T min, const T max) { 102 return min <= aim && aim <= max; 103 } 104 105 typedef long long LL; 106 typedef unsigned long long uLL; 107 typedef pair< double, double > PDD; 108 typedef pair< int, int > PII; 109 typedef pair< int, PII > PIPII; 110 typedef pair< string, int > PSI; 111 typedef pair< int, PSI > PIPSI; 112 typedef set< int > SI; 113 typedef set< PII > SPII; 114 typedef vector< int > VI; 115 typedef vector< char > VC; 116 typedef vector< double > VD; 117 typedef vector< VI > VVI; 118 typedef vector< SI > VSI; 119 typedef vector< PII > VPII; 120 typedef map< int, int > MII; 121 typedef map< LL, int > MLLI; 122 typedef map< int, string > MIS; 123 typedef map< int, PII > MIPII; 124 typedef map< PII, int > MPIII; 125 typedef map< string, int > MSI; 126 typedef map< char, int > MCI; 127 typedef map< string, string > MSS; 128 typedef map< PII, string > MPIIS; 129 typedef map< PII, PII > MPIIPII; 130 typedef multimap< int, int > MMII; 131 typedef multimap< string, int > MMSI; 132 //typedef unordered_map< int, int > uMII; 133 typedef pair< LL, LL > PLL; 134 typedef vector< LL > VL; 135 typedef vector< VL > VVL; 136 typedef priority_queue< int > PQIMax; 137 typedef priority_queue< int, VI, greater< int > > PQIMin; 138 const double EPS = 1e-8; 139 const LL inf = 0x3fffffff; 140 const LL infLL = 0x3fffffffffffffffLL; 141 const LL mod = 1e9 + 7; 142 const int maxN = 2e2 + 7; 143 const LL ONE = 1; 144 const LL evenBits = 0xaaaaaaaaaaaaaaaa; 145 const LL oddBits = 0x5555555555555555; 146 147 struct Edge{ 148 int from, to; 149 }; 150 151 istream& operator>> (istream& in, Edge &x) { 152 in >> x.from >> x.to; 153 return in; 154 } 155 156 struct Vertex{ 157 int in; 158 VI next; 159 160 void clear() { 161 in = 0; 162 next.clear(); 163 } 164 }; 165 166 int N, M, T; 167 Vertex V[maxN]; 168 vector< Edge > E; 169 int topo[maxN], cnt; // 存拓扑序列 170 171 void addEdge(Edge &x) { 172 V[x.from].next.PB(E.size()); 173 ++V[x.to].in; 174 E.PB(x); 175 } 176 177 bool TopoSort() { 178 PQIMax Q; 179 180 For(i, 1, N) if(!V[i].in) Q.push(i); 181 182 while(!Q.empty()) { 183 int tmp = Q.top(); Q.pop(); 184 185 topo[tmp] = cnt--; 186 Rep(i, V[tmp].next.size()) { 187 Edge &e = E[V[tmp].next[i]]; 188 if(!--V[e.to].in) Q.push(e.to); 189 } 190 } 191 return cnt == 0; 192 } 193 194 void init() { 195 For(i, 1, N) V[i].clear(); 196 E.clear(); 197 cnt = N; 198 } 199 200 int main(){ 201 //freopen("MyOutput.txt","w",stdout); 202 //freopen("input.txt","r",stdin); 203 INIT(); 204 cin >> T; 205 while(T--) { 206 cin >> N >> M; 207 init(); 208 209 For(i, 1, M) { 210 Edge t; 211 cin >> t.to >> t.from; // 存反向拓扑图 212 addEdge(t); 213 } 214 215 if(TopoSort()) For(i, 1, N) cout << topo[i] << " \n"[i == N]; 216 else cout << "-1\n"; 217 } 218 return 0; 219 }
View Code
转载于:https://www.cnblogs.com/zaq19970105/p/11311284.html
POJ 3687 Labeling Balls相关推荐
- POJ 3687 Labeling Balls(拓扑序列)
Labeling Balls 大意:n个重量分别为1-n的小球,给定一些小球间的重量关系. 在符合重量关系的前提下,先输出编号小的球. 思路:也是一道很简单的拓扑排序,不过要倒着来,注意一下要判重 ...
- [POJ] 3687 Labeling Balls(拓扑排序)
题目地址:http://poj.org/problem?id=3687 反向建边,即重的球指向轻的球,注意重边,然后拓扑排序.从n-->1循环,即每次从入度为0的球里面选编号大的存(包含输入的逻 ...
- POJ 3687 Labeling Balls【拓扑排序】
题意: 有 n 个球,想要对他们进行编号1..n,知道了一些求的轻重的关系,要求轻的球必须排在重的球之前,如果存在没有冲突的排序方法的话,输出字典序最小的排序方式. 分析: 没考虑重边,WA了N次!! ...
- 拓扑排序 POJ - 3687反向图跑字典序(重边)
题目链接:http://poj.org/problem?id=3687 题目大意: 给定N个球,这些球的编号分别是1-N中的某个数字,它们的重量也分别是1-N中的某个数字,任意两个球的编号和重量不相等 ...
- poj 3687(拓扑排序)
http://poj.org/problem?id=3687 题意:有一些球他们都有各自的重量,而且每个球的重量都不相同,现在,要给这些球贴标签.如果这些球没有限定条件说是哪个比哪个轻的话,那么默认的 ...
- POJ 3687 拓扑排序
一个比较慢的算法. 反向拓扑,从大到小枚举. #include <cstdio> #include <cstring> using namespace std; int cas ...
- poj pku图论、网络流入门题总结、汇总
poj pku图论.网络流入门题总结.汇总 分类: acm图论 2010-08-25 18:49 243人阅读 评论(0) 收藏 举报 网络算法networkgraphconstructioninte ...
- NOIP 好题推荐(DP+搜索+图论)POJ ZOJ
NOIP好题推荐(DP+搜索+图论)POJ ZOJ 1370 Gossiping (数论->模线性方程有无解的判断)+(图论->DFS) 1090 Chain ->格雷码和二进制码 ...
- POJ 图论分类 + DP(较全 自己又加了点)
DP -----------动态规划 状态压缩DP 2411 (棋盘规模较大)状态压缩DP+DFS+滚动数组 2664 (棋盘规模较小)直接递推即可(DP) 2506 (棋盘规模较小)直接递推即可(D ...
最新文章
- Spring编程式和声明式事务实例讲解
- Linux - 系统资源
- Hi3516DV300 U-boot移植应用开发指南(1)
- linux桌面服务器系统下载,Ubuntu 14.10 中文桌面版/服务器正式版下载
- 专访腾讯产品总监邬沛君:TStack斩获OSCAR技术创新奖的背后
- java流行的测试框架调研+市面上书籍调研
- SAP 电商云 Spartacus 5_0.md 迁移文档的编写格式
- java grpc 客户端处理 go 服务端多返回值_grpc基础实践(二)
- Mr.J--C语言头函数的建立(附严薇敏《数据结构》线性表代码)
- [转]闲话操作系统1
- Altium_Designer的使用
- java环境变量配置和android sdk环境变量配置
- MySQL水平分区表初体验总结
- 好用的项目管理工具,选择Tracup的N种理由
- 个人项目-仿微信小打卡小程序
- 两轮差速移动机器人从A点移动到B点的C++语言代码
- ‘\0’ ,‘0’, “0” ,0之间的区别
- 创建Windows Phone内核模式驱动
- WebStorm 2016 最新安装指南 破解 汉化 字体设置
- cad输入法自动切换_银河麒麟操作系统用户教程(八):如何设置输入法?