poj 2942 Knights of the Round Table (bis point connected component)

Topic links: http://poj.org/problem?id=2942

 

Reprinted from: https://blog.csdn.net/lyy289065406/article/details/6756821

 

Roughly meaning of the questions:

King Arthur knights meeting to be held at the round table, in order not to lead to conflict between the Knights and the agenda of the conference can make satisfactory result, each must have the following requirements for the Knights attending the meeting before the meeting:

1, two knights hate not sit directly mutually adjacent two positions;

2, the number of knights to attend the meeting must be odd, which is to allow voting issue can have results.

 

If there is some knight appears unable to attend all the meetings (such as the knight hated all the other knights), the King Arthur for world peace will be forced to reject him out of the Knights.

       Now ready to go to the meeting a given number of knights n, m and then given to hatred (between a two knights represented the hated each other), at least ask how many knights of King Arthur to be removed in order to successfully meeting?

 

Note: 1, given a certain hate relationship is bidirectional, unidirectional hate relationship does not exist.

2, because it is a round table, each attended by around knight must have exactly two knights. That is, on both sides of each seat they are bound to have a knight of the Knights.

3, a knight can not be met, that there are at least three knights only possible meeting.

 

Problem-solving ideas:

Very strong comprehensive view of the topic, said that before solving the report, I suggest that you first be aware of the following, otherwise this question is very difficult to do things:

 

1, defined Complement of

Definition 2, bis connected components

Definition 3 bipartite graph

4, the definition of odd circle

5, it is determined whether a graph is a bipartite graph Method: Cross staining

6, Tarjan algorithm

In order to facilitate everyone to do that, I say something here about the first six knowledge of the above points, I note the following popular understanding, instead of the standard definition:

1, FIG Complement

Complement graph G ~ G is the edge of the original G in FIG deleted in its entirety, that are not present even on all sides.

 

2, the communication component bis

       Briefly, if no communication is double, then delete at least two nodes to the graph G such that G graph G is not connected. In other words, an arbitrary graph G is more than two paths are present between the two connected nodes (note: the path is not directly connected to the edge means), then the connected component is bis means no sub directed graph G, G ' It is a double communication.

 

3, FIG bipartite

       Bipartite graph known as a bipartite graph, which is defined Baidu Encyclopedia, look bipartite graph can look like, and do not need to understand in depth. Then carefully learn it when the students do not know how to do until the subject bipartite graph.

 

4, odd cycle

With a line link up to an odd number of points, the resulting closed loop is the odd cycles. In fact, there are an odd number is odd circle ring vertices.

 

5, cross-dyeing method determines bipartite graph

       Initialize all nodes as a colorless (color 0) state, with a traverse DFS graph G, while on the way to the junction staining (staining only 2 colors), attention traversed nodes can then traverse repainted. Let traversed at some point of the node t dyeing, we found side s-> t s to another node stained, and the color s and t are in the same color of dye, then certainly not bipartite graph G of FIG. .

       This is because imagine bipartite graph is like the two sides of the river there are two rows of nodes, each of coloring and once again cross the river, then the node must be the same color on the same side. Once the opposite side nodes have the same color, it can not be described bipartite graph G of FIG.

 

6, Tarjan algorithm

       I hope that we mainly to learn about the basic principles of the algorithm, especially DFN Low arrays and arrays, as well as what is deep search tree, what tree branch side, what is the backward side.

Tarjan algorithm seeking to learn about the process of cutting points (note that I suggest that you do not use the above Tarjan algorithm to solve the problem point cut, but not so that we do not look at the process of seeking it cut point), because the process is seeking double connected component key.

       And if you want a good understanding of Tarjan algorithm for finding the point of cutting process, it is recommended to see Liu Rujia of "algorithm art and Informatics Contest" P285 page, and then do what POJ1523 (purely seeking a cut point of the title) to find the point of feeling.

Liu Rujia long understood the way, look Tarjan algorithm is very easy to understand.

 

With the above knowledge support, you can begin the problem-solving:

1, using the configuration of FIG hatred of m G, then G has two edges that connect points represent the two knights hate each.

2, showing the structure complement graph G G ~, ~ G in the FIG edges that connect two points represents the two knights can sit adjacent positions.

3, in FIG. ~ G, there may be some degree point <= 1, that can take up the next Knight Knight another, on both sides of each seat k Knight seat according to claim roundtable are bound have a knight (k == 2 degrees), then we believe that these degrees <= 1 point is isolated or is simply connected, that is to say they are not round table "ring" within.

 

For example, a map, we use configuration of FIG complement graph G ~ G after, apparently Knight of 1 = 0, is isolated and does not communicate; Knight 1 = 5 degrees, he is simply connected; Knight {2,3 , 4} constitute one pair of connected components, they are the round table meeting "ring." Clearly the degree of <= 1 conditions, Knights and Knight 51 are outside the ring, does not meet present at the meeting, King Arthur in order to maintain world peace will naturally these two people expelled the Knights.

 

4, the question now is, how do we know which knights outside the ring?

