Bzoj 1098 ([POI2007] office building biu-linked list optimization dfs) __ List

Source: Internet
Author: User
Tags first row time limit
1098: [POI2007] office building BiuTime limit:20 Sec Memory limit:162 MB
submit:640 solved:254
[Submit] [Status] [Discuss] DescriptionSome employees, each of whom knows the number of others. If a knows B's, B must know the telephone number of a. The boss wants to divide the employees into as many office buildings as possible. However, if A and B are not in the same office, A and B must have the phone number of each other. Ask these employees to be able to be divided into the number of office buildings, and in ascending order to output the number of employees in each office. InputThe first row contains two integers N (2<=n<=100000) and M (1<=m<=2000000). The clerk is numbered 1,2,......, N. The following m lines, each containing two positive numbers a and B (1<=a<b<=n), indicating that staff A and B have each other's telephone number. Outputcontains two lines. The first line contains a number s, which indicates the number of office buildings that the FGD can place in the staff. The second line contains the number of s from small to large, separated by spaces in the middle, indicating the number of employees arranged in each office. Sample Input7 16
1 3
1 4
1 5
2 3
3 4
4 5
4 7
4 6
5 6
6 7
2 4
2 7
2 5
3 5
3 7
1 7

Sample Output3
1 2 4

HINT

FGD can arrange staff 4 into an office building, staff 5 and staff 7 arranged into the 2nd office Building, the others into the Office Building No. 3rd.

First to find the original map, the request to convert to-different office buildings are not connected.

The idea is to find the complement of the connected block-obviously the complement is a dense graph, will t

The problem is to use the linked list to optimize DFS.

Normally, we would enumerate from Node 1 to node N, and we would tle

All you can do is set up a list, and when an element is gone, delete it.

This efficiency translates to O (n+m)

Ps: This problem with CIN and scanf efficiency is much worse



#include <cstdio> #include <cstring> #include <cstdlib> #include <iostream> #include < algorithm> #include <functional> #include <cmath> #include <queue> #include <list> using
namespace Std;
#define MAXN (100000+10) #define MAXM (2000000+10) int n,m;
int size=0,edge[maxm*2],pre[maxm],next[maxm*2]; struct link {int pre,next;}
L[MAXN];
	void del (int x) {l[l[x].pre].next=l[x].next;	
L[l[x].next].pre=l[x].pre;
} queue<int> Q;
	void Addedge (int u,int v) {edge[++size]=v;
	Next[size]=pre[u];
Pre[u]=size;
} bool B[MAXN],Q_B[MAXN];
int z[maxn],siz_z=0;
	void BFs () {memset (b,0,sizeof (b));
	memset (q_b,0,sizeof (Q_b));
		while (l[0].next) {int now=l[0].next,ans=1;
		Del (now);
		Q.push (now); q_b[now]=1; cout<<now<<endl;
			00x while (!q.empty ()) {int U=q.front (); Q.pop ();
			for (int p=pre[u];p; p=next[p]) b[edge[p]]=1;
					for (int i=l[0].next;i;i=l[i].next) {if (!b[i]&&!q_b[i]) {q_b[i]=1; Q. push (i);//cout<<v<<endl;
					0XX ans++;
				Del (i);
		for (int p=pre[u];p; p=next[p]) b[edge[p]]=0;
		cout<<endl;
	} Z[++siz_z]=ans;
	int main () {//Freopen ("Bzoj1098.in", "R", stdin);
	memset (pre,0,sizeof (pre));
	scanf ("%d%d", &n,&m);
		for (int i=1;i<=m;i++) {int u,v;
		scanf ("%d%d", &u,&v);
	Addedge (u,v); Addedge (V,u);
	for (int i=1;i<=n;i++) {l[i-1].next=i;l[i].pre=i-1;} l[n].next=0;	
	BFS ();
	Sort (z+1,z+1+siz_z);
	printf ("%d\n", siz_z);
	if (siz_z>0) {for (int i=1;i<siz_z;i++) printf ("%d", Z[i]);p rintf ("%d", z[siz_z]);
return 0; }


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.