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 ...
随机推荐
- SQL常用命令浅析
表操作备注:操作之前使用“use <数据库名>”应连接某个数据库.建表命令:create table <表名> (<字段名 1> <类型 1> [,.. ...
- ubuntu 12.04 安装 nginx+php+mysql web服务器
Nginx 是一个轻量级,以占用系统资源少,运行效率而成为web服务器的后起之秀,国内现在很多大型网站都以使用nginx,包括腾讯.新浪等大型信息网站,还有淘宝网站使用的是nginx二次开发的web服 ...
- Unity3d Shader开发(五)Fallback ,Category
Fallback定义在所有子着色器后.简单来说,它表示"如果没有任何子着色器能被运行在当前硬件上,请尝试使用降级着色器". Syntax 语法 Fallback "nam ...
- 微软职位内部推荐-ATG Engineer II
微软近期Open的职位: ATG Engineer - GeneralistReady to work on some of the most advanced hardware on the pla ...
- C#基础|面向对象之多态
什么是多态 一句话解释,同一种事物表现出的多种形态. 看完以后,应该还是蒙的,还是看代码吧.. 现有Person类,Student类,Teacher类 其中Person类是Student和Te ...
- 简单的map转换成Bean的工具
简单的map转换成Bean的工具 package com.sd.microMsg.util; import java.lang.reflect.Field; import java.lang.refl ...
- uva 12648
一个简单的搜索: 反正树的结构不会变,只需要把节点的名称换一下就行: 可惜比赛的时候思路不清晰: #include<cstdio> #define maxn 5050 #include&l ...
- 一些简单的问题. 2的10次方与k (涉及到b k m的要用乘来解读)
2的10次方是k k就表示2的10次方 2的16次方,解读为 2的6次方(64)*2的10次方(k) 简写为64k 64k=64*k 同理2的20次方 解读为2的10次方*2的10次方 k ...
- TC SRM 607 DIV2
求拼接完成后的字符串包含的子回文串的数目,一开始还用暴力去做,想都不用想 肯定超时了. 复习了一下求最长子回文串的算法,发现可以类似解决. 给相邻字符之间添加一个'@'字符,这样所有的回文串都是奇数长 ...
- SPRING IN ACTION 第4版笔记-第六章RENDERING WEB VIEWS-006- 使用thymeleaf(TemplateResolver、SpringTemplateEngine、ThymeleafViewResolver、th:include、th:object、th:field="*{firstName}")
一.在Spring中使用thymeleaf的步骤 1.配置 In order to use Thymeleaf with Spring, you’ll need to configure three ...