Thursday, March 28, 2019

[Codeforces] E. Almost Regular Bracket Sequence

Author            : Dipu Kumar Mohanto 
                    CSE, Batch - 6
                    BRUR.
Problem Statement : E. Almost Regular Bracket Sequence
Source            : Codeforces
Category          : Data Structure
Algorithm         : Segment Tree
Verdict           : Accepted

  1. #include "bits/stdc++.h"  
  2.   
  3. using namespace std;  
  4.   
  5. static const int maxn = 1e6 + 5;  
  6.   
  7. struct segmentTree  
  8. {  
  9.       int open, close;  
  10.       segmentTree(int open = 0, int close = 0) : open(open), close(close) {}  
  11.   
  12.       void assignLeaf(char bracket)  
  13.       {  
  14.             open = close = 0;  
  15.             if (bracket == '(') ++open;  
  16.             else ++close;  
  17.       }  
  18.       void marge(const segmentTree &lchild, const segmentTree &rchild)  
  19.       {  
  20.             int mini = min(lchild.open, rchild.close);  
  21.             open = lchild.open + rchild.open - mini;  
  22.             close = lchild.close + rchild.close - mini;  
  23.       }  
  24.       bool isRegular()  
  25.       {  
  26.             return (open == 0 && close == 0);  
  27.       }  
  28. } Tree[maxn*4];  
  29.   
  30. int n;  
  31. string s;  
  32.   
  33. void build(int node = 1, int a = 1, int b = n)  
  34. {  
  35.       if (a == b)  
  36.       {  
  37.             Tree[node].assignLeaf(s[a-1]);  
  38.             return;  
  39.       }  
  40.       int lft = node << 1;  
  41.       int rgt = lft | 1;  
  42.       int mid = (a + b) >> 1;  
  43.       build(lft, a, mid);  
  44.       build(rgt, mid+1, b);  
  45.       Tree[node].marge(Tree[lft], Tree[rgt]);  
  46. }  
  47.   
  48. void update(int node, int a, int b, int pos, char bracket)  
  49. {  
  50.       if (a > pos or b < pos) return;  
  51.       if (a >= pos && b <= pos)  
  52.       {  
  53.             Tree[node].assignLeaf(bracket);  
  54.             return;  
  55.       }  
  56.       int lft = node << 1;  
  57.       int rgt = lft | 1;  
  58.       int mid = (a + b) >> 1;  
  59.       update(lft, a, mid, pos, bracket);  
  60.       update(rgt, mid+1, b, pos, bracket);  
  61.       Tree[node].marge(Tree[lft], Tree[rgt]);  
  62. }  
  63.   
  64. int main()  
  65. {  
  66.       ios_base::sync_with_stdio(false);  
  67.       cin.tie(nullptr);  
  68.       cout << fixed << setprecision(15);  
  69.       #ifndef ONLINE_JUDGE  
  70.             freopen("in.txt""r", stdin);  
  71.       #endif // ONLINE_JUDGE  
  72.   
  73.       cin >> n >> s;  
  74.       build();  
  75.       int cnt = 0;  
  76.       for (int i = 1; i <= n; i++)  
  77.       {  
  78.             char bracket = s[i-1];  
  79.             if (bracket == '(')  
  80.             {  
  81.                   update(1, 1, n, i, ')');  
  82.                   if (Tree[1].isRegular()) ++cnt;  
  83.                   update(1, 1, n, i, '(');  
  84.             }  
  85.             else  
  86.             {  
  87.                   update(1, 1, n, i, '(');  
  88.                   if (Tree[1].isRegular()) ++cnt;  
  89.                   update(1, 1, n, i, ')');  
  90.             }  
  91.       }  
  92.       cout << cnt;  
  93. }  

Monday, March 25, 2019

[UVA] 12363 - Hedge Mazes

Author            : Dipu Kumar Mohanto 
                    CSE, Batch - 6
                    BRUR.
