#include <bits/stdc++.h> #define sf scanf #define pf printf #define WHITE 0 #define GRAY 1 #define BLACK 2 #define newl '\n' #define FAST ios_base::sync_with_stdio(0); using namespace std; map<string , int>visited; map<string, vector<string> >mp; bool cycle; void visit(string start){ visited[start] = GRAY; for(int i=0; i<mp[start].size(); i++){ if(visited[mp[start][i]] == WHITE){ visit(mp[start][i]); }else if(visited[mp[start][i]] == GRAY){ cycle = true; return; } } visited[start] = BLACK; } void DFS(){ map<string, vector<string> >::iterator it; visited.clear(); for(it = mp.begin() ; it!=mp.end(); it++){ if(cycle)return; if(visited[(*it).first] == WHITE){ visit((*it).first); } } } int main() { FAST; int t , kase=0; cin >> t; while(t--){ int n; cin >> n; for(int i=0; i<n; i++){ string a , b; cin >> a >> b; mp[a].push_back(b); visited[a] = WHITE; } cycle = false; DFS(); if(cycle)cout<<"Case "<<++kase<<": No"<<newl; else cout<<"Case "<<++kase<<": Yes"<<newl; mp.clear(); visited.clear(); } return 0; }
I have stored my online judge solutions of my own :)
Tuesday, January 10, 2017
1003 - Drunk - LightOJ
Subscribe to:
Posts (Atom)