当前位置:网站首页>PAT甲级 1021 Deepest Root
PAT甲级 1021 Deepest Root
2022-06-27 02:44:00 【九是否非随机的称呼】
并查集查看树的棵树;迪杰特斯拉方式寻找最远的路径
#include<iostream>
#include<algorithm>
#include<stdlib.h>
#include<vector>
#include<string>
#include<map>
#include<set>
#include<bits/stdc++.h>
using namespace std;
int finder(int *unionfind, int x){
while(x!=unionfind[x]) x = unionfind[x];
return x;
}
void unioner(int *unionfind, int x, int y){
int a = finder(unionfind, x);
int c = finder(unionfind, y);
if(a > c)
unionfind[y] = unionfind[x] = c;
else
unionfind[y] = unionfind[x] = a;
}
int main(void){
int m, n, a, b, c, d, e, f;
int INFINF = -999999;
cin>>n;
f = n + 1;
int matrix[f][f];
int unionsz[f]; //并查集查看树的棵数
for(int i=0; i<f; i++) unionsz[i] = i;
memset((void *)matrix, INFINF, sizeof(int) * f * f);
for(int i = 0; i < (n - 1); i++){
cin>>a>>b;
matrix[b][a] = matrix[a][b] = 1;
unioner(unionsz, a, b);
}
e = 0;
for(int i = 1; i < f; i++){
if(unionsz[i]==i) e++;
}
if(e!=1){
printf("Error: %d components", e);
return 0;
}
int maxdis[f][f];
int visited[f][f];
memset((void *)visited, 0, sizeof(int) * f * f);
memset((void *)maxdis, INFINF, sizeof(int) * f * f);
int maxval;
for(c = 1; c < f; c++){ //迪杰特斯拉方式
for(int i = 0; i < f; i++){
maxval = -9999999;
maxdis[c][c] = 0;
m = 0;
for(int j = 0; j < f; j++){
if(maxdis[c][j] > maxval && visited[c][j]==0){
maxval = maxdis[c][j];
m = j;
}
}
visited[c][m] = 1;
for(int j = 0; j < f; j++){
if((maxval + matrix[m][j]) > maxdis[c][j] && visited[c][j]==0){
maxdis[c][j] = maxval + matrix[m][j];
}
}
}
}
maxval = -999999;
for(int i = 0; i < f; i++){
for(int j = 0; j < f; j++){
if(maxdis[i][j] > maxval){
maxval = maxdis[i][j];
}
}
}
vector<int> vr;
for(int i = 0; i < f; i++){
for(int j = 0; j < f; j++){
if(maxdis[i][j] == maxval){
vr.push_back(j);
}
}
}
vr.push_back(999999);
sort(vr.begin(), vr.end());
for(int i = 0; i < vr.size() - 1; i++){
if(vr[i+1]==vr[i]) continue;
else cout<<vr[i]<<endl;
}
return 0;
}边栏推荐
- 正则表达式:语法
- Oracle/PLSQL: Translate Function
- Laravel 的 ORM 缓存包
- Learn Tai Chi Maker - mqtt (VI) esp8266 releases mqtt message
- dat. gui. JS star circle track animation JS special effect
- Detailed explanation of ThreadLocal
- 测试nohup和&的各自作用
- [array] sword finger offer II 012 The sum of left and right subarrays is equal | sword finger offer II 013 Sum of two dimensional submatrix
- Microsoft365开发人员申请
- Precautions for using sneakemake
猜你喜欢

Mmdetection valueerror: need at least one array to concatenate solution

超級詳細,2 萬字詳解,吃透 ES!

解决cherry pick提交报错问题

pytorch_grad_cam——pytorch下的模型特征(Class Activation Mapping, CAM)可视化库

【数组】剑指 Offer II 012. 左右两边子数组的和相等 | 剑指 Offer II 013. 二维子矩阵的和

Flink learning 5: how it works

谷歌开始卷自己,AI架构Pathways加持,推出200亿生成模型
![455. distribute biscuits [distribution questions]](/img/51/c7544d0eaa121cd461ffa678079473.jpg)
455. distribute biscuits [distribution questions]

P5.js death planet

docker部署redis集群
随机推荐
Qingscan use
[array] sword finger offer II 012 The sum of left and right subarrays is equal | sword finger offer II 013 Sum of two dimensional submatrix
What if asreml-r does not converge in operation?
学习太极创客 — MQTT(六)ESP8266 发布 MQTT 消息
pytorch_ grad_ Cam -- visual Library of class activation mapping (CAM) under pytorch
Press key to control LED status reversal
Paddlepaddle 19 dynamically modify the last layer of the model
The use and introduction of pytorch 23 hook and the implementation of plug and play dropblock based on hook
Oracle/PLSQL: Cast Function
Parameter estimation -- Chapter 7 study report of probability theory and mathematical statistics (point estimation)
测试nohup和&的各自作用
达梦数据库的卸载
Introduction to stm32
paddlepaddle 21 基于dropout实现用4行代码dropblock
Flink learning 1: Introduction
1、项目准备与新建
Canvas particles: mouse following JS effect
[micro service sentinel] degradation rules slow call proportion abnormal proportion abnormal constant
解决cherry pick提交报错问题
超級詳細,2 萬字詳解,吃透 ES!
https://github.com/ZouJiu1/PAT