Complete Tripartite

D - Complete Tripartite

Ideas: This title is a coloring problem. Understanding the problem almost half written. At first discrete state would like to use to store each point, that point what it is connected, but are helpless, too many points, long longwithin the scope of definitely save endless, so think of using pythonto write, but there is no py very skilled ..... just gave up.

It should be noted:

To count total number of color, otherwise the situation will miss only two colors, this is the output -1of. There exist before the edge of the star when I remember to open twice.

Code:

// Created by CAD on 2019/10/2.
#include <bits/stdc++.h>
#define mst(name, value) memset(name,value,sizeof(name))
using namespace std;

const int maxn=3e5+5;
struct edge{
    int to,next;
}e[maxn<<1];
int cnt,head[maxn],color[maxn],vis[maxn],n,m,blo;

void add(int u,int v)
{
    e[++cnt].to=v;
    e[cnt].next=head[u];
    head[u]=cnt;
}
bool check(int x)
{
    if(++blo==4) return false;
    color[x]=blo;
    int num=0;
    for(int i=head[x];i;i=e[i].next)
        vis[e[i].to]=1,num++;
    for(int i=1;i<=n;++i)
    {
        if(i==x||vis[i])    continue;
        int cnt2=0;
        color[i]=blo;
        for(int j=head[i];j;j=e[j].next)
        {
            if(!vis[e[j].to]) return false;
            cnt2++;
        }
        if(cnt2!=num)   return false;
    }
    mst(vis,0);
    return true;
}
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cin>>n>>m;
    for(int i=1,u,v;i<=m;++i)
        cin>>u>>v,add(u,v),add(v,u);
    for(int i=1;i<=n;++i)
        if(!color[i])
        if(!check(i))   return puts("-1");
    if(blo!=3)  return puts("-1");
    for(int i=1;i<n;++i)
        cout<<color[i]<<" ";
    cout<<color[n]<<endl;
    return 0;
}

Guess you like

Origin www.cnblogs.com/CADCADCAD/p/11617446.html