Connect Graph Exercise Summary

Source: Internet
Author: User

Connected graph is a concept of graph theory based on Unicom, and part of the investigation of graph theory in ACM is also based on connected graph. The basic idea of solving this kind of problem is to find out the corresponding connected component (strong connection of directed graph, double connectivity of undirected graph) to simplify the graph, and then combine with other algorithms to calculate.

1. POJ 3180 the Cow Prom

This question if can understand the topic, how to do it is very obvious, can form a small group can circle, is equivalent to a strong connected components, it is necessary to note that this small group can not only a cow.

#include <set> #include <map> #include <list> #include <stack> #include <queue> #include <ctime> #include <cmath> #include <cstdio> #include <vector> #include <string> #include <cstdlib> #include <cstring> #include <iostream> #include <algorithm> #pragma comment (linker, " /stack:1024000000,1024000000 ") #define IT iterator#define PB (x) push_back (x) #define CLR (       A, B) memset (A,b,sizeof (a)) using namespace Std;typedef long long ll;typedef unsigned long long Ull;typedef vector<int> vint;typedef vector<ll> vll;typedef vector& lt;ull> vull;typedef set<int> sint;typedef set<ull> sull; const int MAXN = 10000 + 5;vint g[maxn];int pre[maxn],lowlink[maxn],sccno[maxn],dfs_clock,scc_cnt;stack<int> S; int a[maxn];void init (int n) {for (int i = 0; I <= N; i++) G[i].clear ();}    void Dfs (int u) {pre[u] = lowlink[u] = ++dfs_clock;    S.push (U);        for (int i = 0; i < g[u].size (); i++) {int v = g[u][i];            if (!pre[v]) {DFS (v);        Lowlink[u] = min (lowlink[u],lowlink[v]);        } else if (!sccno[v]) {Lowlink[u] = min (lowlink[u],pre[v]);        }} if (lowlink[u] = = Pre[u]) {scc_cnt++;            while (1) {int x = S.top ();            S.pop ();            SCCNO[X] = scc_cnt;        if (x = = u) break;    }}}void FIND_SCC (int n) {dfs_clock = scc_cnt = 0;    CLR (sccno,0);    CLR (pre,0);    for (int i = 1; I <= n; i++) {if (!pre[i]) DFS (i);    }}int Cnt[maxn];int Main () {int n,m;    Freopen ("Data.in", "R", stdin);        while (cin>>n>>m) {init (n);            for (int i = 0; i < m; i++) {int A, B;            scanf ("%d%d", &a,&b); G[a].        PB (b); }//for (int i = 0; I <= scc_cnT        i++) new_g[i].clear ();        FIND_SCC (n);        for (int i = 1; I <= n; i++) {cnt[sccno[i]]++;        } int ans = 0;        /*cout<<scc_cnt<<endl; for (int i = 1, i <= scc_cnt; i++) {for (int j = 0; J < New_g[i].size (); j + +) {cout<&            lt;i<< "" <<new_g[i][j].from<< "" <<new_g[i][j].cost<<endl;        }}*/for (int i = 1; I <= scc_cnt; i++) {if (Cnt[i] > 1) ans++;    } cout<<ans<<endl; }}

2. POJ 1236 Network of schools

At the beginning of the processing of the diagram also need to use strong connectivity to re-composition of the indentation, and then for two tasks, the first good understanding is the number of strong connectivity to 0, the second problem needs a little thought, to the whole diagram into a strong connectivity, Then it is equivalent to the current figure in the degree of 0 and out of 0 points are eliminated (plus edge), then think of here the answer is out, that is, the number of points max{into 0, the number of points of 0.

#include <set> #include <map> #include <list> #include <stack> #include <queue> #include <ctime> #include <cmath> #include <cstdio> #include <vector> #include <string> #include <cstdlib> #include <cstring> #include <iostream> #include <algorithm> #pragma comment (linker, " /stack:1024000000,1024000000 ") #define IT iterator#define PB (x) push_back (x) #define CLR (       A, B) memset (A,b,sizeof (a)) using namespace Std;typedef long long ll;typedef unsigned long long Ull;typedef vector<int> vint;typedef vector<ll> vll;typedef vector& lt;ull> vull;typedef set<int> sint;typedef set<ull> sull; const int MAXN = 10000 + 5;vint g[maxn];int pre[maxn],lowlink[maxn],sccno[maxn],dfs_clock,scc_cnt;stack<int> S; int a[maxn];void init (int n) {for (int i = 0; I <= N; i++) G[i].clear ();}    void Dfs (int u) {pre[u] = lowlink[u] = ++dfs_clock;    S.push (U);        for (int i = 0; i < g[u].size (); i++) {int v = g[u][i];            if (!pre[v]) {DFS (v);        Lowlink[u] = min (lowlink[u],lowlink[v]);        } else if (!sccno[v]) {Lowlink[u] = min (lowlink[u],pre[v]);        }} if (lowlink[u] = = Pre[u]) {scc_cnt++;            while (1) {int x = S.top ();            S.pop ();            SCCNO[X] = scc_cnt;        if (x = = u) break;    }}}void FIND_SCC (int n) {dfs_clock = scc_cnt = 0;    CLR (sccno,0);    CLR (pre,0);    for (int i = 1; I <= n; i++) {if (!pre[i]) DFS (i);    }}int in[maxn];int Out[maxn];int Main () {int n,m;    Freopen ("Data.in", "R", stdin);        while (cin>>n) {init (n);            for (int i = 1; I <= n; i++) {int m; while (scanf ("%d", &m), M) {G[i].            PB (m); }}//for (int i = 0; I <= scc_cnt;        i++) new_g[i].clear ();        FIND_SCC (n);        CLR (in,0);        CLR (out,0);        int Tmpa = 0,TMPB = 0;                for (int i = 1, i <= N; i++) {for (int j = 0; J < g[i].size (); + j) {int v = g[i][j];                    if (sccno[i]! = Sccno[v]) {out[sccno[i]]++;                in[sccno[v]]++;            }}} for (int i = 1; I <= scc_cnt; i++) {if (!out[i]) tmpb++;        if (!in[i]) tmpa++;    } cout<<tmpa<<endl<< (scc_cnt = = 1? 0:max (Tmpb,tmpa)) <<endl; }}

3. POJ 2186 Popular Cows

The topic gives some admiration, and requires the number of cows admired by all cows. Because the diagram is relatively large, so the first strong connectivity to the indentation, re-composition, after this is relatively simple, to ensure that the diagram connected to the case, we need to ensure that the degree of 0 of the component only one, if there are multiple, then there is no admiration between these components.

#include <set> #include <map> #include <list> #include <stack> #include <queue> #include <ctime> #include <cmath> #include <cstdio> #include <vector> #include <string> #include <cstdlib> #include <cstring> #include <iostream> #include <algorithm> #pragma comment (linker, " /stack:1024000000,1024000000 ") #define IT iterator#define PB (x) push_back (x) #define CLR (       A, B) memset (A,b,sizeof (a)) using namespace Std;typedef long long ll;typedef unsigned long long Ull;typedef vector<int> vint;typedef vector<ll> vll;typedef vector& lt;ull> vull;typedef set<int> sint;typedef set<ull> sull; const int MAXN = 10000 + 5;vint g[maxn];int pre[maxn],lowlink[maxn],sccno[maxn],dfs_clock,scc_cnt;stack<int> S; int a[maxn];void init (int n) {for (int i = 0; I <= N; i++) G[i].clear ();}    void Dfs (int u) {pre[u] = lowlink[u] = ++dfs_clock;    S.push (U);        for (int i = 0; i < g[u].size (); i++) {int v = g[u][i];            if (!pre[v]) {DFS (v);        Lowlink[u] = min (lowlink[u],lowlink[v]);        } else if (!sccno[v]) {Lowlink[u] = min (lowlink[u],pre[v]);        }} if (lowlink[u] = = Pre[u]) {scc_cnt++;            while (1) {int x = S.top ();            S.pop ();            SCCNO[X] = scc_cnt;        if (x = = u) break;    }}}void FIND_SCC (int n) {dfs_clock = scc_cnt = 0;    CLR (sccno,0);    CLR (pre,0);    for (int i = 1; I <= n; i++) {if (!pre[i]) DFS (i);    }}int out[maxn];int Cnt[maxn];int Main () {int n,m;    Freopen ("Data.in", "R", stdin);        while (cin>>n>>m) {init (n);            for (int i = 0; i < m; i++) {int A, B;            scanf ("%d%d", &a,&b); G[a].        PB (b); }//for (int i = 0;I <= scc_cnt;        i++) new_g[i].clear ();        FIND_SCC (n);        CLR (out,0);        CLR (cnt,0);            for (int i = 1; I <= n; i++) {cnt[sccno[i]]++;                for (int j = 0; J < G[i].size (); j + +) {int v = g[i][j];                if (sccno[i]! = Sccno[v]) {out[sccno[i]]++;        }}} BOOL flag = TRUE;        int k = 0;                for (int i = 1; I <= scc_cnt; i++) {if (k &&!out[i]) {flag = false;            Break        } if (!out[i]) k = i;        } if (flag) cout<<cnt[k]<<endl;    else cout<<0<<endl; }}

4. HDU 3861 The King ' s problem

After reading the topic, it is found that it should be the smallest path overlay, but two questions: the data range and the graph have loops. The first strong connection indentation, re-composition, so that the diagram becomes a DAG, in the application of the Hungary algorithm can find the answer.

#include <set> #include <map> #include <list> #include <stack> #include <queue> #include <ctime> #include <cmath> #include <cstdio> #include <vector> #include <string> #include <cstdlib> #include <cstring> #include <iostream> #include <algorithm> #pragma comment (linker, " /stack:1024000000,1024000000 ") #define IT iterator#define PB (x) push_back (x) #define CLR (       A, B) memset (A,b,sizeof (a)) using namespace Std;typedef long long ll;typedef unsigned long long Ull;typedef vector<int> vint;typedef vector<ll> vll;typedef vector& lt;ull> vull;typedef set<int> sint;typedef set<ull> sull; const int MAXN = 50000 + 5;vint g[maxn];int pre[maxn],lowlink[maxn],sccno[maxn],dfs_clock,scc_cnt;stack<int> S; int a[maxn];void init (int n) {for (int i = 0; I <= N; i++) G[i].clear ();}    void Dfs (int u) {pre[u] = lowlink[u] = ++dfs_clock;    S.push (U);        for (int i = 0; i < g[u].size (); i++) {int v = g[u][i];            if (!pre[v]) {DFS (v);        Lowlink[u] = min (lowlink[u],lowlink[v]);        } else if (!sccno[v]) {Lowlink[u] = min (lowlink[u],pre[v]);        }} if (lowlink[u] = = Pre[u]) {scc_cnt++;            while (1) {int x = S.top ();            S.pop ();            SCCNO[X] = scc_cnt;        if (x = = u) break;    }}}void FIND_SCC (int n) {dfs_clock = scc_cnt = 0;    CLR (sccno,0);    CLR (pre,0);    for (int i = 1; I <= n; i++) {if (!pre[i]) DFS (i);         }}vint new_g[maxn];int match[maxn];bool vis[maxn];bool findpath (int u) {for (int i = 0; i < new_g[u].size (); i++) {        int v = new_g[u][i];            if (!vis[v]) {vis[v] = 1;                if (match[v] = =-1 | | findpath (MATCH[V])) {Match[v] = u; RetUrn true; }}} return false;}    int Hungary () {int ans = 0;    CLR (match,-1);        for (int i = 1; I <= scc_cnt; i++) {CLR (vis,0);    if (Findpath (i)) ans++; } return ans;    int main () {//freopen ("data.in", "R", stdin);    int n,m;    int T;    cin>>t;        while (t--) {cin>>n>>m;        Init (n);            for (int i = 0; i < m; i++) {int A, B;            scanf ("%d%d", &a,&b); G[a].        PB (b);        }//for (int i = 0; I <= scc_cnt; i++) new_g[i].clear ();        FIND_SCC (n);        for (int i = 0; I <= scc_cnt; i++) new_g[i].clear ();                for (int i = 1, i <= N; i++) {for (int j = 0; J < g[i].size (); + j) {int v = g[i][j]; if (sccno[i]! = Sccno[v]) {New_g[sccno[i]].                PB (Sccno[v]);    }}} cout<<scc_cnt-hungary () <<endl; }}

5. HDU 3639 Hawk-and-chicken

The beginning or the old routine, the first strong connection contraction point, need to pay attention to the words of the re-composition of the problem need to reverse the construction of the edge, and then to each of the 0 points DFS, the maximum value can be.

#include <set> #include <map> #include <list> #include <stack> #include <queue> #include <ctime> #include <cmath> #include <cstdio> #include <vector> #include <string> #include <cstdlib> #include <cstring> #include <iostream> #include <algorithm> #pragma comment (linker, " /stack:1024000000,1024000000 ") #define IT iterator#define PB (x) push_back (x) #define CLR (       A, B) memset (A,b,sizeof (a)) using namespace Std;typedef long long ll;typedef unsigned long long Ull;typedef vector<int> vint;typedef vector<ll> vll;typedef vector& lt;ull> vull;typedef set<int> sint;typedef set<ull> sull; const int MAXN = 5;vint g[maxn];int pre[maxn],lowlink[maxn],sccno[maxn],dfs_clock,scc_cnt;stack<int> S;int A[maxn];void init (int n) {for (int i = 0; I <= N; i++) G[i].clear ();}    void Dfs (int u) {pre[u] = lowlink[u] = ++dfs_clock;    S.push (U);        for (int i = 0; i < g[u].size (); i++) {int v = g[u][i];            if (!pre[v]) {DFS (v);        Lowlink[u] = min (lowlink[u],lowlink[v]);        } else if (!sccno[v]) {Lowlink[u] = min (lowlink[u],pre[v]);        }} if (lowlink[u] = = Pre[u]) {scc_cnt++;            while (1) {int x = S.top ();            S.pop ();            SCCNO[X] = scc_cnt;        if (x = = u) break;    }}}void FIND_SCC (int n) {dfs_clock = scc_cnt = 0;    CLR (sccno,0);    CLR (pre,0);    for (int i = 0; i < n; i++) {if (!pre[i]) DFS (i);    }}vint new_g[maxn];int rec[maxn];int out[maxn];int lab[maxn];bool vis[maxn];int cnt;int dp (int u) {Vis[u] = true;    CNT + = Lab[u];        for (int i = 0; i < new_g[u].size (); i++) {int v = new_g[u][i];        if (!vis[v]) {DP (v); }} return cnt;} int main () {//freopen ("Data.in", "R", stdin);    int n,m;    int T;    cin>>t;        for (int t = 1; t <= t; t++) {cin>>n>>m;        Init (n);            for (int i = 0; i < m; i++) {int A, B;            scanf ("%d%d", &a,&b); G[a].        PB (b);        } FIND_SCC (n);        CLR (out,0);        CLR (lab,0);        for (int i = 0; I <= scc_cnt; i++) new_g[i].clear ();            for (int i = 0; i < n; i++) {lab[sccno[i]]++;                for (int j = 0; J < G[i].size (); j + +) {int v = g[i][j]; if (sccno[i]! = Sccno[v]) {New_g[sccno[v]].                    PB (Sccno[i]);                out[sccno[i]]++;        }}} CLR (rec,0);        int ans = 0;                for (int i = 1; I <= scc_cnt; i++) {if (!out[i]) {CLR (vis,0);                CNT = 0;                Rec[i] = DP (i);            ans = max (ans,rec[i]); }}/*for (int i = 0; I < n;        i++) {cout<<sccno[i]<< "";        } cout<<endl;        for (int i = 1; I <= scc_cnt; i++) {cout<<rec[i][0]<< "" <<rec[i][1]<<endl;        }*/printf ("Case%d:%d\n", t,ans-1);        BOOL flag = FALSE;                for (int i = 0; i < n; i++) {if (rec[sccno[i]] = = ans) {if (flag) printf ("%d", I);                    else {printf ("%d", I);                Flag = true;    }}} printf ("\ n"); }}

Connect Graph Exercise Summary

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.