7-1 还原二叉树 (25 分)
#include<bits/stdc++.h>
#define ll long long
const int N = 2e5 + 10;
using namespace std;
int n;
char pre[N];
char mid[N];
struct node{
char data;
node *l,*r;
};
node *buildtree(int len,char *pre,char *mid){
node *root=new node;
if(!len) return NULL;
root->data=pre[0];
int i;
for(i=0;i<len;i++){
if(mid[i]==pre[0]) break;
}
root->l=buildtree(i,pre+1,mid);
root->r=buildtree(len-i-1,pre+i+1,mid+i+1);
return root;
}
int get_high(node *root){
int res=0;
int hl=0,hr=0;
if(!root) res=0;
else{
hl=get_high(root->l);
hr=get_high(root->r);
res=max(hl,hr)+1;
}
return res;
}
inline void solve(){
cin>>n;
cin>>pre>>mid;
node *root=buildtree(n,pre,mid);
cout<<get_high(root)<<endl;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t=1;
while(t--) solve();
return 0;
}
7-2 朋友圈 (25 分)
#include<bits/stdc++.h>
#define ll long long
const int N = 2e5 + 10;
using namespace std;
int vis[N];
int num[N];
void init(int n){
for(int i=0;i<=n;i++) vis[i]=i;
}
int Find(int x){
if(x!=vis[x]) vis[x]=Find(vis[x]);
return vis[x];
}
void Merge(int x,int y){
int xx=Find(x);
int yy=Find(y);
if(xx!=yy) vis[xx]=yy;
}
inline void solve(){
int n,m;
cin>>n>>m;
init(n);
while(m--){
int k,x,y;
cin>>k;
if(k){
k--;
cin>>x;
while(k--){
cin>>y;
Merge(x,y);
}
}
}
int maxn=0;
for(int i=1;i<=n;i++){
int x=Find(i);
num[x]++;
maxn=max(maxn,num[x]);
}
cout<<maxn<<endl;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
solve();
return 0;
}
7-3 修理牧场 (25 分)
#include<bits/stdc++.h>
#define ll long long
const int N = 2e5 + 10;
using namespace std;
inline void solve(){
int n;
cin>>n;
int x;
priority_queue<int,vector<int>,greater<int> >p;
for(int i=1;i<=n;i++){
cin>>x;
p.push(x);
}
int sum=0;
int a,b;
while(p.size()>1){
a=p.top();p.pop();
b=p.top();p.pop();
sum+=a+b;
p.push(a+b);
}
cout<<sum<<endl;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
solve();
return 0;
}
7-4 树的遍历 (25 分)
#include<bits/stdc++.h>
#define ll long long
const int N = 2e5 + 10;
using namespace std;
int n;
int post[N];
int mid[N];
struct node{
int data;
node *l,*r;
};
node *buildtree(int len,int *post,int *mid){
node *root=new node;
if(!len) return NULL;
root->data=post[len-1];
int i;
for(i=0;i<len;i++){
if(mid[i]==post[len-1]) break;
}
root->l=buildtree(i,post,mid);
root->r=buildtree(len-i-1,post+i,mid+i+1);
return root;
}
void cengxu(node *root){
queue<node>q;
q.push(*root);
bool tag=0;
while(q.size()){
auto t=q.front();
q.pop();
if(tag) cout<<" ";
else tag=1;
cout<<t.data;
if(t.l) q.push(*t.l);
if(t.r) q.push(*t.r);
}
cout<<endl;
}
inline void solve(){
cin>>n;
for(int i=0;i<n;i++) cin>>post[i];
for(int i=0;i<n;i++) cin>>mid[i];
node *root=buildtree(n,post,mid);
cengxu(root);
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t=1;
while(t--) solve();
return 0;
}
7-5 部落 (25 分)
#include<bits/stdc++.h>
#define ll long long
const int N = 2e5 + 10;
using namespace std;
int vis[N];
void init(int n){
for(int i=0;i<=n;i++) vis[i]=i;
}
int Find(int x){
if(x!=vis[x]) vis[x]=Find(vis[x]);
return vis[x];
}
void Merge(int x,int y){
int xx=Find(x);
int yy=Find(y);
if(xx!=yy) vis[xx]=yy;
}
set<int>peo,num;
inline void solve(){
int n;
cin>>n;
init(100000);
for(int i=1;i<=n;i++){
int k;
cin>>k;
if(k){
int x,y;
cin>>x;
peo.insert(x);
k--;
while(k--){
cin>>y;
peo.insert(y);
Merge(x,y);
}
}
}
for(int i=1;i<=peo.size();i++) num.insert(Find(i));
cout<<peo.size()<<" "<<num.size()<<endl;
int q;
cin>>q;
while(q--){
int x,y;
cin>>x>>y;
if(Find(x)!=Find(y)) cout<<"N"<<endl;
else cout<<"Y"<<endl;
}
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
solve();
return 0;
}
7-6 交换二叉树中每个结点的左孩子和右孩子 (20 分)
#include<bits/stdc++.h>
#define ll long long
const int N = 2e5 + 10;
using namespace std;
typedef struct node{
char data;
node *l,*r;
}*Bitree;
string s;
int pos;
void buildtree(Bitree &root){
if(s[pos]=='#'){
root=NULL;
pos++;
}else{
root=new node;
root->data=s[pos++];
buildtree(root->l);
buildtree(root->r);
}
}
void midorder(Bitree root){
if(root){
midorder(root->l);
cout<<root->data;
midorder(root->r);
}
}
void exmidorder(Bitree root){
if(root){
exmidorder(root->r);
cout<<root->data;
exmidorder(root->l);
}
}
inline void solve(){
cin>>s;
pos=0;
node *root=new node;
buildtree(root);
midorder(root);
cout<<endl;
exmidorder(root);
cout<<endl;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
solve();
return 0;
}
7-7 列出叶结点 (25 分)
#include<bits/stdc++.h>
#define eb emplace_back
#define ll long long
const int N = 2e5 + 10;
using namespace std;
struct node{
int l,r;
}tree[N];
int val[N];
queue<int>q;
vector<int>res;
void BFS(){
while(q.size()){
auto t=q.front();
q.pop();
if(tree[t].l==-1&&tree[t].r==-1) res.eb(t);
if(tree[t].l!=-1) q.push(tree[t].l);
if(tree[t].r!=-1) q.push(tree[t].r);
}
}
inline void solve(){
int n;
cin>>n;
char a,b;
for(int i=0;i<n;i++){
cin>>a>>b;
if(a=='-') tree[i].l=-1;
else{
tree[i].l=a-'0';
val[a-'0']=1;
}
if(b=='-') tree[i].r=-1;
else{
tree[i].r=b-'0';
val[b-'0']=1;
}
}
int root;
for(int i=0;i<n;i++){
if(!val[i]){
root=i;
break;
}
}
q.push(root);
BFS();
bool tag=0;
for(auto i:res){
if(tag) cout<<" ";
else tag=1;
cout<<i;
}
cout<<endl;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
solve();
return 0;
}
7-8 建立与遍历二叉树 (25 分)
#include<bits/stdc++.h>
#define ll long long
const int N = 2e5 + 10;
using namespace std;
typedef struct node{
char data;
node *l,*r;
}*Bitree;
string s;
int pos;
void buildtree(Bitree &root){
if(s[pos]=='#'){
root=NULL;
pos++;
}else{
root=new node;
root->data=s[pos++];
buildtree(root->l);
buildtree(root->r);
}
}
void midorder(Bitree root){
if(root){
midorder(root->l);
cout<<root->data;
midorder(root->r);
}
}
inline void solve(){
cin>>s;
pos=0;
node *root=new node;
buildtree(root);
midorder(root);
cout<<endl;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
solve();
return 0;
}
7-9 完全二叉树的层序遍历 (25 分)
#include<bits/stdc++.h>
#define ll long long
const int N = 2e5 + 10;
using namespace std;
int n;
int root[N];
void buildtree(int x){
if(x>n) return ;
buildtree(2*x);
buildtree(2*x+1);
cin>>root[x];
}
inline void solve(){
cin>>n;
buildtree(1);
for(int i=0;i<n;i++){
if(i) cout<<" ";
cout<<root[i+1];
}
cout<<endl;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
solve();
return 0;
}