Light 1003 - Drunk (拓扑排序)
2015-05-13 16:46
381 查看
题意
如果要喝一种饮料,需要先喝另一种饮料。给出这种关系,问一个人能不能喝完全部的饮料。
思路
一开始我以为只能选一种饮料喝,然后喝完全部的饮料。WA了几次后才发现题意就是能不能喝完,也就是说会不会出现不能被喝的饮料,也就是成环的饮料。
用拓扑排序去搞。
代码
#include <stack> #include <cstdio> #include <list> #include <cassert> #include <set> #include <fstream> #include <iostream> #include <string> #include <vector> #include <queue> #include <functional> #include <cstring> #include <algorithm> #include <cctype> //#pragma comment(linker, "/STACK:102400000,102400000") #include <string> #include <map> #include <cmath> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/hash_policy.hpp> using namespace std; using namespace __gnu_pbds; #define LL long long #define ULL unsigned long long #define SZ(x) (int)x.size() #define Lowbit(x) ((x) & (-x)) #define MP(a, b) make_pair(a, b) #define MS(arr, num) memset(arr, num, sizeof(arr)) #define PB push_back #define X first #define Y second #define ROP freopen("input.txt", "r", stdin); #define MID(a, b) (a + ((b - a) >> 1)) #define LC rt << 1, l, mid #define RC rt << 1|1, mid + 1, r #define LRT rt << 1 #define RRT rt << 1|1 #define FOR(i, a, b) for (int i=(a); (i) < (b); (i)++) #define FOOR(i, a, b) for (int i = (a); (i)<=(b); (i)++) const double PI = acos(-1.0); const int INF = 0x3f3f3f3f; const double eps = 1e-8; const int MAXN = 2e4+10; const int MOD = 1e9; const int dir[][2] = { {-1, 0}, {1, 0}, {0, -1}, {0, 1} }; const int seed = 131; int cases = 0; typedef pair<int, int> pii; map<string, int> mp; int clk, in[MAXN]; vector<int> G[MAXN]; bool Toposort() { queue<int> Q; int cnt = 0; FOOR(i, 1, clk) if (in[i] == 0) Q.push(i); while (!Q.empty()) { int u = Q.front(); Q.pop(); FOR(i, 0, SZ(G[u])) { int v = G[u][i]; in[v]--; if (in[v] == 0) Q.push(v); } cnt++; } return cnt == clk; } int main() { //ROP; int T; scanf("%d", &T); while (T--) { MS(in, 0); mp.clear(); clk = 0; int n; scanf("%d", &n); FOOR(i, 0, 2*n) G[i].clear(); while (n--) { string a, b; cin >> a >> b; if (!mp.count(a)) mp[a] = ++clk; if (!mp.count(b)) mp[b] = ++clk; G[mp[b]].PB(mp[a]); in[mp[a]]++; } printf("Case %d: %s\n", ++cases, Toposort() ? "Yes" : "No"); } return 0; }
相关文章推荐
- Light oj 1003 - Drunk(拓扑排序, DAG)
- lightoj1003 Drunk 、uva10305 Ordering Tasks (基于dfs的拓扑排序)
- LightOJ 1003 Drunk【有向图的拓扑排序判环】
- LightOJ 1003 Drunk 拓扑排序判有向环
- 1003 - Drunk (拓扑排序)
- Lightoj 1003 - Drunk(拓扑排序)
- LightOJ-1003-Drunk [拓扑排序]
- Lightoj 1003 - Drunk(拓扑排序判断是否有环 Map离散化)
- Drunk(LightOJ-1003)(拓扑排序)(并查集查环)
- LightOJ 1003 - Drunk【拓扑排序】
- LightOJ1003---Drunk(拓扑排序判环)
- LightOJ 1003 Drunk
- Loj 1003–Drunk(拓扑排序)
- LightOJ1003---Drunk(拓扑排序判环)
- lightoj1003 Drunk
- light oj 1003—拓扑排序
- LightOJ - 1003 Drunk
- 1003 - Drunk (from LightOj)
- Lightoj 1003 - Drunk(喝酒问题,拓扑排序环的判断)
- 【lightoj1003 - Drunk】