Saleh127

Light oj(LO) 1003

Mar 9th, 2021
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.49 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define ll long long
  4. #define test int t; cin>>t; for(int cs=1;cs<=t;cs++)
  5. queue<ll>q;
  6. map<string,ll>mp;
  7. vector<ll>g[100000];
  8. ll a[100000];
  9. ll ans,l=1;
  10.  
  11. void solve()
  12. {
  13. while(!q.empty())
  14. {
  15. ll u=q.front();
  16. q.pop();
  17.  
  18. ans++;
  19.  
  20. for(ll i=0;i<g[u].size();i++)
  21. {
  22. a[g[u][i]]--;
  23. if(a[g[u][i]]==0)
  24. {
  25. q.push(g[u][i]);
  26. }
  27. }
  28.  
  29. }
  30. }
  31.  
  32.  
  33. int main()
  34. {
  35. ios_base::sync_with_stdio(0);
  36. cin.tie(0);cout.tie(0);
  37.  
  38. ll n;
  39.  
  40. test
  41. {
  42. cin>>n;
  43.  
  44. ll i,j,k=0;
  45.  
  46. l=1;
  47.  
  48. for(i=0;i<50000;i++)
  49. {
  50. a[i]=0;
  51. g[i].clear();
  52. }
  53. mp.clear();
  54.  
  55.  
  56. for(i=0;i<n;i++)
  57. {
  58. string u,v;
  59.  
  60. cin>>u>>v;
  61.  
  62. if(mp[u]==0)
  63. {
  64. k++;
  65. mp[u]=k;
  66. }
  67. if(mp[v]==0)
  68. {
  69. k++;
  70. mp[v]=k;
  71. }
  72. ll x,y;
  73. x=mp[u];
  74. y=mp[v];
  75.  
  76. g[x].push_back(y);
  77. a[y]++;
  78.  
  79. }
  80.  
  81. for(i=1;i<=k;i++)
  82. {
  83. if(a[i]==0)
  84. {
  85. q.push(i);
  86. }
  87. }
  88. ans=0;
  89. solve();
  90.  
  91. if(ans!=k) l=0;
  92.  
  93.  
  94. if(l==0) cout<<"Case "<<cs<<": No"<<endl;
  95. else cout<<"Case "<<cs<<": Yes"<<endl;
  96.  
  97. }
  98.  
  99.  
  100. return 0;
  101. }
  102.  
Advertisement
Add Comment
Please, Sign In to add comment