您的位置:首页 > 其它

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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  acm-icpc