1006: [hnoi2008] Time Limit: 20 sec memory limit: 162 MB
Submit: 1788 solved: 775
[Submit] [Status] Description
K is a country keen on triangles, and even people only like the triangle principles. they believe that the triangular relationship: AB, BC, and Ca are concise and efficient. in order to consolidate the triangular relationship, K countries prohibit the existence of four-edge relations, five-edge relations, and so on. the so-called n-edge relationship refers to N people a1a2... there are only N pairs of cognitive relationships between an: (a1a2) (a2a3 )... (ana1. for example, four members of ABCD know each other, namely AB, BC, CD, and DA, but AC and BD do not. during the national competition, in order to prevent disadvantages, it was stipulated that any one of the people who knew each other should not be in the same team. The king knew at least how many teams could be divided.
Input
The first line has two integers, N and M. 1 <= n <= random, 1 <= m <= 1000000. It indicates there are n people, M is a relationship with each other, and then input a friend in each line in M.
Output
The output is an integer. The minimum number of teams can be divided.
Sample input4 5
1 2
1 4
2 4
2 3
3 4
Sample output3hint
One solution (1, 3) (2) (4)
Source
Really surprised this strange figure there is a pile of algorithms, the portal: http://wenku.baidu.com/link? Dqd1T3C4o5DAjuPwQ_v44DnCHtQn5kxI-HoSsDb_QqSJQ0MeByzYYmpGSDSYXjPTsGQF9nz1AliKkp _-tvsxftzqsdo3vhfeed0yigbegvc
This question uses the perfect sequence elimination of the string graph. Although I have not understood the most general MCS algorithm, it should be easy to back up, and only YY is available in the test room.
#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<queue>using namespace std;#define MAXN 10010#define MAXE 2100000inline int nextInt(){ int x=0; char ch; while (ch=getchar(),ch<‘0‘||ch>‘9‘); do x=x*10+ch-‘0‘; while (ch=getchar(),ch<=‘9‘&&ch>=‘0‘); return x;}struct Edge{ int np; Edge *next;}E[MAXE],*V[MAXN];int tope=-1;inline void addedge(int x,int y){ E[++tope].np=y; E[tope].next=V[x]; V[x]=&E[tope];}int n,m;bool vis[MAXN];int cnt[MAXN];int seq[MAXN];int pos[MAXN];bool operator <(pair<int,int> p1,pair<int,int> p2){ return p1.second>p2.second;}struct cmp_c{ bool operator ()(pair<int,int> p1,pair<int,int> p2) { return p1.second<p2.second; }};priority_queue<pair<int,int>,vector<pair<int,int> >,cmp_c > Q;bool fl[10000];int col[MAXN];int main(){ freopen("input.txt","r",stdin); //freopen("output.txt","w",stdout); int i,j,k; int x,y,z; scanf("%d%d",&n,&m); for (i=0;i<m;i++) { x=nextInt(); y=nextInt(); addedge(x,y); addedge(y,x); } int now; int rk=n+1; Edge *ne; Q.push(make_pair(1,cnt[1])); while (!Q.empty()) { now=Q.top().first; Q.pop(); if (vis[now])continue; vis[now]=true; seq[--rk]=now; pos[now]=rk; for (ne=V[now];ne;ne=ne->next) { if (vis[ne->np])continue; cnt[ne->np]++; Q.push(make_pair(ne->np,cnt[ne->np])); } } Edge *ne2; int mx=0; int ans=0; for (i=n;i>=1;i--) { now=seq[i]; memset(fl,0,sizeof(fl[0])*(mx+1)); for (ne=V[now];ne;ne=ne->next) { if (pos[ne->np]<i)continue; fl[col[ne->np]]=true; mx=max(mx,col[ne->np]); } for (j=1;j<=mx+1;j++) { if (!fl[j]) { col[now]=j; ans=max(ans,j); break; } } } printf("%d\n",ans);}