[BZOJ-1006 & Luo Gu P3196] [HNOI2008] wonderful country - [Coloring - maximum potential MCS algorithm]

Luo Gu: Time limit 1.00s  memory limit 125.00MB

Time Limit: 20 Sec  Memory Limit: 162 MB

Topic links: https://www.lydsy.com/JudgeOnline/problem.php?id=1006

Luo Gu: https://www.luogu.com.cn/problem/P3196

Description

  K State is a keen triangle country, even people exchanges only love triangle principle they believe that triangle: namely AB mutual understanding, BC know each other, CA
. Know each other, is simple and efficient in order to consolidate the triangular relationship, K State is prohibited the presence of the four sides of the relationship, five-sided relationship and so-called N-sided relationship, refers to N individual A1A2
exist only between ... An N to understand the relationship between: (A1A2) (A2A3) ... (AnA1), and no other understanding of the relationship, such as four-sided relationship refers to the four ABCD AB, BC, C
D, DA to know each other, and AC, BD do not know when the game all the people, in order to prevent cheating, the provisions of any pair of people may not know each other in a team, King knows phase,
the minimum number of detachment can be divided.

Input

  The first line of two integers N, M. 1 <= N <= 10000,1 < = M <= 1000000. N expressed individuals, understanding the relationship between M Next M lines each pair of input Points
Friends

Output

  Output an integer, the minimum number of teams can be divided

Sample Input

4 5
1 2
1 4
2 4
2 3
3 4

Sample Output

3

HINT

  A solution (3) (2) (4)


 

emmm, draw a map to see to know is that it requires minimal coloring of graphs, for a graph coloring, adjacent dots not the same color, at least ask how many colors you can use.

Here's the biggest potential algorithm, the specific process is as follows:

1. beginning of each point potential is 0, point out any deleted.

2. The potential of 1 point, and deleting the point of attachment, remove the maximum potential point deletion.

3. Repeat 2 until all points are removed.

So there will be a deleted sequence, and this sequence appeared delete a number of different potential, the minimum number of colors is how much.

In the second test point Luo Valley, then get caught in time, open the optimization of oxygen like, BZOJ not the problem.

The following is the AC codes:

#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>
using namespace std;

const int mac=1e6+10;
const int mac2=1e4+10;

struct node
{
    int to,next,w;
}eg[mac<<1];
int head[mac2],cnt=0,vis[mac2],color[mac2],point[mac2];
struct pot
{
    int val,id;
    bool operator <(const pot&a)const{
        return val<a.val;
    }
};

void add(int u,int v)
{
    eg[++cnt]=node{v,head[u],0};
    head[u]=cnt;
}

int main()
{
    int n,m;
    scanf ("%d%d",&n,&m);
    memset(head,-1,sizeof head);
    for (int i=1; i<=m; i++){
        int u,v;
        scanf ("%d%d",&u,&v);
        add(u,v);add(v,u);
    }
    priority_queue<pot>q;
    q.push(pot{0,1});
    int ans=0;
    while (!q.empty()){
        pot now=q.top();
        q.pop();
        if (vis[now.id]) continue;
        vis[now.id]=1;
        int u=now.id;
        if (!color[now.val]) ans++,color[now.val]=1;
        for (int i=head[u]; i!=-1; i=eg[i].next){
            int v=eg[i].to;
            if (vis[v]) continue;
            point[v]++;
            q.push(pot{point[v],v});
        }
    }
    printf("%d\n",ans);
    return 0;
}

 

 

 

Guess you like

Origin www.cnblogs.com/lonely-wind-/p/12191171.html