Uvalive 5135 mining your own bussiness [Tarjan dual]

Source: Internet
Author: User

Link1

Link2

Theme

A undirected connected graph allows you to dye some dots in black. You need to disconnect any node after dyeing, and there must be at least one black spot among the remaining nodes in any connected block.

Ideas

At the beginning, we thought that we could multiply the size of each vertex's dual-Unicom component except the cut vertex.

And then find out

Then find out the nature

FirstThe dual-Unicom component of all adjacent points must have common cut points.

If a dual-Unicom component contains only one cut point, if this cut point is cut off, the dual-Unicom component will be isolated.

Therefore, such dual-Unicom components should select at least one point.

Then, if a dual-Unicom component has more than or equal to two cut points, even if one is cut off and the other side is connected to at least one single cut point, the dual-Unicom component

So it is easy to do

If the entire graph is a dual-link component, you can select any two points.

//Author: dream_maker#include<bits/stdc++.h>using namespace std;//----------------------------------------------//typenametypedef long long ll;//convenient for#define fu(a, b, c) for (int a = b; a <= c; ++a)#define fd(a, b, c) for (int a = b; a >= c; --a)#define fv(a, b) for (int a = 0; a < (signed)b.size(); ++a)//inf of different typenameconst int INF_of_int = 1e9;const ll INF_of_ll = 1e18;//fast read and writetemplate <typename T>void Read(T &x) {  bool w = 1;x = 0;  char c = getchar();  while (!isdigit(c) && c != '-') c = getchar();  if (c == '-') w = 0, c = getchar();  while (isdigit(c)) {    x = (x<<1) + (x<<3) + c -'0';    c = getchar();  }  if (!w) x = -x;}template <typename T>void Write(T x) {  if (x < 0) {    putchar('-');    x = -x;  }  if (x > 9) Write(x / 10);  putchar(x % 10 + '0');}//----------------------------------------------typedef pair<int, int> pi;#define fi first#define se secondconst int N = 5e4 + 10;struct Edge {  int v, nxt;} E[N << 1];stack<pi > st;int head[N], tot = 0, ind = 0, cnt_bcc = 0;int n, m, dfn[N], low[N], siz[N], bel[N];bool iscut[N];vector<int> g[N];void init() {  fu(i, 1, N - 1) dfn[i] = low[i] = bel[i] = head[i] = siz[i] = 0, iscut[i] = 0;  tot = ind = cnt_bcc = 0;}void add(int u, int v) {  E[++tot] = (Edge) {v, head[u]};  head[u] = tot;}void tarjan(int u, int fa) {  dfn[u] = low[u] = ++ind;  int num = 0;  for (int i = head[u]; i; i = E[i].nxt) {    int v = E[i].v;    if (v == fa) continue;    if (!dfn[v]) {      st.push(pi(u, v));      ++num;      tarjan(v, u);      low[u] = min(low[u], low[v]);      if (low[v] >= dfn[u]) {        iscut[u] = 1;        g[++cnt_bcc].clear();        pi now;        do {          now = st.top(); st.pop();          if (bel[now.fi] != cnt_bcc) {            g[cnt_bcc].push_back(now.fi);            bel[now.fi] = cnt_bcc;          }           if (bel[now.se] != cnt_bcc) {            g[cnt_bcc].push_back(now.se);            bel[now.se] = cnt_bcc;          }        } while (now.fi != u || now.se != v);      }    } else low[u] = min(low[u], dfn[v]);  }  if (num < 2 && !fa) iscut[u] = 0;}void solve() {  n = 0;  init();  fu(i, 1, m) {    int u, v;    Read(u), Read(v);    add(u, v);    add(v, u);    n = max(n, max(u, v));  }  tarjan(1, 0);  if (cnt_bcc == 1) {    Write(2), putchar(' ');    Write(ll(n) * ll(n - 1) / 2), putchar('\n');  } else {    ll ans1 = 0, ans2 = 1;    fu(i, 1, cnt_bcc) {      int cntnow = 0;      fv(j, g[i])        if (iscut[g[i][j]]) ++cntnow;      if (cntnow == 1)         ++ans1, ans2 *= (ll) g[i].size() - 1;    }    Write(ans1), putchar(' ');    Write(ans2), putchar('\n');  }}int main() {#ifdef dream_maker  freopen("input.txt", "r", stdin);#endif  int id = 0;  while (1) {    Read(m);    if (!m) return 0;    printf("Case %d: ", ++id);    solve();  }  return 0;}

Uvalive 5135 mining your own bussiness [Tarjan dual]

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.