Monday, December 10, 2018

[Light OJ] 1359 – Sabotaging Contest

Author            : Dipu Kumar Mohanto 
                    CSE, Batch - 6
                    BRUR.
Problem Statement : 1359 – Sabotaging Contest
Source            : Light Online Judge
Category          : Graph Theory
Algorithm         : Dominator Tree
Verdict           : Accepted
#include "bits/stdc++.h"
#include "ext/pb_ds/assoc_container.hpp"
#include "ext/pb_ds/tree_policy.hpp"
 
using namespace std;
using namespace __gnu_pbds;
 
#define FI              freopen("in.txt", "r", stdin)
#define FO              freopen("out.txt", "w", stdout)
#define FAST            ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL)
 
#define FOR(i, n)       for (int i = 1; i <= n; i++)
#define For(i, n)       for (int i = 0; i < n; i++)
#define ROF(i, n)       for (int i = n; i >= 1; i--)
#define Rof(i, n)       for (int i = n-1; i >= 0; i--)
#define FORI(i, n)      for (auto i : n)
 
#define ll              long long
#define ull             unsigned long long
#define vi              vector <int>
#define vl              vector <ll>
#define pii             pair <int, int>
#define pll             pair <ll, ll>
#define mk              make_pair
#define ff              first
#define ss              second
#define eb              emplace_back
#define em              emplace
#define pb              push_back
#define ppb             pop_back
#define All(a)          a.begin(), a.end()
#define memo(a, b)      memset(a, b, sizeof a)
#define Sort(a)         sort(All(a))
#define ED(a)           Sort(a), a.erase(unique(All(a)), a.end())
#define rev(a)          reverse(All(a))
#define sz(a)           (int)a.size()
#define max3(a, b, c)   max(a, max(b, c))
#define min3(a, b, c)   min(a, min(b, c))
#define maxAll(a)       *max_element(All(a))
#define minAll(a)       *min_element(All(a))
#define allUpper(a)     transform(All(a), a.begin(), :: toupper)
#define allLower(a)     transform(All(a), a.begin(), :: tolower)
#define endl            '\n'
#define nl              puts("")
#define ub              upper_bound
#define lb              lower_bound
#define Exp             exp(1.0)
#define PIE             2*acos(0.0)
#define Sin(a)          sin(((a)*PIE)/180.0)
#define EPS             1e-9
 
// int dr[] = {1, -1, 0, 0}; // 4 Direction
// int dc[] = {0, 0, 1, -1};
// int dr[] = {0, 0, 1, -1, 1, 1, -1, -1}; // 8 Direction
// int dc[] = {1, -1, 0, 0, 1, -1, 1, -1};
// int dr[] = {-1, 1, -2, -2, -1, 1, 2, 2}; // knight Moves
// int dc[] = {-2, -2, -1, 1, 2, 2, 1, -1};
 
#define trace1(x)                           cerr << #x << ": " << x << endl;
#define trace2(x, y)                        cerr << #x << ": " << x << " | " << #y << ": " << y << endl;
#define trace3(x, y, z)                     cerr << #x << ": " << x << " | " << #y << ": " << y << " | " << #z << ": " << z << endl;
#define trace4(a, b, c, d)                  cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << endl;
#define trace5(a, b, c, d, e)               cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << " | " << #e << ": " << e << endl;
#define trace6(a, b, c, d, e, f)            cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << " | " << #e << ": " << e << " | " << #f << ": " << f << endl;
 
inline int setbit(int mask, int pos)      { return mask |= (1 << pos); }
inline int resetbit(int mask, int pos)    { return mask &= ~(1 << pos); }
inline int togglebit(int mask, int pos)   { return mask ^= (1 << pos); }
inline bool checkbit(int mask, int pos)   { return (bool)(mask & (1 << pos)); }
 
#define popcount(mask)                       __builtin_popcount(mask) // count set bit
#define popcountLL(mask)                     __builtin_popcountll(mask) // for long long
 
inline int read()                         { int a; scanf("%d", &a); return a; }
inline ll readLL()                        { ll a; scanf("%lld", &a); return a; }
inline double readDD()                    { double a; scanf("%lf", &a); return a; }
 
template <typename T> string toString(T num) { stringstream ss; ss << num; return ss.str(); }
int toInt(string s)                          { int num; istringstream iss(s); iss >> num; return num;  }
ll toLLong(string s)                         { ll num; istringstream iss(s); iss >> num; return num; }
 
template <typename T> using orderset = tree <T, null_type, less <T>, rb_tree_tag, tree_order_statistics_node_update>;
 
