https://www.patest.cn/contests/gplt/L2-007

题解:一开始是想直接并查集,一个家就是一个集合,对每个集合维护一个人数num1一个房产数num2 一个房产面积area 以及一个最小编号。

  后来想了一下,直接建树,然后对每棵树搜一遍算了,给的信息太多了。。。但是每个人会有两个父亲节点,我们一个都不管,只是存在数组里,在找这颗树的最小id时会用到。

  再想了一下,直接建图呗。然后写了半小时。。太复杂了吧

坑:1.set的erase的参数不是int,是iterator//把我neng得没脾气

  2。这题的总人数和给的数据数是不一样的//我这儿卡了

  3.最后还忘了输出家庭总数

  4.auto t:不能修改容器的值,只能用来输出。

 

#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<vector>
#include<queue>
#include<map>
#include<cmath>
#include<stack>
#include<string.h>
#include<set>
#include<algorithm>
using namespace std;
const int maxn = 1e5 + ;
int f[maxn], num[maxn], are[maxn], mn[maxn],vis[maxn];
vector<int> E[maxn];
vector<int> root; set<int> st;
map<int, int>mmp;
struct node {
double num1, num2, area;//1人2房
int idx;
node(double num1 = , double num2 = , double area = , int idx = 1e5) :num1(num1), num2(num2), area(area), idx(idx) {}
};
int cmp(node a, node b) {
if (a.area == b.area) {
return a.idx < b.idx;
}
else return a.area > b.area;
} int main(){
int n;
cin >> n;
int cnt = n;
while (n--) {
int x, y, z,k;
cin >> x >> y >> z; mmp[x]++; mmp[y]++; mmp[z]++;
if (y == - && z == -)root.push_back(x);
else {
if(y!=-)E[y].push_back(x); E[x].push_back(y);
if(x!=-)E[z].push_back(x); E[x].push_back(z);
}
if (x == -) x = 1e5;
if (y == -)y = 1e5;
mn[x] = min(x, y);
cin>> k; while (k--) {
int w;
cin >> w; mmp[w]++;
E[x].push_back(w); E[w].push_back(x);
}
cin >> num[x] >> are[x];
}
vector<node> ans; vis[-] = ;
int id =;
mmp.erase(-);
while(!mmp.empty()) {
int t = mmp.begin()->first;
if (vis[t]) continue;//his couple has done.
//bfs
ans.push_back(node());
ans[id].num2 =num[t];
ans[id].num1=;
ans[id].area = are[t];
ans[id].idx =t;
id++;
queue<int> Q;
Q.push(t); vis[t] = ; mmp.erase(t);
while (!Q.empty()) {
int now = Q.front(); Q.pop();
for (int i = ; i < E[now].size(); i++) {
int v = E[now][i];
if (vis[v])continue;
Q.push(v); vis[v] = ; mmp.erase(v);
ans.back().num2+= num[v];
ans.back().num1++;
ans.back().area += are[v];
ans.back().idx = min(ans.back().idx, v); }
}
}
for (vector<node>::iterator t = ans.begin(); t != ans.end();t++) { t->area /= t->num1; t->num2 /= t->num1; }
sort(ans.begin(), ans.end(),cmp);
cout << ans.size() << endl;
for (auto t : ans) {
printf("%04d", t.idx);
//cout << ' ' << t.num1;
printf(" %.0lf %.3lf %.3lf\n", t.num1, t.num2, t.area);
//<< ' ' << t.num2 << ' ' << t.area << endl;
}
system("pause");
}

