栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

zoj 3007 Final Treasure

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

zoj 3007 Final Treasure

#include <cmath>#include <vector>#include <cstdio>using namespace std;int pre[512], tag[512], dis[512], cc;vector<int> e[512];void dfs(int n, int p, int c, int d){    for (size_t i = 0; i < e[p].size(); i++) {        if (pre[e[p][i]] == -1) { pre[e[p][i]] = p; tag[e[p][i]] = c; dis[e[p][i]] = d + 1; ++cc; dfs(n, e[p][i], c, d + 1);        }    }}int cnt[512];int handle(int n){    int ret = 0, t, tt;    for (int i = 0; i < n; i++) {        pre[i] = -1;        e[i].clear();    }    for (int i = 1; i < n; i++) {        scanf("%d", &t);        e[i - 1].push_back(t);        e[t].push_back(i - 1);    }    pre[0] = -2;    dis[0] = 0;    dfs(n, 0, 0, 0);    t = n - 1;    do {        tt = pre[t];        pre[t] = -2;        tag[t] = dis[t];        dis[t] = 0;        t = tt;    } while (t != -2);    for (int i = 0; i < n; i++) {        if (pre[i] != -2) { pre[i] = -1;        }    }    for (int i = 0; i < n; i++) {        if (pre[i] == -2) { ++ret; cc = 1; dfs(n, i, tag[i], 0); cnt[tag[i]] = cc;        }    }    return ret;}double calc_x(int m, double x, double y){    double ret = 0;    for (int i = 1; i <= m; i++) {        ret += x / hypot(x, y);        y += cnt[i];    }    return ret;}double calc_y(int m, double x, double y){    double ret = 0;    for (int i = 1; i <= m; i++) {        ret += y / hypot(x, y);        y += cnt[i];    }    return ret;}int main(void){    int n, m, k;    double s, lx, rx, mx, ly, ry, my, xx, yy;    while (scanf("%d%lf%d", &n, &s, &k) != EOF) {        m = handle(n) - 1;        lx = 0;        rx = 1e11;          while (rx - lx > 1e-8) {     mx = (lx + rx) / 2; ly = -n; ry = n; while (ry - ly > 1e-8) {         my = (ly + ry) / 2;     yy = calc_y(m, mx, my);     if (yy >= 0) {         ry = my;     }     else {         ly = my;     } } my = (ly + ry) / 2; xx = calc_x(m, mx, my); if (xx >= s) {     rx = mx; } else {     lx = mx; }        }        mx = (lx + rx) / 2;        xx = 0;        yy = 0;        for (int i = 1; i <= tag[k]; i++) { xx += mx / hypot(mx, my); yy += my / hypot(mx, my); my += cnt[i];        }        yy -= dis[k];        printf("%.3lf %.3lfn", xx, yy);    }    return 0;}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/370284.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号