#define inf             123456789
#define mod             1000000007
 
static const int maxn = 1e5 + 5;
static const int logn = 18;
 
struct node
{
      int v, w;
      node(int v = 0, int w = 0) : v(v), w(w) {}
      inline bool operator < (const node &p) const
      {
            return w > p.w;
      }
};
 
vector <node> graph[maxn];
int dist[maxn], dom[maxn], depth[maxn], father[maxn][logn], subtreeSize[maxn];
vector <int> adj[maxn], par[maxn], topo;
vector <int> dominatorTree[maxn];
bool vis[maxn];
int N, M, Q;
 
inline void clean()
{
      topo.clear();
      For(i, maxn)
      {
            graph[i].clear();
            adj[i].clear();
            par[i].clear();
            dominatorTree[i].clear();
            vis[i] = 0;
            depth[i] = 0;
            subtreeSize[i] = 0;
      }
}
 
inline void dijkstra(int src = 1)
{
      For(i, maxn) dist[i] = inf;
      priority_queue <node> PQ;
      PQ.em(src, 0);
      dist[src] = 0;
      while (!PQ.empty())
      {
            node u = PQ.top(); PQ.pop();
            for (node v : graph[u.v])
            {
                  if (dist[v.v] > dist[u.v] + v.w)
                  {
                        dist[v.v] = dist[u.v] + v.w;
                        PQ.em(v.v, dist[v.v]);
                  }
            }
      }
}
 
inline void createDAG(int src = 1)
{
      FOR(u, N)
      {
            for (node e : graph[u])
            {
                  int v = e.v;
                  int w = e.w;
                  if (dist[v] != inf && dist[v] == dist[u] + w)
                  {
                        adj[u].eb(v);
                        par[v].eb(u);
                  }
            }
      }
}
 
inline void topologicalSort(int u = 1)
{
      vis[u] = 1;
      for (int v : adj[u])
      {
            if (vis[v]) continue;
            topologicalSort(v);
      }
      topo.eb(u);
}
 
inline void addToTree(int u, int v)   // u is parent of v in dominator tree
{
      dom[v] = u; // immediate dominator
      depth[v] = depth[u] + 1;
      father[v][0] = u;
      dominatorTree[u].eb(v); // make dominator tree
      dominatorTree[v].eb(u);
      for (int i = 1; i < logn; i++) father[v][i] = father[ father[v][i-1] ][i-1];
}
 
inline int LCA(int u, int v)
{
      if (depth[u] < depth[v]) swap(u, v);
      for (int i = logn-1; i >= 0; i--)
      {
            if (depth[ father[u][i] ] >= depth[v])
            {
                  u = father[u][i];
            }
      }
      if (u == v) return u;
      for (int i = logn-1; i >= 0; i--)
      {
            if (father[u][i] != father[v][i])
            {
                  u = father[u][i];
                  v = father[v][i];
            }
      }
      return father[u][0];
}
 
 
void makeDominatorTree(int root = 1)
{
      topologicalSort(root);
      memo(father, 0);
      memo(dom, -1);
      depth[root] = 1;
      int len = sz(topo);
      for (int i = len-2; i >= 0; i--)
      {
            int u = topo[i];
            int d = -1;
            for (int v : par[u]) d = d == -1 ? v : LCA(d, v);
            addToTree(d, u);
      }
}
 
inline void dfs(int u = 1, int p = 0)
{
      subtreeSize[u] = 1;
      for (int v : dominatorTree[u])
      {
            if (v == p) continue;
            dfs(v, u);
            subtreeSize[u] += subtreeSize[v];
      }
}
 
int main()
{
//      FI;
      int tc = read();
      FOR(tcase, tc)
      {
            N = read(), M = read();
            FOR(m, M)
            {
                  int u = read();
                  int v = read();
                  int w = read();
                  u++, v++;
                  graph[u].eb(v, w);
                  graph[v].eb(u, w);
            }
            dijkstra();
            createDAG();
            makeDominatorTree();
            dfs();
            printf("Case %d:\n", tcase);
            Q = read();
            FOR(q, Q)
            {
                  int K = read();
                  int lca = -1;
                  FOR(k, K)
                  {
                        int v = read();
                        v++;
                        if (!vis[v]) continue;
                        if (lca == -1) lca = v;
                        else lca = LCA(lca, v);
                  }
                  if (lca == -1) puts("0");
                  else printf("%d %d\n", depth[lca], subtreeSize[lca]);
            }
            clean();
      }
}

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.