-
Notifications
You must be signed in to change notification settings - Fork 0
/
Lightoj 1003 - Drunk.cpp
90 lines (88 loc) · 1.88 KB
/
Lightoj 1003 - Drunk.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
//Author - Soumik Roy
//Date & Time - September 02, 2020 2:16 PM
//Problem name - 1003 - Drunk
//Problem url - http://lightoj.com/volume_showproblem.php?problem=1003
//Time limit - 2000 ms
//Memory Limit - 32 MB
#include<bits/stdc++.h>
#define ll long long int
#define pb push_back
#define F first
#define S second
#define mp make_pair
#define MOD 1000000007
#define vi vector<int>
#define vll vector<ll>
#define pll pair<ll,ll>
#define pii pair<int,int>
#define all(p) p.begin(),p.end()
#define mid(s,e) (s+(e-s)/2)
#define eb emplace_back
#define ull unsigned long long
#define bug(x) cout<<" [ "#x<<" = "<<x<<" ]"<<endl;
#define KAMEHAMEHA ios_base::sync_with_stdio(0);
#define RASENGAN ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
using namespace std;
int dir[]={0,1,0,-1,1,0,-1,0};
int m;
unordered_map<string,int> nodes;
vector<vi> graph;
vi visited;
bool dfs(int node)
{
visited[node]=1;
for(auto x: graph[node])
{
if(visited[x]==0)
{
if(dfs(x))
return true;
}
else if(visited[x]==1)
return true;
}
visited[node]=2;
return false;
}
void solve()
{
cin>>m;
graph=vector<vi> (2*m+1);
nodes.clear();
visited=vi (2*m+1);
int cnt=0;
for(int i=0;i<m;i++)
{
string s,t;
cin>>s>>t;
if(nodes.find(s)==nodes.end())
nodes[s]=cnt++;
if(nodes.find(t)==nodes.end())
nodes[t]=cnt++;
graph[nodes[s]].eb(nodes[t]);
}
for(int i=0;i<cnt;i++)
{
if(!visited[i])
{
if(dfs(i))
{
cout<<"No"<<endl;
return;
}
}
}
cout<<"Yes"<<endl;
}
int main()
{
KAMEHAMEHA
int t=1;
cin>>t;
for(int cn=1;cn<=t;cn++)
{
cout<<"Case "<<cn<<": ";
solve();
}
return 0;
}