Problem Statement : 12363 - Hedge Mazes
Source            : UVA Online Judge
Category          : Graph Theory
Algorithm         : Finding Bridge
Verdict           : Accepted

  1. #include "bits/stdc++.h"  
  2.   
  3. using namespace std;  
  4.   
  5. static const int maxn = 1e5 + 5;  
  6.   
  7. struct Edge  
  8. {  
  9.       int v, e;  
  10.       Edge(int v = 0, int e = 0) : v(v), e(e) {}  
  11. };  
  12.   
  13. vector <Edge> graph[maxn];  
  14. int maze_no[maxn];  
  15. bool isBridge[maxn], vis[maxn];  
  16. int dfsTime, dis[maxn], low[maxn];  
  17.   
  18. void findBridge(int u, int p, int maze)  
  19. {  
  20.       vis[u] = 1;  
  21.       dis[u] = low[u] = ++dfsTime;  
  22.       maze_no[u] = maze;  
  23.       for (Edge E : graph[u])  
  24.       {  
  25.             int v = E.v;  
  26.             int e = E.e;  
  27.             if (v == p) continue;  
  28.             if (!vis[v])  
  29.             {  
  30.                   findBridge(v, u, maze);  
  31.                   low[u] = min(low[u], low[v]);  
  32.                   if (dis[u] < low[v]) isBridge[e] = 1;  
  33.             }  
  34.             else  
  35.             {  
  36.                   low[u] = min(low[u], dis[v]);  
  37.             }  
  38.       }  
  39. }  
  40.   
  41. int compo[maxn];  
  42.   
  43. void dfs(int u, int comp)  
  44. {  
  45.       compo[u] = comp;  
  46.       for (Edge E : graph[u])  
  47.       {  
  48.             int v = E.v;  
  49.             int e = E.e;  
  50.             if (compo[v] or !isBridge[e]) continue;  
  51.             dfs(v, comp);  
  52.       }  
  53. }  
  54.   
  55. void clean(int n)  
  56. {  
  57.       dfsTime = 0;  
  58.       for (int i = 0; i < n; i++)  
  59.       {  
  60.             graph[i].clear();  
  61.             dis[i] = 0;  
  62.             low[i] = 0;  
  63.             isBridge[i] = 0;  
  64.             vis[i] = 0;  
  65.             compo[i] = 0;  
  66.             maze_no[i] = 0;  
  67.       }  
  68. }  
  69.   
  70. int main()  
  71. {  
  72.       ios_base::sync_with_stdio(false);  
  73.       cin.tie(nullptr);  
  74.       cout << fixed << setprecision(15);  
  75.       #ifndef ONLINE_JUDGE  
  76.             freopen("in.txt""r", stdin);  
  77.       #endif // ONLINE_JUDGE  
  78.   
  79.       int tnode, tedge, tquery;  
  80.       while (cin >> tnode >> tedge >> tquery)  
  81.       {  
  82.             if (tnode + tedge + tquery == 0) break;  
  83.             for (int e = 1; e <= tedge; e++)  
  84.             {  
  85.                   int u, v;  
  86.                   cin >> u >> v;  
  87.                   graph[u].emplace_back(v, e);  
  88.                   graph[v].emplace_back(u, e);  
  89.             }  
  90.             int no = 0;  
  91.             for (int i = 1; i <= tnode; i++)  
  92.             {  
  93.                   if (!vis[i]) findBridge(i, -1, ++no);  
  94.             }  
  95.             no = 0;  
  96.             for (int i = 1; i <= tnode; i++)  
  97.             {  
  98.                   if (!compo[i]) dfs(i, ++no);  
  99.             }  
  100.             while (tquery--)  
  101.             {  
  102.                   int u, v;  
  103.                   cin >> u >> v;  
  104.                   if (maze_no[u] != maze_no[v] or compo[u] != compo[v]) cout << "N\n";  
  105.                   else cout << "Y\n";  
  106.             }  
  107.             cout << "-\n";  
  108.             clean(tnode+10);  
  109.       }  
  110. }