Monday, December 10, 2018

[UVa] 12616 - Gymman vs Fila

Author            : Dipu Kumar Mohanto 
                    CSE, Batch - 6
                    BRUR.
Problem Statement : 12616 - Gymman vs Fila
Source            : UVa Online Judge
Category          : Graph Theory
Algorithm         : Block Cut Tree, Articulation Point
Verdict           : Accepted 
#include "bits/stdc++.h"
 
using namespace std;
 
#define ll             long long
#define vi             vector <int>
#define em             emplace
#define eb             emplace_back
 
static const int maxn = 2e5 + 5;
 
vi graph[maxn], btree[maxn];
int tnode, tedge;
int dfsTime, disc[maxn], low[maxn], tot, bcc[maxn];
stack <int> s;
 
void addEdge1(int u, int v) { graph[u].eb(v), graph[v].eb(u); }
void addEdge2(int u, int v) { btree[u].eb(v), btree[v].eb(u); }
 
void findBCC(int u, int p = -1)
{
    dfsTime++;
    disc[u] = low[u] = dfsTime;
    s.em(u);
    for (int v : graph[u])
    {
        if (v == p) continue;
        if (!disc[v])
        {
            findBCC(v, u);
            low[u] = min(low[u], low[v]);
            if (disc[u] <= low[v])
            {
                tot++;
                int h;
                do {
                    h = s.top();
                    bcc[h] = tot;
                    addEdge2(h, tot);
                    s.pop();
                } while (h != v);
                addEdge2(tot, u);
            }
        }
        else low[u] = min(low[u], disc[v]);
    }
}
 
int subTreeSize[maxn];
 
void dfs(int u, int p = -1)
{
    subTreeSize[u] = 0;
    if (u <= tnode) subTreeSize[u] = 1;
    for (int v : btree[u])
    {
        if (v == p) continue;
        dfs(v, u);
        subTreeSize[u] += subTreeSize[v];
    }
}
 
ll calc(const vi &v, int u, int total)
{
    int len = v.size();
    if (u == 1 && len <= 1) return 0;
    if (u > tnode || len < 1) return 0;
    vi sum(len+1);
    sum[0] = v[0];
    for (int i = 1; i < len; i++) sum[i] = sum[i-1] + v[i];
    ll cnt = 0;
    for (int i = 0; i < len; i++) cnt += v[i] * (sum[len-1] - sum[i]);
    ll up = total - sum[len-1] - 1;
    if (up < 0) up = 0;
    cnt += sum[len-1] * up;
    return cnt*2;
}
 
ll triplets;
 
void dfs2(int u, int p = -1, int total = 0)
{
    vi vec;
    for (int v : btree[u])
    {
        if (v == p) continue;
        if (v > tnode) vec.eb(subTreeSize[v]);
        dfs2(v, u, total);
    }
    triplets += calc(vec, u, total);
}
 
void clearall(int u, int p = -1)
{
    for (int v : btree[u])
    {
        if (v == p) continue;
        clearall(v, u);
    }
    subTreeSize[u] = 0;
    btree[u].clear();
}
 
void init()
{
    triplets = 0;
    dfsTime = 0;
    for (int i = 0; i < maxn; i++)
    {
        graph[i].clear();
        btree[i].clear();
        disc[i] = 0;
        low[i] = 0;
        bcc[i] = 0;
    }
}
 
int main()
{
    //freopen("in.txt", "r", stdin);
 
    int tc;
    cin >> tc;
    for (int tcase = 1; tcase <= tc; tcase++)
    {
        init();
        cin >> tnode >> tedge;
        for (int e = 1; e <= tedge; e++)
        {
            int u, v;
            cin >> u >> v;
            addEdge1(u, v);
        }
        for (int i = 1; i <= tnode; i++)
        {
            if (disc[i]) continue;
            tot = tnode;
            findBCC(i, -1);
            dfs(i, -1);
            dfs2(i, -1, subTreeSize[i]);
            clearall(i, -1);
        }
        cout << "Case " << tcase << ": " << triplets << endl;
    }
}
 

No comments:

Post a Comment

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