POJ 1325 Kőnig's Theorem

http://poj.org/problem?id=1325

The minimum vertex cover bipartite graph

According to the Kőnig's theorem

The maximum matching problem and the minimum vertex cover problem are equivalent

so the problem is easy

just using the Hungarian algorithm to solve

but noticed that the machine is in 0 status

so when you build the graph through the adjacency list

you should not to add the edge with 0 vertex

code of AC:

#include<iostream>
#include<cstring>
using namespace std;
const int N=2e3+10;
int head[N],ver[N],Next[N],v[N],match[N];
int tot=0;
void add(int x,int y){
	ver[++tot]=y;
	Next[tot]=head[x];
	head[x]=tot;
}
bool dfs(int x){
	for(int i=head[x];i;i=Next[i]){
		int y=ver[i];
		if(v[y]) continue;
		v[y]=1;
		if(match[y]==0||dfs(match[y])){
			match[y]=x;
			return 1;
		}
	}
	return 0;
}
int main(){
	int n,m,k,a,b,c;
	while(cin>>n&&n){
		cin>>m>>k;
		tot=0;
		for(int i=0;i<N;++i){
			head[i]=ver[i]=Next[i]=v[i]=match[i]=0;
		}
		for(int i=1;i<=k;++i){
			cin>>a>>b>>c;
			if(b*c)
				add(b,c);
		}
		int ans=0;
		for(int i=0;i<n;++i){
			memset(v,0,sizeof v);
			if(dfs(i)) ++ans;
		}
		cout<<ans<<endl;
	}
}


阅读更多
换一批

Lagrange's Four-Square Theorem

09-11

The fact that any positive integer has a representation as the sum of at most four positive squares (i.e. squares of positive integers) is known as Lagrange's Four-Square Theorem. The first published proof of the theorem was given by Joseph-Louis Lagrange in 1770. Your mission however is not to explain the original proof nor to discover a new proof but to show that the theorem holds for some specific numbers by counting how many such possible representations there are.nFor a given positive integer n, you should report the number of all representations of n as the sum of at most four positive squares. The order of addition does not matter, e.g. you should consider 4^2 + 3^2 and 3^2 + 4^2 are the same representation.nnFor example, let's check the case of 25. This integer has just three representations 1^2+2^2+2^2+4^2, 3^2 + 4^2, and 5^2. Thus you should report 3 in this case. Be careful not to count 4^2 + 3^2 and 3^2 + 4^2 separately.nnnInputnnThe input is composed of at most 255 lines, each containing a single positive integer less than 2^15, followed by a line containing a single zero. The last line is not a part of the input data.nnnOutputnnThe output should be composed of lines, each containing a single integer. No other characters should appear in the output.nnThe output integer corresponding to the input integer n is the number of all representations of n as the sum of at most four positive squares.nnnSample Inputnn1n25n2003n211n20007n0nnnSample Outputnn1n3n48n7n738

没有更多推荐了,返回首页