LightOj 1003 - Drunk
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<vector>
#include<map>
#include<string>
#include<stack>
#define sf scanf
#define pf printf
#define mx 20010
#define white 0
#define gray 1
#define black 2
using namespace std;
typedef map<string, int>mpsi;
typedef vector < vector <int> >vvii;
typedef vector<int>vi;
vi G[mx];
int color[mx];
bool cycle = false;
void graph_clear()
{
for(int i=0; i<mx; i++)
{
G[i].clear();
}
}
void DFS_Visit(int u)
{
color[u] = gray;
int i , v;
for(i=0; i<G[u].size(); i++)
{
v = G[u][i];
if(color[v] == white)
{
DFS_Visit(v);
}
else if(color[v] == gray)
{
cycle = true;
return;
}
}
color[u] = black;
}
void DFS(int node)
{
memset(color, 0, sizeof(color));
int i;
for(i=0; i<node; i++)
{
if(cycle)
{
return ;
}
if(color[i] == white)
{
DFS_Visit(i);
}
}
}
int main()
{
ios_base::sync_with_stdio(false);
int t, tc=0;
mpsi mp;
cin >> t;
while(t--)
{
mp.clear();
graph_clear();
cycle=false;
int m , i , assin=0 , x , y;
string s1, s2;
cin >> m;
for(i=0; i<m; i++)
{
cin >> s1;
if(mp.find(s1) == mp.end())
{
mp[s1] = assin;
assin++;
}
cin >> s2;
if(mp.find(s2) == mp.end())
{
mp[s2] = assin;
assin++;
}
x = mp[s1];
y = mp[s2];
G[x].push_back(y);
}
DFS(assin);
cout << "Case " << ++tc << ": ";
string ans = cycle ? "No" : "Yes";
cout << ans << "\n";
}
return 0;
}
#include<cstdio>
#include<cstring>
#include<cmath>
#include<vector>
#include<map>
#include<string>
#include<stack>
#define sf scanf
#define pf printf
#define mx 20010
#define white 0
#define gray 1
#define black 2
using namespace std;
typedef map<string, int>mpsi;
typedef vector < vector <int> >vvii;
typedef vector<int>vi;
vi G[mx];
int color[mx];
bool cycle = false;
void graph_clear()
{
for(int i=0; i<mx; i++)
{
G[i].clear();
}
}
void DFS_Visit(int u)
{
color[u] = gray;
int i , v;
for(i=0; i<G[u].size(); i++)
{
v = G[u][i];
if(color[v] == white)
{
DFS_Visit(v);
}
else if(color[v] == gray)
{
cycle = true;
return;
}
}
color[u] = black;
}
void DFS(int node)
{
memset(color, 0, sizeof(color));
int i;
for(i=0; i<node; i++)
{
if(cycle)
{
return ;
}
if(color[i] == white)
{
DFS_Visit(i);
}
}
}
int main()
{
ios_base::sync_with_stdio(false);
int t, tc=0;
mpsi mp;
cin >> t;
while(t--)
{
mp.clear();
graph_clear();
cycle=false;
int m , i , assin=0 , x , y;
string s1, s2;
cin >> m;
for(i=0; i<m; i++)
{
cin >> s1;
if(mp.find(s1) == mp.end())
{
mp[s1] = assin;
assin++;
}
cin >> s2;
if(mp.find(s2) == mp.end())
{
mp[s2] = assin;
assin++;
}
x = mp[s1];
y = mp[s2];
G[x].push_back(y);
}
DFS(assin);
cout << "Case " << ++tc << ": ";
string ans = cycle ? "No" : "Yes";
cout << ans << "\n";
}
return 0;
}
How to do with Java?
ReplyDelete