题目链接:http://lightoj.com/volume_showproblem.php?problem=1003
Time Limit: 2 second(s) | Memory Limit: 32 MB |
One of my friends is always drunk. So, sometimes I get a bit confused whether he is drunk or not. So, one day I was talking to him, about his drinks! He began to describe his way of drinking. So, let me share his ideas a bit. I am expressing in my words.
There are many kinds of drinks, which he used to take. But there are some rules; there are some drinks that have some pre requisites. Suppose if you want to take wine, you should have taken soda, water before it. That's why to get real drunk is not that easy.
Now given the name of some drinks! And the prerequisites of the drinks, you have to say that whether it's possible to get drunk or not. To get drunk, a person should take all the drinks.
Input
Input starts with an integer T (≤ 50), denoting the number of test cases.
Each case starts with an integer m (1 ≤ m ≤ 10000). Each of the next m lines will contain two names each in the format a b, denoting that you must have a before havingb. The names will contain at most 10 characters with no blanks.
Output
For each case, print the case number and 'Yes' or 'No', depending on whether it's possible to get drunk or not.
Sample Input | Output for Sample Input |
2 2 soda wine water wine 3 soda wine water wine wine water | Case 1: Yes Case 2: No |
题目大意:给你几个字符串,每一行两个字符串,第一个优先级比较高,问是否能正常排序
解析:拓扑排序,看是否有环, 有环 NO, 反之YES
代码1:
#include<iostream>
#include<algorithm>
#include<map>
#include<stack>
#include<set>
#include<string>
#include<cstdio>
#include<cstring>
#include<cctype>
#include<cmath>
#define N 10009
using namespace std;
const int inf = 0x3f3f3f3f;
const int mod = 1e9 + 7;
const double eps = 1e-8;
const double pi = acos(-1.0);
typedef long long LL;
map<string, int> mp;
int head[N], c[N], n, num;
typedef struct
{
int n, to;
} T;
T t[N];
void add(int u, int v)
{
t[num].to = v;
t[num].n = head[u];
head[u] = num++;
}
int dfs(int u)
{
c[u] = -1;
for(int i = head[u]; ~i; i = t[i].n)
{
if(c[t[i].to] < 0) return 0;
else if(!c[t[i].to] && !dfs(t[i].to)) return 0;
}
c[u] = 1;
return 1;
}
int toposort()
{
memset(c, 0, sizeof(c));
for(int i = 0; i <= n; i++)
{
if(!c[t[i].to] && !dfs(t[i].to)) return 0;
}
return 1;
}
int main()
{
//freopen("out.txt", "w", stdout);
int t, f, cnt = 0;
char s[17], ss[17];
cin >> t;
while(t--)
{
memset(head, -1, sizeof(head));
scanf("%d", &n);
f = num = 0;
mp.clear();
for(int i = 1; i <= n; i++)
{
scanf(" %s %s", s, ss);
if(!mp.count(s)) mp[s] = ++f;
if(!mp.count(ss)) mp[ss] = ++f;
int u, v;
u = mp[s];
v = mp[ss];
add(u, v);
}
if(toposort()) printf("Case %d: Yes\n", ++cnt);
else printf("Case %d: No\n", ++cnt);
}
}
代码2:
#include<iostream>
#include<algorithm>
#include<map>
#include<stack>
#include<queue>
#include<set>
#include<string>
#include<cstdio>
#include<cstring>
#include<cctype>
#include<cmath>
#define N 10009
using namespace std;
const int inf = 0x3f3f3f3f;
const int mod = 1e9 + 7;
const double eps = 1e-8;
const double pi = acos(-1.0);
typedef long long LL;
struct node
{
int n, to;
}t[N];
int f, vis[N], head[N], ans;
map<string, int> mp;
void add(int u, int v)
{
t[ans].to = v;
t[ans].n = head[u];
head[u] = ans++;
}
int toposort()
{
int i;
queue<int> q;
for(i = 1; i <= f; i++)
if(!vis[i]) q.push(i);
while(!q.empty())
{
int u = q.front(); q.pop(); f--;
for(i = head[u]; ~i; i = t[i].n)
{
int v = t[i].to; vis[v]--;
if(!vis[v]) q.push(v);
}
}
if(f) return 0;
return 1;
}
int main()
{
int t, i, n, cnt = 0;
char s[17], ss[17];
cin >> t;
while(t--)
{
memset(vis, 0, sizeof(vis));
memset(head, -1, sizeof(head));
scanf("%d", &n);
f = ans = 0;
mp.clear();
for(i = 1; i <= n; i++)
{
int u, v;
scanf(" %s %s", s, ss);
if(!mp.count(s)) mp[s] = ++f;
if(!mp.count(ss)) mp[ss] = ++f;
u = mp[s]; v = mp[ss];
add(u, v);
vis[v]++;
}
if(toposort()) printf("Case %d: Yes\n", ++cnt);
else printf("Case %d: No\n", ++cnt);
}
return 0;
}