日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) >

POJ1785(笛卡尔树的构造)

發(fā)布時(shí)間:2024/4/11 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 POJ1785(笛卡尔树的构造) 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

題目:http://poj.org/problem?id=1785

?

題意:構(gòu)造笛卡爾樹(shù),這里是最大堆構(gòu)造,然后以廣義表的形式輸出這棵笛卡爾樹(shù)。

#include <iostream> #include <string.h> #include <algorithm> #include <stdio.h>using namespace std; const int N=500010; const int INF=~0U>>1;struct node {int fix;int pre,l,r;char str[10];void clear(){pre=l=r=0;}bool operator < (node t) const{return strcmp(str,t.str)<0;} };node T[N];void Init(int n) {for(int i=0; i<=n; i++)T[i].clear();T[0].fix=INF; }int Build(int n) {for(int i=1; i<=n; i++){int j=i-1;while(T[j].fix<T[i].fix)j=T[j].pre;T[i].l=T[j].r;T[j].r=i;T[i].pre=j;}return T[0].r; }void dfs(int cur) {if(cur==0) return;printf("(");dfs(T[cur].l);printf("%s/%d",T[cur].str,T[cur].fix);dfs(T[cur].r);printf(")"); }int main() {int n;char str[105];while(scanf("%d",&n),n){Init(n);for(int i=1; i<=n; i++){scanf("%s",str);sscanf(str,"%[^/]/%d",T[i].str,&T[i].fix);}sort(T+1,T+n+1);int root=Build(n);dfs(root);puts("");}return 0; }


?

總結(jié)

以上是生活随笔為你收集整理的POJ1785(笛卡尔树的构造)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。