Poj2186tarjan algorithm to find out the degree of contraction point

Source: Internet
Author: User

Poj2186tarjan algorithm to find out the degree of contraction point

Play the first question yourself, get started, get started

The topic is simple, many cows, to you admire the relationship (can pass), ask you whether the last cow is all the cattle admire

According to the relationship can be built, the use of Tarjan algorithm after the indentation processing, to get the direction of the non-circular graph, the point of contraction is mutual admiration, so according to the transitivity can be regarded as a point, and then the staining of the block, calculate the out of each block.

If the out degree is 0 and there is only one, then the output of all the points within the block will meet the requirements

If you have more than one direct output of 0

#include <iostream> #include <cstdio> #include <string.h>using namespace std;const int maxn = 10005; const int MAXM = 100005;struct node{int to,pre;} E[maxm];int n,m;int idx;int id[maxn],cnt;int dfn[maxn],low[maxn];int stack1[maxn],s_top;int out[maxn];int COLOR[MAXN]    ; int Vis[maxn];int cut_point = 0;void init () {memset (dfn,0,sizeof (DFN));    memset (vis,0,sizeof (VIS));    memset (color,0,sizeof (color));    memset (low,0,sizeof (Low));    Memset (id,-1,sizeof (id));    Memset (out,0,sizeof (id));    CNT = 0;    IDX = 0;    s_top = 0; Cut_point = 0;}    void Add (int u,int v) {e[cnt].to = v;    E[cnt].pre = Id[u]; Id[u] = cnt++;}    void Tarjan (int u, int pre) {Dfn[u] = low[u] = ++idx;    Vis[u] = 1;    stack1[s_top++] = u;        for (int i = Id[u];~i;i = e[i].pre) {int v = e[i].to;            if (!vis[v]) {Tarjan (v,u);        Low[u] = min (Low[v],low[u]);        } else {Low[u] = min (low[u],dfn[v]); }} if (low[U] = = Dfn[u]) {cut_point++;//color 1 ...            n while (s_top > 0 && stack1[s_top]! = u)//The purpose is to first process the element in the judgment is not the last {s_top--;            Vis[stack1[s_top]] = 2;        Color[stack1[s_top]] = cut_point;    }}}int Main () {int u,v;        while (~SCANF ("%d%d", &n,&m)) {//Initialize init ();            Add Edge for (int i = 0;i < m;i++) {scanf ("%d%d", &u,&v);        Add (U,V);                }//Indent processing--staining for (int i = 1;i <= n;i++)//Prevent non-connected condition {if (!vis[i]) {            Tarjan (i,-1);            }}//After dyeing successfully, record the degree for (int i = 1;i <= n;i++) {for (int j = id[i];~j;j = E[j].pre)                {int v = e[j].to;                if (color[i]! = Color[v]) {++out[color[i]]; }}}//Take a look at the set of points with a degree of 0, find all the points//color is also the same, cannot have two int sum = 0,p_color;        for (int i = 1;i <= cut_point;i++) {if (!out[i]) Sum++,p_color = i;            } if (sum = = 1) {int ans = 0;            for (int i = 1;i <= n;i++) {if (color[i] = = P_color) ans++;        } printf ("%d\n", ans);        } else {puts ("0"); }} return 0;}

Poj2186tarjan algorithm to find out the degree of contraction point

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.