POJ 3352 Road Construction (point double connected component shrink Point + shrinking point tree into double connected component)

Source: Internet
Author: User

Topic Link:

Click to open the link

The main effect of the topic:

Give a picture, ask at least how many edges, turn him into a side-connected graph

Topic Analysis:

First, the point of double connected graph is reduced, (the point-connected graph must be a side-connected graph), then get a tree, for a tree, we just need to know how many points at the bottom of the tree, and then connect them 22, then all the points have at least two paths to reach, because the tree's two chain connected to become a ring, The loop is the path point has two paths to arrive, and then very easy to get the results, shrink point what is already a template

The code is as follows:

#include <iostream> #include <algorithm> #include <cstdio> #include <cstring> #include <

vector> #include <map> #include <stack> #define MAX 1007 using namespace std;
int n,m,u,v;
int dfn[max],low[max],b[max],times,cnt;

int In[max];

typedef pair<int,int> PII;
Stack<int> s;
Vector<int> E[max];


Map<pii,bool> MP;
    void Tarjan (int u, int p) {Dfn[u] = low[u] = ++times;
    S.push (U);
    int len = E[u].size ();
        for (int i = 0; i < len; i++) {int v = e[u][i];
        if (v = = p) continue;
            if (!dfn[v]) {Tarjan (V, u);
        Low[u] = min (Low[u], low[v]);
    else Low[u] = min (Low[u], dfn[v]);
        } if (low[u] = = Dfn[u]) {int temp;
            do {temp = S.top ();
            B[TEMP] = cnt;
        S.pop ();
        }while (temp!= u);
    cnt++;
} void Init () {    for (int i = 0; i < MAX; i++) e[i].clear ();
    while (!s.empty ()) S.pop ();
    Times = CNT = 0;
    Mp.clear ();
memset (in, 0, sizeof (in));
        int main () {while (~scanf ("%d%d", &n, &m)) {init ();
            while (m--) {scanf ("%d%d", &u, &v);
            E[u].push_back (v);
        E[v].push_back (U);

        for (int i = 1; I <= n; i++) if (!dfn[i]) Tarjan (i,-1);
            for (int i = 1; I <= n; i++) {int len = e[i].size ();
                for (int j = 0; J < Len; j +) {int v = e[i][j];
                if (b[i] = = B[v]) continue;
                if (Mp[make_pair (I,V)]) continue;
                Mp[make_pair (i,v)] = true;
            in[b[v]]++;
        } int ans = 0;
for (int i = 0; i < cnt; i++) if (in[i] = = 1) ans++;
        printf ("%d\n", (ans+1)/2);
 }
}


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.