We can put into question, how do we know what the Knights in the ring? Obviously all the nodes in the ring is double communication, we might bis connected component algorithm by Tarjan. Note that, in FIG. ~ G might make several bis connected subgraph, i.e. it may have more than one set of connected components of a double, double and Tarjan algorithm is a set of connected components of a group of seeking out, so each connected component to obtain a double set of we must immediately deal with a group.

The following components are processed dual communication for a group.

 

5, within the knights bis communication component (in the ring), it does not follow that it would be attended, because if the double communication component where the knight, the ring is not a vertex odd (odd cycle), but an even number ring vertex, then all of the knight in the connected components have been expelled bis Arthur.

 

6, then how to judge one pair of connected components is odd cycle it?

First, we have to accept two theorems, want to know the certification process can surf the Internet, do not prove here:

(1) If some of the vertices in a connected component in an odd double circle (i.e. bis communication component contained odd cycle), then the other vertices that are connected components of a double-odd circle;

(2) If the connected component comprising one pair of odd cycle, he must not be a bipartite graph. Opposite is also true, this is a necessary and sufficient condition.

Because the dual communication component is a view to determining whether it is an odd double circle communication component, determining only whether the connected component is a bis bipartite graph, and to judge whether it is a bipartite graph of FIG, staining it with a cross!

 

7, it is clear that all knights in odd circles, are allowed to attend the meeting, and because there may be some knight allowed to attend more than one meeting (that is, they are more common point of the two odd circle), then in order to avoid double counting the number, which when we judged that the Knights allowed to attend the meeting, and let them make a mark (only a knight of the same mark). Finally, when the end of the Tarjan algorithm, we look at the statistics of how many persons are marked, and then subtracting the total number of these people, the number is to be removed in the rest of King Arthur.

 Code:

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#define inf 0x3f3f3f3f
using namespace std;
typedef long long ll;
const int maxn=1010;
const int maxm=2000010;
struct node{
    int u,v,nxt;
}e[maxm];
int h[maxn],color[maxn];
bool inst[maxn],ok[maxn],flag[maxn];
int st[maxn],dfn[maxn],low[maxn],tp[maxn];
int cnt,top,tot,num,scc,n,m; 
bool g[maxn][maxn];

void add(int u,int v)
{
    e[cnt].u=u,e[cnt].v=v;
    e[cnt].nxt=h[u],h[u]=cnt++;
}

void init()//初始化 
{
    memset(h,-1,sizeof(h));
    memset(inst,0,sizeof(inst));
    memset(dfn,0,sizeof(dfn));
    memset(flag,0,sizeof(flag));
    memset(g,0,sizeof(g));
    cnt=tot=top=num=scc=0;
}

bool dfs(int x,int cl)//染色,判断是否是二分图 
{
    color[x]=cl;
    for(int i=h[x];i!=-1;i=e[i].nxt)
    {
        int v=e[i].v;
        if(!ok[v]) continue;
        if(color[v]==cl) return false;
        if(!color[v]&&!dfs(v,-cl)) return false;
    }
    return true;
}

void tarjan(int u,int pre) 
{
    low[u]=dfn[u]=++tot;
    st[top++]=u;
    inst[u]=1;
    int pre_cnt=0;
    for(int i=h[u];i!=-1;i=e[i].nxt)
    {
        int v=e[i].v;
        if(v==pre&&pre_cnt==0)
        {
            pre_cnt++;
            continue;
        }
        if(!dfn[v])
        {
            tarjan(v,u);
            low[u]=min(low[u],low[v]);
            if(dfn[u]<=low[v])//找到u是割点 
            {
                memset(ok,0,sizeof(ok));
                int t;
                scc=0; 
                do{
                    t=st[--top];
                    inst[t]=0;
                    ok[t]=1 ; // tag for dyeing 
                    TP [SCC ++] = T; // save communication component by double 
                } the while (T =! V); 
                OK [U] = 1 ; 
                Memset (Color, 0 , the sizeof (Color)) ;
                 IF (! DFS (U, . 1 )) // not a bipartite graph is odd cycle, the marker may meet knight 
                { 
                    in flag [U] = . 1 ;
                     the while (scc-- ) 
                        in flag [TP [SCC]] = 1 ;  
                }
            }
        }
        else if(inst[v])
            low[u]=min(low[u],dfn[v]);
    }
}

void solve()
{
    int ans=n;
    for(int i=1;i<=n;i++)
        if(!dfn[i])
            tarjan(i,-1);
    for(int i=1;i<=n;i++)
        if(flag[i])
            ans--;
    printf("%d\n",ans);
}

int main()
{
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        if(!n&&!m) break;
        init();
        int u,v;
        for(int i=0;i<m;i++)
        {
            scanf("%d%d",&u,&v);
            g[u][v]=g[v][u]=1;
        }
        for(int i=1;i<=n;i++)//建补图 
            for(int j=1;j<=n;j++)
                if(i!=j&&!g[i][j])
                    add(i,j);
        solve();
    }
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/xiongtao/p/11276991.html