Showing posts with label Strongly Connected Component. Show all posts
Showing posts with label Strongly Connected Component. Show all posts

Saturday, September 21, 2019

[Codemarshal] H. The Great Kind King of Tanguzi

Author            : Dipu Kumar Mohanto 
                    CSE, Batch - 6
                    BRUR.
Problem Statement : H. The Great Kind King of Tanguzi
                    BAPS-BUBT NATIONAL PROGRAMMING CAMP, 2016 ONLINE SELECTION CONTEST
Source            : Codemarshal
Category          : Graph Theory, Strongly Connected Components
Algorithm         : SCC
Verdict           : Accepted

  1. #include "bits/stdc++.h"  
  2.   
  3. using namespace std;  
  4.   
  5. static const int maxn = 1e5 + 5;  
  6.   
  7. vector < vector <int> > graph;  
  8. vector < vector <int> > rgraph;  
  9. vector < vector <int> > dag;  
  10.   
  11. int finishTime[maxn];  
  12. int dfsTime;  
  13. bool vis[maxn];  
  14. bool vis1[maxn];  
  15.   
  16. void dfs(int u)  
  17. {  
  18.       vis[u] = 1;  
  19.       for (int v : graph[u])  
  20.       {  
  21.             if (vis[v]) continue;  
  22.             dfs(v);  
  23.       }  
  24.       finishTime[u] = ++dfsTime;  
  25. }  
  26.   
  27. int compo;  
  28. int compo_num[maxn];  
  29. int compo_sze[maxn];  
  30.   
  31. void dfs_scc(int u)  
  32. {  
  33.       vis1[u] = 1;  
  34.       compo_num[u] = compo;  
  35.       compo_sze[compo]++;  
  36.       for (int v : rgraph[u])  
  37.       {  
  38.             if (vis1[v]) continue;  
  39.             dfs_scc(v);  
  40.       }  
  41. }  
  42.   
  43. int dp[maxn];  
  44. bool memoize[maxn];  
  45.   
  46. int solve(int u)  
  47. {  
  48.       int &ret = dp[u];  
  49.       bool &mem = memoize[u];  
  50.       if (mem) return ret;  
  51.       mem = 1;  
  52.       int sum = compo_sze[u];  
  53.       for (int v : dag[u]) sum += solve(v);  
  54.       return ret = sum;  
  55. }  
  56.   
  57. signed main()  
  58. {  
  59.       ios_base::sync_with_stdio(false);  
  60.       cin.tie(nullptr);  
  61.       cout.tie(nullptr);  
  62.   
  63. //      #ifndef ONLINE_JUDGE  
  64. //            freopen("in.txt", "r", stdin);  
  65. //      #endif // ONLINE_JUDGE  
  66.   
  67.       int tc;  
  68.       cin >> tc;  
  69.       for (int tcase = 1; tcase <= tc; tcase++)  
  70.       {  
  71.             int tnode, tedge;  
  72.             cin >> tnode >> tedge;  
  73.             graph.resize(tnode + 1);  
  74.             rgraph.resize(tnode + 1);  
  75.             vector < pair <intint> > Edge;  
  76.             for (int e = 1; e <= tedge; e++)  
  77.             {  
  78.                   int u, v;  
  79.                   cin >> u >> v;  
  80.                   graph[u].push_back(v);  
  81.                   rgraph[v].push_back(u);  
  82.                   Edge.push_back({u, v});  
  83.             }  
  84.             dfsTime = 0;  
  85.             memset(vis, 0, sizeof vis);  
  86.             for (int i = 1; i <= tnode; i++)  
  87.             {  
  88.                   if (vis[i]) continue;  
  89.                   dfs(i);  
  90.             }  
  91.             vector < pair <intint> > vec;  
  92.             for (int i = 1; i <= tnode; i++) vec.push_back({finishTime[i], i});  
  93.             sort(vec.begin(), vec.end());  
  94.             reverse(vec.begin(), vec.end());  
  95.             memset(vis1, 0, sizeof vis1);  
  96.             memset(compo_num, 0, sizeof compo_num);  
  97.             memset(compo_sze, 0, sizeof compo_sze);  
  98.             compo = 0;  
  99.             for (int i = 0; i < vec.size(); i++)  
  100.             {  
  101.                   int u = vec[i].second;  
  102.                   if (vis1[u]) continue;  
  103.                   ++compo;  
  104.                   dfs_scc(u);  
  105.             }  
  106.             dag.resize(compo + 1);  
  107.             for (int i = 0; i < tedge; i++)  
  108.             {  
  109.                   int u = compo_num[ Edge[i].first ];  
  110.                   int v = compo_num[ Edge[i].second ];  
  111.                   if (u == v) continue;  
  112.                   dag[u].push_back(v);  
  113.             }  
  114.             memset(memoize, 0, sizeof memoize);  
  115.             for (int i = 1; i <= compo; i++) dp[i] = solve(i);  
  116.             long long ans = 1;  
  117.             for (int i = 1; i <= tnode; i++)  
  118.             {  
  119.                   int sze = dp[ compo_num[i] ];  
  120.                   ans = (1LL * sze * ans) % 1000000007;  
  121.             }  
  122.             cout << "Case " << tcase << ": " << ans << '\n';  
  123.   
  124.             graph.clear();  
  125.             rgraph.clear();  
  126.             dag.clear();  
  127.       }  
  128. }  