CCCC L2-007. 家庭房产 建图 XJB模拟的更多相关文章

  1. PAT 天梯赛 L2-007 家庭房产

    建图+DFS 题目链接:https://www.patest.cn/contests/gplt/L2-007 题解 在热身赛的时候没有做出来,用的并查集的思想,但是敲残了,最后也没整出来.赛后听到别人 ...

  2. [BZOJ4289] [PA2012] Tax 解题报告 (最短路+差分建图)

    题目链接:https://www.lydsy.com/JudgeOnline/problem.php?id=4289 4289: PA2012 Tax Time Limit: 10 Sec  Memo ...

  3. 【2019.7.26 NOIP模拟赛 T3】化学反应(reaction)(线段树优化建图+Tarjan缩点+拓扑排序)

    题意转化 考虑我们对于每一对激活关系建一条有向边,则对于每一个点,其答案就是其所能到达的点数. 于是,这个问题就被我们搬到了图上,成了一个图论题. 优化建图 考虑我们每次需要将一个区间向一个区间连边. ...

  4. CF786B Legacy 线段树优化建图 + spfa

    CodeForces 786B Rick和他的同事们做出了一种新的带放射性的婴儿食品(???根据图片和原文的确如此...),与此同时很多坏人正追赶着他们.因此Rick想在坏人们捉到他之前把他的遗产留给 ...

  5. 7月13日考试 题解(DFS序+期望+线段树优化建图)

    T1 sign 题目大意:给出一棵 N 个节点的树,求所有起点为叶节点的有向路径,其 上每一条边权值和的和.N<=10000 水题.考试的时候毒瘤出题人(学长orz)把读入顺序改了一下,于是很多 ...

  6. 【BZOJ-1570】BlueMary的旅行 分层建图 + 最大流

    1570: [JSOI2008]Blue Mary的旅行 Time Limit: 15 Sec  Memory Limit: 162 MBSubmit: 388  Solved: 212[Submit ...

  7. 【BZOJ-4289】Tax 最短路 + 技巧建图

    4289: PA2012 Tax Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 168  Solved: 69[Submit][Status][Dis ...

  8. CF467D Fedor and Essay 建图DFS

      Codeforces Round #267 (Div. 2) CF#267D D - Fedor and Essay D. Fedor and Essay time limit per test ...

  9. UVa 3487 & 蜜汁建图

    题意: 有两家公司都想向政府申请某些资源的使用权,并且他们都提供了一些申请列表,列表中含有申请费用和资源种类,同一家公司的申请列表之间不含有重复的资源.政府只可以完整地接受和拒绝谋一份申请列表,问政府 ...

随机推荐

  1. Dubbo 实例

    POM: <!-- Dubbo --> <dependency> <groupId>com.alibaba</groupId> <artifact ...

  2. Oracle存储过程入参传入List集合的小例子

    第一步:创建一个对象类型 create or replace type STUDENT as object( id ), name ), age ) ); / 第二步:创建一个数组类型 (任意选择下面 ...

  3. 【安全开发】PHP安全编码规范

    申明:本文非笔者原创,原文转载自:https://github.com/SecurityPaper/SecurityPaper-web/blob/master/_posts/2.SDL%E8%A7%8 ...

  4. 【RF库Built-In测试】Catenate

    Name:CatenateSource:BuiltIn <test library>Arguments:[ *items ]Catenates the given items togeth ...

  5. Redis 操作字符串数据

    Redis 操作字符串数据: > set name "Tom" // set 用于添加 key/value 数据,如果 key 存在则覆盖 OK > setnx nam ...

  6. jquery前端验证框架

    1.validationEngine.jquery.css  样式包 2.jquery.validationEngine-zh_CN.js 中文语言包 3.jquery.validationEngin ...

  7. 类型化dataset分页

    SELECT TOP (@每页行数) 所选列FROM 表名WHERE (主键 NOT IN( SELECT TOP ((@页数-1)*@每页行数) 主键FROM 表名where ( 条件)))AND ...

  8. 求组合数 C++程序

    一 递归求组合数 设函数为void    comb(int m,int k)为找出从自然数1.2.... .m中任取k个数的所有组合. 分析:当组合的第一个数字选定时,其后的数字是从余下的m-1个数中 ...

  9. Windows平台下PHP7添加Sqlserver扩展

    1.7.0.x 7.0.x的扩展下载地址: Microsoft Drivers for PHP for SQL Server  https://www.microsoft.com/en-us/down ...

  10. Javascript 变态题解析

    读者可以先去做一下感受感受. 当初笔者的成绩是 21/44... 当初笔者做这套题的时候不仅怀疑智商, 连人生都开始怀疑了.... 不过, 对于基础知识的理解是深入编程的前提. 让我们一起来看看这些变 ...