The main idea of the topic portal
title:
Given a graph composed of triangles, color each point, the color of each point and adjacent points cannot be the same, and ask how many colors to use at least.
Thinking process & specific practice:
Please refer to the chord diagram and interval diagram - cdq
Code:
#include <bits/stdc++.h>
using namespace std;
const int maxn=1e4+100,maxm=1e6+100;
struct stu
{
int to,next;
}road[2*maxm]; int first[maxn],cnt=0;
int label[maxn],book[maxn],col[maxn];
int n,m;
queue <int> q;
void addedge(int x,int y)
{
road[++cnt].to=y;
road[cnt].next=first[x];
first[x]=cnt;
}
void mcs()
{
for(int i=1;i<=n;i++) label[i]=0;
for(int i=n;i>=1;i--)
{
int maxx=-1,mark;
for(int j=1;j<=n;j++) if(label[j]>=maxx&&!book[j]) { maxx=label[j];mark=j; }
q.push(mark);
book[mark]=1;
for(int i=first[mark];i;i=road[i].next)
{
int to=road[i].to;
if(!book[to]) label[to]++;
}
}
}
int main()
{
scanf("%d%d",&n,&m);
for(int i=1;i<=m;i++)
{
int x,y;
scanf("%d%d",&x,&y);
addedge(x,y);addedge(y,x);
}
mcs();
int cnt=0;
while(!q.empty())
{
int now=q.front();q.pop();
for(int i=1;i<=cnt;i++) book[i]=0;
for(int i=first[now];i;i=road[i].next)
{
int to=road[i].to;
book[col[to]]=1;
}
bool flag=0;
for(int i=1;i<=cnt;i++) if(!book[i]) { flag=1;col[now]=i;break; }
if(!flag) { cnt++;col[now]=cnt; }
}
printf("%d\n",cnt);
return 0;
}