Sunday, February 17, 2019

[Codeforces] E. The Road to Berland is Paved With Good Intentions

Author            : Dipu Kumar Mohanto 
                    CSE, Batch - 6
                    BRUR.
Problem Statement : E. The Road to Berland is Paved With Good Intentions
Source            : Codeforces
Category          : Graph Theory, Strongly Connected Components, 2 SAT
Algorithm         : 2 SAT
Verdict           : Accepted
 
  1. #include "bits/stdc++.h"    
  2. #include "ext/pb_ds/assoc_container.hpp"    
  3. #include "ext/pb_ds/tree_policy.hpp"    
  4. #include "ext/rope"    
  5.     
  6. using namespace std;    
  7. using namespace __gnu_pbds;    
  8. using namespace __gnu_cxx;    
  9.     
  10. #define FI              freopen("in.txt", "r", stdin)    
  11. #define FO              freopen("out.txt", "w", stdout)    
  12. #define FAST            ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL)    
  13.     
  14. #define FOR(i, n)       for (int i = 1; i <= n; i++)    
  15. #define For(i, n)       for (int i = 0; i < n; i++)    
  16. #define ROF(i, n)       for (int i = n; i >= 1; i--)    
  17. #define Rof(i, n)       for (int i = n-1; i >= 0; i--)    
  18. #define FORI(i, n)      for (auto i : n)    
  19. #define FORAB(i, a, b)  for (int i = a; i <= b; i++)    
  20.     
  21. #define ll              long long    
  22. #define ull             unsigned long long    
  23. #define vi              vector <int>    
  24. #define vl              vector <ll>    
  25. #define pii             pair <int, int>    
  26. #define pll             pair <ll, ll>    
  27. #define mk              make_pair    
  28. #define ff              first    
  29. #define ss              second    
  30. #define eb              emplace_back    
  31. #define em              emplace    
  32. #define pb              push_back    
  33. #define ppb             pop_back    
  34. #define All(a)          a.begin(), a.end()    
  35. #define memo(a, b)      memset(a, b, sizeof a)    
  36. #define Sort(a)         sort(All(a))    
  37. #define ED(a)           Sort(a), a.erase(unique(All(a)), a.end())    
  38. #define rev(a)          reverse(All(a))    
  39. #define sz(a)           (int)a.size()    
  40. #define max3(a, b, c)   max(a, max(b, c))    
  41. #define min3(a, b, c)   min(a, min(b, c))    
  42. #define maxAll(a)       *max_element(All(a))    
  43. #define minAll(a)       *min_element(All(a))    
  44. #define allUpper(a)     transform(All(a), a.begin(), :: toupper)    
  45. #define allLower(a)     transform(All(a), a.begin(), :: tolower)    
  46. #define endl            '\n'    
  47. #define nl              puts("")    
  48. #define ub              upper_bound    
  49. #define lb              lower_bound    
  50. #define Exp             exp(1.0)    
  51. #define PIE             2*acos(0.0)    
  52. #define Sin(a)          sin(((a)*PIE)/180.0)    
  53. #define EPS             1e-9    
  54.     
  55. template <typename T> using orderset = tree <T, null_type, less <T>, rb_tree_tag, tree_order_statistics_node_update>;    
  56.     
  57. // rope <int> Rope;    
  58.     
  59. // int dr[] = {1, -1, 0, 0}; // 4 Direction    
  60. // int dc[] = {0, 0, 1, -1};    
  61. // int dr[] = {0, 0, 1, -1, 1, 1, -1, -1}; // 8 Direction    
  62. // int dc[] = {1, -1, 0, 0, 1, -1, 1, -1};    
  63. // int dr[] = {-1, 1, -2, -2, -1, 1, 2, 2}; // knight Moves    
  64. // int dc[] = {-2, -2, -1, 1, 2, 2, 1, -1};    
  65.     
  66. #define here                                cerr << "Here" << endl;    
  67. #define trace1(x)                           cerr << #x << ": " << x << endl;    
  68. #define trace2(x, y)                        cerr << #x << ": " << x << " | " << #y << ": " << y << endl;    
  69. #define trace3(x, y, z)                     cerr << #x << ": " << x << " | " << #y << ": " << y << " | " << #z << ": " << z << endl;    
  70. #define trace4(a, b, c, d)                  cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << endl;    
  71. #define trace5(a, b, c, d, e)               cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << " | " << #e << ": " << e << endl;    
  72. #define trace6(a, b, c, d, e, f)            cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << " | " << #e << ": " << e << " | " << #f << ": " << f << endl;    
  73.     
  74. inline int setbit(int mask, int pos)        { return mask |= (1 << pos); }    
  75. inline int resetbit(int mask, int pos)      { return mask &= ~(1 << pos); }    
  76. inline int togglebit(int mask, int pos)     { return mask ^= (1 << pos); }    
  77. inline bool checkbit(int mask, int pos)     { return (bool)(mask & (1 << pos)); }    
  78.     
  79. #define ones(mask)                          __builtin_popcount(mask)   // count set bit    
  80. #define onesLL(mask)                        __builtin_popcountll(mask) // for long long    
  81. #define lzeros(mask)                        __builtin_clz(mask)        // no of leading zeros    
  82. #define tzeros(mask)                        __builtin_ctz(mask)        // no of trailing zeros    
  83.     
  84. inline int read()                           { int a; scanf("%d", &a); return a; }    
  85. inline ll readLL()                          { ll a; scanf("%lld", &a); return a; }    
  86. inline double readDD()                      { double a; scanf("%lf", &a); return a; }    
  87.     
  88. template <typename T> string toString(T num) { stringstream ss; ss << num; return ss.str(); }    
  89. int toInt(string s)                          { int num; istringstream iss(s); iss >> num; return num;  }    
  90. ll toLLong(string s)                         { ll num; istringstream iss(s); iss >> num; return num; }    
  91.     
  92. #define inf             1e8    
  93. #define mod             1000000007    
  94.     
  95. static const int maxn = 300 + 5;    
  96. static const int logn = 18;    
  97.     
  98. int city, road;    
  99. vi graph[maxn], rgraph[maxn];    
  100. int finishTime[maxn], dfsTime;    
  101. int rfinishTime[maxn], rdfsTime;    
  102. bool vis[maxn], vis1[maxn];    
  103. int scc_no[maxn];    
  104.     
  105. void dfs(int u)    
  106. {    
  107.       vis[u] = 1;    
  108.       for (int v : graph[u])    
  109.       {    
  110.             if (vis[v]) continue;    
  111.             dfs(v);    
  112.       }    
  113.       finishTime[u] = ++dfsTime;    
  114. }    
  115.     
  116. void dfsSCC(int u, int compo)    
  117. {    
  118.       vis1[u] = 1;    
  119.       scc_no[u] = compo;    
  120.       for (int v : rgraph[u])    
  121.       {    
  122.             if (vis1[v]) continue;    
  123.             dfsSCC(v, compo);    
  124.       }    
  125.       rfinishTime[u] = ++rdfsTime;    
  126. }    
  127.     
  128. int main()    
  129. {    
  130.       //FI;    
  131.       //FAST;    
  132.     
  133.       city = read();    
  134.       road = read();    
  135.       FOR(r, road)    
  136.       {    
  137.             int u = read();    
  138.             int v = read();    
  139.             int status = read();    
  140.             if (status == 0)    
  141.             {    
  142.                   graph[city+u].eb(v);    
  143.                   graph[city+v].eb(u);    
  144.                   rgraph[v].eb(city+u);    
  145.                   rgraph[u].eb(city+v);    
  146.     
  147.                   graph[u].eb(city+v);    
  148.                   graph[v].eb(city+u);    
  149.                   rgraph[city+v].eb(u);    
  150.                   rgraph[city+u].eb(v);    
  151.             }    
  152.             else    
  153.             {    
  154.                   graph[city+u].eb(city+v);    
  155.                   graph[v].eb(u);    
  156.                   rgraph[city+v].eb(city+u);    
  157.                   rgraph[u].eb(v);    
  158.     
  159.                   graph[u].eb(v);    
  160.                   graph[city+v].eb(city+u);    
  161.                   rgraph[v].eb(u);    
  162.                   rgraph[city+u].eb(city+v);    
  163.             }       
  164.       }    
  165.       FOR(i, 2*city) if (vis[i] == 0) dfs(i); 
  166.       vector <pii> vec;    
  167.       FOR(i, 2*city) vec.eb(finishTime[i], i);     
  168.       Sort(vec);    
  169.       int compo = 0;    
  170.       Rof(i, sz(vec))    
  171.       {    
  172.             int u = vec[i].second;    
  173.             if (vis1[u] == 0) dfsSCC(u, ++compo);    
  174.       }    
  175.       vi True;    
  176.       FOR(i, city)    
  177.       {      
  178.             if (scc_no[i] == scc_no[city+i])    
  179.             {    
  180.                   puts("Impossible");    
  181.                   exit(0);    
  182.             }    
  183.             if (rfinishTime[i] > rfinishTime[city+i]) True.eb(i);    
  184.       }    
  185.       printf("%d\n", sz(True));    
  186.       for (int t : True) printf("%d ", t);    
  187. }