"Bzoj 1854" [Scoi2010] Game (Hungarian algorithm)

Source: Internet
Author: User
1854: [Scoi2010] GamesTime Limit:5 Sec Memory limit:162 MB
submit:4521 solved:1764
[Submit] [Status] [Discuss] DescriptionLXHGWW has recently been fascinated by a game in which he has a lot of equipment, each with 2 attributes, the values of which are expressed in numbers between [1,10000]. When he uses some kind of equipment, he can only use one of the properties of the equipment. And each device can be used at most once. At the end of the game, LXHGWW encountered the ultimate boss, the ultimate boss is very strange, attack his equipment used by the attribute value must start from 1 continuous incremental attack, in order to damage the boss. In other words, at first, LXHGWW can only use a device with a property value of 1 to attack the boss, and then only use a device with a property value of 2 to attack the boss, and then only use a property value of 3 of the equipment attack boss ... And so on Now LXHGWW want to know how many times he can attack bosses in succession. InputThe first line of input is an integer n, indicating that LXHGWW has n equipment next n lines, is a description of the n equipment, 2 numbers per line, representing the 2 property values of the first equipment OutputOutputs a row, including 1 digits, that represents the maximum number of consecutive attacks LXHGWW can have. Sample Input3
1 2
3 2
4 5
Sample Output2
HINT

"Data Range"
For 30% of data, ensure n < =1000
For 100% of data, ensure n < =1000000
Source

Day1 [submit][status][discuss]

"Solving" "two-point graph maximum matching"

"Hungarian algorithm naked problem, put the equipment on one side, the property value of the equipment on the other side, equipped with each property value to the corresponding equipment edge." Since the equipment value from the beginning of each increment, so, the first equipment to match the attribute value I, so every time I can determine whether the equipment matches the attribute value I, if not, then the answer is i-1, if you can meet, continue to judge the next "

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
using namespace std;
int A[2000010],nxt[2000010],p[1000010],tot;
int n,maxn,link[2000010],f[2000010];
inline void Add (int x,int y)
{
	tot++; a[tot]=y; nxt[tot]=p[x]; p[x]=tot;
}
BOOL Check (int x,int u)
{for
	(int i=p[x];i!=-1;i=nxt[i])
	 if (link[a[i]]!=u)
	  {
	  	link[a[i]]=u ;
	  	if (f[a[i]]==-1| | Check (f[a[i]],u))
	  	 {
	  	 	f[a[i]]=x;
	  	 	return 1;
		   }
	  }
	return 0;
}
int main ()
{
	int i,j;
	memset (P,-1,sizeof (P));
	memset (F,-1,sizeof (f));
	memset (nxt,-1,sizeof (NXT));
	scanf ("%d", &n);
	for (I=1;i<=n;++i)
	 {
	 	int x, y;
	 	scanf ("%d%d", &x,&y);
	 	Add (x,i); Add (y,i);
	 	Maxn=max (MAXN,X); Maxn=max (maxn,y);
	 }
	for (I=1;i<=maxn;++i)
	 if (!check (i,i))
	  {
	  	printf ("%d\n", i-1);
	  	return 0;
	  }
	printf ("%d\n", MAXN);
	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.