HDU 2767 Strong Unicom components

Source: Internet
Author: User

Click to open link

Test instructions: Ask how many sides the graph will become a strong unicom component of 1 figure

Train of thought: simple strong unicom, shrink point after looking into the degree and out of the line, water problem

#include <vector> #include <stdio.h> #include <string.h> #include <stdlib.h> #include < Iostream> #include <algorithm>using namespace std;typedef long long ll;const int Inf=0x3f3f3f3f;const int maxn= 20010;vector<int> g[maxn];vector<int> rg[maxn];vector<int> vs;bool used[maxn];int Cmp[maxn],V;    void Add_edge (int from,int to) {g[from].push_back (to); Rg[to].push_back (from);}    void Dfs (int v) {used[v]=1;    for (int i=0;i<g[v].size (); i++) {if (!used[g[v][i])) DFS (G[v][i]); } vs.push_back (v);}    void Rdfs (int v,int k) {used[v]=1;    Cmp[v]=k;    for (int i=0;i<rg[v].size (); i++) {if (!used[rg[v][i]) Rdfs (rg[v][i],k);    }}int SCC () {memset (used,0,sizeof (used));    Vs.clear ();    for (int v=0;v<v;v++) if (!used[v]) Dfs (v);    memset (used,0,sizeof (used));    int k=0;    for (int i=vs.size () -1;i>=0;i--) {if (!used[vs[i]) Rdfs (vs[i],k++); } return k;} int A[maxn*3],b[maxn*3];int In[maxn],out[maxn];int mAin () {int t,m;    scanf ("%d", &t);        while (t--) {scanf ("%d%d", &v,&m);        memset (In,0,sizeof (in));        Memset (out,0,sizeof (out));            for (int i=0;i<maxn;i++) {g[i].clear ();        Rg[i].clear ();        } vs.clear ();            for (int i=0;i<m;i++) {scanf ("%d%d", &a[i],&b[i]); a[i]--;            b[i]--;        Add_edge (A[i],b[i]);        } int ANS=SCC ();            if (ans==1) {printf ("0\n");        Continue            } for (int i=0;i<m;i++) {int a=cmp[a[i]],b=cmp[b[i]];            if (a!=b) {in[b]++;out[a]++;        }} int sum1=0,sum2=0;            for (int i=0;i<ans;i++) {if (in[i]==0) sum1++;        if (out[i]==0) sum2++;    } printf ("%d\n", Max (sum1,sum2)); } return 0;}

HDU 2767 Strong Unicom components

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.