Title Link: http://lightoj.com/volume_showproblem.php?problem=1003
1003-drunk
|
PDF (Chinese Version) |
Statistics |
Forum |
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's drunk or not. So, one day I is talking to him, on his drinks! He began to describe his drinking. So, let me share his ideas a bit. I am expressing in my words.
There is many kinds of drinks, which he used to take. But there is some rules; There is some drinks that has some pre requisites. Suppose if you want to take wine, you should has taken soda, water before it. That's why-to get-real drunk is not so easy.
Now given the name of some drinks! And the prerequisites of the drinks, you had 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 is starts with an integer m (1≤m≤10000). Each of the next m lines would contain the names each in the format a B, denoting that you must has A before havingb. The names would contain at a 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 |
Problem Setter:jane ALAM JAN
The main topic: give you a few strings, two strings per line, the first priority is higher, ask whether it can be sorted properly
Parse: Topology sort, see if there is ring, ring NO, vice versa Yes
Code Listing 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);
}
}
Code Listing 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;
}