当前位置: 首页 > news >正文

POJ3107树的重心

 请使用用scanf和printf谢谢。关闭输入输出流还是t

/*I lv ya*/
#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <vector>
#include <stack>
#include <sstream>
#include <map>
#include <set>
#include <queue>
#include <stdlib.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> PII;
typedef pair<ll,ll> PLL;
typedef pair<char,char> PCC;
#define fi first
#define se second
#define delay for(ll DELAY=0;DELAY<=4000000000;DELAY++)
#define pb push_back
bool cmp1(ll x,ll y) {return x>y;}
const ll mod = 1e9+7;
const int N = 2e5+5;
int mx[] = { 1,0,-1,0 }, my[] = { 0,1,0,-1 };
///list star startint head[N<<2], IDX = 0;struct NODE{int t, ne, w;}ed[N<<2];
void add(int s,int t){ed[++IDX].ne = head[s]; ed[IDX].t = t; head[s] = IDX;
}///list star end
/// DSU_begin
/*
int FA[N];
int _fi(int x){return (FA[x] == x) ? x : FA[x] = _fi(FA[x]);}
void uni(int x, int y){int xx = _fi(x), yy = _fi(y);if (xx != yy) FA[xx] = yy;}
*/
/// DSU_end
int n;
int d[N];///当前以i为根的子树的点数
int maxn = 0x3f3f3f3f;
int cnt;
int ans[N];
void dfs(int now,int last)
{d[now] = 1;int tmp = 0;///当前结点的最大子树大小for(int i = head[now];i;i=ed[i].ne){int t = ed[i].t;if(t!=last){dfs(t,now);///向下遍历,向上传递d[now]+=d[t];tmp = max(tmp,d[t]);}}tmp = max(tmp,n-d[now]);///父节点为根、不涉及当前结点分支的子树的节点数if(tmp<maxn){maxn = tmp;cnt = 0;ans[++cnt] = now;}else if(tmp == maxn){ans[++cnt] = now;}
}
void solve()
{scanf("%d",&n);for(int i=1;i<=n-1;i++){int a,b;scanf("%d %d",&a,&b);add(a,b);add(b,a);}dfs(1,0);///直接从1结点遍历sort(ans+1,ans+cnt+1);for(int i=1;i<=cnt;i++){printf("%d ",ans[i]);}cout<<endl;
}
int main()
{//ios::sync_with_stdio(false);//cin.tie(NULL),cout.tie(NULL);int _=1;//cin>>_;while(_--){solve();}return 0;
}

http://www.dtcms.com/a/201231.html

相关文章:

  • java collection集合特点知识点详解
  • Golang的Web应用架构设计
  • CentOS相关操作hub(更新中)
  • Hooks实现原理与自定义Hooks
  • 头歌之软件工程-用例设计
  • 【C++】不推荐使用的std::allocator<void>
  • ETL数据集成产品选型需要关注哪些方面?
  • 嵌入式自学第二十四天
  • 操作系统————五种页面置换算法(OPT,FIFO,LRU,NRU,加强版NRU)大总结
  • 日语学习-日语知识点小记-构建基础-JLPT-N4阶段(24):受身形
  • 4.7 时间模块
  • 《深入理解数组名:sizeof(arr)、arr 和 arr 的区别》
  • 开源情报的发展前景与行业运用
  • 后退的风景
  • Spring boot 集成 Knife4j
  • 专题五:floodfill算法(扫雷游戏精讲)
  • vs2017编译zlib1.2.11
  • 定积分,不定积分,变限积分和反常积分的对比记忆
  • 高防CDN:让攻击流量“一键清零“的智能防御之道
  • C++类与对象--3 C++对象模型和this指针
  • interface接口和defer场景分析
  • 深入理解动态规划:从斐波那契数列到最优子结构
  • ORPO:让大模型调优更简单高效的新范式
  • Ubuntu——配置静态IP
  • python next 函数
  • 大模型训练计算显存占用
  • 部署springBoot项目的脚本-linux
  • 【MYSQL】基本查询,表的增删查改
  • 树莓派系列教程第八弹:结合 ESP32-CAM 实现远程摄像头监控
  • Effective C++阅读笔记(item 1-4)