ZOJ-2362 Beloved Sons 贪心 | KM
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2362
裸的匹配问题,直接KM,就算是O(n^4)的KM也不会超。当然注意到题目中左边的点到右点所连的边的权值是一样的,所以完全可以贪心拍个序,然后找增广路。。。
//STATUS:C++_AC_250MS_848KB
#include <functional>
#include <algorithm>
#include <iostream>
//#include <ext/rope>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <numeric>
#include <cstring>
#include <cassert>
#include <cstdio>
#include <string>
#include <vector>
#include <bitset>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <list>
#include <set>
#include <map>
using namespace std;
//using namespace __gnu_cxx;
//define
#define pii pair<int,int>
#define mem(a,b) memset(a,b,sizeof(a))
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define PI acos(-1.0)
//typedef
//typedef __int64 LL;
//typedef unsigned __int64 ULL;
//const
const int N=;
const int INF=0x3f3f3f3f;
const int MOD=,STA=;
//const LL LNF=1LL<<60;
const double EPS=1e-;
const double OO=1e15;
const int dx[]={-,,,};
const int dy[]={,,,-};
const int day[]={,,,,,,,,,,,,};
//Daily Use ...
inline int sign(double x){return (x>EPS)-(x<-EPS);}
template<class T> T gcd(T a,T b){return b?gcd(b,a%b):a;}
template<class T> T lcm(T a,T b){return a/gcd(a,b)*b;}
template<class T> inline T lcm(T a,T b,T d){return a/d*b;}
template<class T> inline T Min(T a,T b){return a<b?a:b;}
template<class T> inline T Max(T a,T b){return a>b?a:b;}
template<class T> inline T Min(T a,T b,T c){return min(min(a, b),c);}
template<class T> inline T Max(T a,T b,T c){return max(max(a, b),c);}
template<class T> inline T Min(T a,T b,T c,T d){return min(min(a, b),min(c,d));}
template<class T> inline T Max(T a,T b,T c,T d){return max(max(a, b),max(c,d));}
//End struct Node{
int val,id;
bool operator < (const Node& a)const{
return val>a.val;
}
}nod[N];
int ca; int w[N][N],y[N],vis[N];
int n,m; int dfs(int u)
{
int v;
for(v=;v<=n;v++){
if(w[u][v] && !vis[v]){
vis[v]=;
if(y[v]==- || dfs(y[v])){
y[v]=u;
return ;
}
}
}
return ;
} int main()
{
// freopen("in.txt","r",stdin);
int i,j,tot,a;
int x[N];
scanf("%d",&ca);
while(ca--)
{
scanf("%d",&n);
for(i=;i<=n;i++){
scanf("%d",&nod[i].val);
nod[i].val*=nod[i].val;
nod[i].id=i;
}
mem(w,);
for(i=;i<=n;i++){
scanf("%d",&tot);
while(tot--){
scanf("%d",&a);
w[i][a]=nod[i].val;
}
}
sort(nod+,nod+n+); mem(x,);
mem(y,-);
for(i=;i<=n;i++){
mem(vis,);
dfs(nod[i].id);
}
for(i=;i<=n;i++)
if(y[i]!=-)x[y[i]]=i; printf("%d",x[]);
for(i=;i<=n;i++)
printf(" %d",x[i]);
putchar('\n');
}
return ;
}
ZOJ-2362 Beloved Sons 贪心 | KM的更多相关文章
- zoj 2362 Beloved Sons【二分匹配】
题目:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2361 来源:http://acm.hust.edu.cn/vjudg ...
- SGU 210 Acdream 1227 Beloved Sons KM
题目链接:点击打开链接 题意: 给定n个人 每一个人的点权 以下n行i行表示第i个人能够获得哪些数(数字从1-n.且不能反复分配) 若这个人获得了数字则你能够获得他的权值. 要你能获得的权值和最大. ...
- POJ - 2349 ZOJ - 1914 Arctic Network 贪心+Kru
Arctic Network The Department of National Defence (DND) wishes to connect several northern outposts ...
- ZOJ-2362 Beloved Sons 最大权值匹配
题意:国王有N个儿子,现在每个儿子结婚都能够获得一定的喜悦值,王子编号为1-N,有N个女孩的编号同样为1-N,每个王子心中都有心仪的女孩,现在问如果安排,能够使得题中给定的式子和最大. 分析:其实题目 ...
- POJ 1862 & ZOJ 1543 Stripies(贪心 | 优先队列)
题目链接: PKU:http://poj.org/problem?id=1862 ZJU:http://acm.zju.edu.cn/onlinejudge/showProblem.do?proble ...
- 2017CCPC秦皇岛G ZOJ 3987Numbers(大数+贪心)
Numbers Time Limit: 2 Seconds Memory Limit: 65536 KB DreamGrid has a nonnegative integer n . He ...
- ZOJ 2702 Unrhymable Rhymes 贪心
贪心.能凑成一组就算一组 Unrhymable Rhymes Time Limit: 10 Seconds Memory Limit: 32768 KB Special Judge ...
- ZOJ 3946 Highway Project 贪心+最短路
题目链接: http://www.icpc.moe/onlinejudge/showProblem.do?problemCode=3946 题解: 用dijkstra跑单元最短路径,如果对于顶点v,存 ...
- zoj 2921 Stock(贪心)
Optiver sponsored problem. After years of hard work Optiver has developed a mathematical model that ...
随机推荐
- poj 1830 开关问题
开关问题 题意:给n(0 < n < 29)开关的初始和最终状态(01表示),以及开关之间的关联关系(关联关系是单向的输入a b表示a->b),问有几种方式得到最终的状态.否则输出字 ...
- C语言基础知识-循环结构
用while打印出1~100之间7的倍数 int i = 1; while循环是当条件表达式的结果为真时,执行大括号里面的循环体,重复执行直到条件表达式的结果为假时结束循环. w ...
- Java集合Map接口与Map.Entry学习
Java集合Map接口与Map.Entry学习 Map接口不是Collection接口的继承.Map接口用于维护键/值对(key/value pairs).该接口描述了从不重复的键到值的映射. (1) ...
- ibdata1是?
MySQL使用InnoDB引擎的时候,ibdata1这个文件会随着时间的增长,会变得越来越大,占据大量的磁盘空间. 那么,ibdata1里保存了哪些东西,为什么会变得越来越大呢,让我们开看看ibdat ...
- Source Insight 显示中文乱码
Source Insight 3.X utf8支持插件震撼发布 继上次SI多标签插件之后,因为公司内部编码改为utf8编码,因此特意做了这个Source Insight 3.X utf8插件. 下载地 ...
- CODEVS 1069关押罪犯
题目描述 Description S 城现有两座监狱,一共关押着N 名罪犯,编号分别为1~N.他们之间的关系自然也极 不和谐.很多罪犯之间甚至积怨已久,如果客观条件具备则随时可能爆发冲突.我们用“怨 ...
- HDU4528+BFS
/* bfs+标记状态 如何记录状态是关键!! */ #include<stdio.h> #include<string.h> #include<stdlib.h> ...
- JDBC MySQL字段类型为datetime的数据取出(util.Date)
使用ResultSet的getTimestamp方法获取java.util.Date型数据 java.util.Date time = rs.getTimestamp("time" ...
- jmeter linux使用经验小结
1. 确认务必安装了JDK,并且把路径配置OK.否则执行会报错. 2. 当做负载机时,在hosts 配置上 你的ip 你的hostname 或者使用./bin/jmeter-server ...
- 如何查看Linux的磁盘空间还有多少
无论是Linux还是unix,都提供了很多工具来查看磁盘空间的大小. 1. df命令,df命令,可以查看当前文件系统占用的空间的大小,以及剩余的大小,如下例子: Filesystem 1K-b ...