より多くの混乱を学ぶための本の前にパープルツリー、問題のみを行うことを求めています。今、私たちは新しいツリーこのデータ構造について学びます。
poj1330
親配列で練習するには、ツリーを格納し、ツリーを走査します。
そして、ルートに行くための一つの方法は、この問題のように長いライン上の位置マークを通じて、およびように、このポイントを介して他のかどうかを判断します。
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
const int maxn=10000+10;
int a[maxn];
int t,n;
bool vis[maxn];
int main()
{
//freopen("in.txt","r",stdin);
cin>>t;
while(t--)
{
int x,y;
cin>>n;
memset(a,0,sizeof(a));
memset(vis,false,sizeof(vis));
for(int i=0;i<n-1;++i)
{
cin>>x>>y;
a[y]=x;
}
cin>>x>>y;
while(y!=0)
{
vis[y]=true;
y=a[y];
}
while(!vis[x])
{x=a[x];}
cout<<x<<endl;
}
return 0;
}
poj2499
アイデア:私たちは、ルートの方に行くために、ノードから開始することができるように、パス内のすべてのノードへのバイナリツリーは、一意であるため。
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
int t,kase=0;
int main()
{
cin>>t;
while(t--)
{
int a,b,right=0,left=0;
cin>>a>>b;
int temp;
while(a!=1||b!=1)//这里是或
{
if(a>b)
{
temp=(a-1)/b;//数据规模较大,一次减除来,因为根节点是(1,1)所以a-1,下面同理。
a-=(b*temp);
left+=temp;
}else
{
temp=(b-1)/a;
b-=a*temp;
right+=temp;
}
}
printf("Scenario #%d:\n%d %d\n\n",++kase,left,right);
}
return 0;
}
poj2255
バイナリツリートラバーサル
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
using namespace std;
char a[30],b[30];
int L1,R1,L2,R2;
vector<char>ans;
void tree(int L1,int R1,int L2,int R2)
{
if(L1>R1)return ;
int k=0;
while(a[L1]!=b[L2+k])k++;
tree(L1+1,L1+k,L2,L2+k-1);
tree(L1+k+1,R1,L2+k+1,R2);
ans.push_back(a[L1]);
}
int main()
{
//freopen("in.txt","r",stdin);
while(cin>>a>>b)
{
ans.clear();
L1=strlen(a);
tree(0,L1-1,0,L1-1);
for(int i=0;i<ans.size();++i)
cout<<ans[i];
cout<<endl;
}
return 0;
}
poj2309
アイデア:木の姿の特性を観察します。
1 xが奇数の場合、単一のノード。
2図のxが偶数であれば、底[X-2 ^ K + 1、X-1] [X + 1、X + 2 ^ K-1]。2 ^ kがX&されていてもよい - 計算(x)は、負のバイナリ表現に関連します。
#include <iostream>
#include <cstdio>
using namespace std;
int t,x,k;
int main()
{
cin>>t;
while(t--)
{
cin>>x;
k=x&(-x);
cout<<x-k+1<<" "<<x+k-1<<endl;
}
return 0;
}
zoj2724
バイナリヒープは、ルートノードは優先待ち行列を使用して、リーフノード(より大きい)未満です。なお、構造とのstrcmpの使用
大量のデータ、提案された変更のscanfとprintf関数。
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <iostream>
using namespace std;
struct message
{
char name[100];
int data;
int priority;
bool operator <(const message &a) const
{
return a.priority<priority;
}
};
priority_queue<message>q;
char cmd[100];
message m;
int main()
{
//freopen("in.txt","r",stdin);
while(scanf("%s",&cmd)!=EOF)
{
if(strcmp(cmd,"GET")==0)
{
if(!q.size())cout<<"EMPTY QUEUE!\n";
else
{
cout<<q.top().name<<" "<<q.top().data<<endl;
q.pop();
}
}
else if(strcmp(cmd,"PUT")==0)
{
cin>>m.name>>m.data>>m.priority;
q.push(m);
}
}
return 0;
}
poj3253
*ハフマン木の重みと最低のプライオリティキューのパスがここで使用されます。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <vector>
using namespace std;
priority_queue<int,vector<int>,greater<int> >q;
int main()
{
int n,temp;
while(cin>>n)
{
while(!q.empty())q.pop();
for(int i=0;i<n;++i)
{
cin>>temp;
q.push(temp);
}
int a,b;
long long ans=0;
while(q.size()>1)
{
a=q.top();q.pop();
b=q.top();q.pop();
ans+=(a+b);
q.push(a+b);
}
cout<<ans<<endl;
}
return 0;
}