Advertisement
arinado

recursion

Jan 1st, 2018
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.28 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include <iostream>
  3. #include <vector>
  4. #include<algorithm>
  5. #include<string>
  6. #include<math.h>
  7. #pragma comment(linker, "/STACK:66777216")
  8.  
  9. using namespace std;
  10.  
  11. #define ll unsigned _int64
  12.  
  13. vector<ll>u;
  14. vector<pair<string, vector<string>>>p;
  15. ll n;
  16.  
  17. bool dfs(string s, vector<string>v, ll j) {
  18. u[j] = 1;
  19. for (string s1 : v) {
  20. if (s1 == s)return(1);
  21. ll i = 0;
  22. for (i; i < n; i++) {
  23. if (p[i].first == s1) break;
  24. }
  25. if (!u[i])dfs(s1, p[i].second, i);
  26. else return(0);
  27. }
  28. }
  29.  
  30. int main() {
  31. ios_base::sync_with_stdio(false);
  32. // #ifndef ONLINE_JUDGE
  33. freopen("input.txt", "r", stdin);
  34. freopen("output.txt", "w", stdout);
  35. // #endif
  36.  
  37. cin >> n;
  38. u.resize(n);
  39.  
  40.  
  41. for (ll i = 0; i < n; i++) {
  42. ll c;
  43. string s;
  44. cin >> s >> c;
  45. vector<string>cc(c, "");
  46. p.emplace_back(make_pair(s, cc));
  47. for (ll j = 0; j < c; j++) {
  48. cin >> p[i].second[j];
  49. }
  50. cin >> s;
  51. }
  52. ll i = 0;
  53. for (pair<string, vector<string>> a : p) {
  54. cout << a.first << ':' << ' ';
  55. if (dfs(a.first, a.second, i)) {
  56. ll f = 1;
  57. for (ll o : u) {
  58. if (!o) {
  59. cout << "NO" << endl;
  60. f = 0;
  61. break;
  62. }
  63. }
  64. if (f)cout << "YES" << endl;
  65. }
  66. else cout << "NO" << endl;
  67. i++;
  68. u.assign(n, 0);
  69. }
  70.  
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement