The perfect elimination sequence of the non-direction graph to judge the chord graph ZOJ 1015 Fish Net

Source: Internet
Author: User
Tags ord

ZOJ1015

Test instructions Brief Description: Given an image without a direction, determine if there is a loop with a length greater than 3, and there is no chord on it (the edge of the connection ring on the different two points and not on the ring).

The proposition is equivalent to the existence of a perfect elimination sequence for the graph.

The so-called perfect elimination sequence: in the vi,vi+1,... vn VI and after the adjacent points with the vi form a regiment (complete sub-map).

The MCS algorithm for the perfect elimination sequence. In reverse order, the point labeled as I appears in item I of the sequence. For each vertex I, maintain the label Label[i], which represents the degree of the label, each time you select the largest point label. Use heap acceleration.

After the perfect elimination sequence is obtained, it can be concluded that the sequence should be judged by its legality.

In the vi,vi+1,... vn derived sub-chart found with vi adjacent to the lowest mark (the smallest) point, set as VJ, and then check whether VJ and each of the neighboring points of the VI adjacent.

#include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #include <cmath > #include <vector> #include <queue> #include <algorithm> #include <set> #define CLR (x, y) memset (x,y,sizeof (x)) using namespace Std;const int maxn=1010;vector<int> construct (int n,vector<int> adj[ MAXN]) {static int rank[maxn],label[maxn]; CLR (rank,-1); CLR (label,0); priority_queue<pair<int,int> > Heap  ;  for (int i=1;i<=n;i++) Heap.push (Make_pair (0,i));   for (int i=n-1;i>=0;i--) {while (true) {int u=heap.top (). Second;heap.pop ();    if (rank[u]==-1) {rank[u]=i; for (VECTOR&LT;INT&GT;:: Iterator Iter=adj[u].begin (); Iter!=adj[u].end (); ++iter) {if (rank[*iter]==-1) {LA      bel[*iter]++; Heap.push (Make_pair (Label[*iter],*iter));}} Break;}}} vector<int> result (n);  for (int i=1;i<=n;i++) {result[rank[i]]=i;} return result;} BOOL Check (int n,vector<int>adj[maxn],vector<int> ord) {static bool MarK[MAXN]; static int RANK[MAXN]; for (int i=0;i<n;i++) rank[ord[i]]=i; CLR (mark,0);     for (int i=0;i<n;i++) {vector<pair<int,int> >tmp; For (Vector<int>::iterator Iter=adj[ord[i]].begin (); Iter!=adj[ord[i]].end (); ++iter) if (!mark[*iter]) Tmp.push_back (Make_pair (rank[*iter],*iter)); Sort (Tmp.begin (), Tmp.end ());  if (Tmp.size ()) {int u=tmp[0].second;set<int> Tmpadj; For (Vector<int>::iterator Iter=adj[u].begin (), Iter!=adj[u].end (); ++iter) {Tmpadj.insert (*iter);} for (int i=1 ;i< (int) tmp.size (); ++i) {if (!tmpadj.count (Tmp[i].second)) return false;}} Mark[ord[i]]=true;} return true;} BOOL Is_chordal (int nodecount,vector<pair<int,int> >edges) {int n=nodecount; vector<int>adj[maxn]; for (int i=0;i<=n;i++) adj[i].clear (); For (vector<pair<int,int> >::iterator iter=edges.begin (); Iter!=edges.end (); ++iter) {Adj[iter->first  ].push_back (Iter->second); Adj[iter->second].push_back (Iter->first);} Return Check (N,adj,construct (N,adj));}  int main () {int n,m; while (scanf ("%d%d", &n,&m)) {if (n==0&&m==0) return 0;  Vector<pair<int,int> >ed;   for (int i=0;i<m;i++) {int A, B;   scanf ("%d%d", &a,&b); Ed.push_back (Make_pair (b));} if (Is_chordal (n,ed)) printf ("perfect\n\n"); else printf ("imperfect\n\n");} return 0;}

The perfect elimination sequence also has a wide range of applications that can be supplemented later.

The perfect elimination sequence of the non-direction graph to judge the chord graph ZOJ 1015 Fish Net

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.