#include <iostream>
#include <stdio.h>
#include <cstdio>
#include <string>
#include <string.h>
#include <math.h>
#include <algorithm>
#include <stack>
using namespace std;
#define maxSize 101
#include <iostream>
#include <stdio.h>
#include <cstdio>
#include <string>
#include <string.h>
#include <math.h>
#include <algorithm>
#include <stack>
using namespace std;
#define maxSize 101
/*
//从图的邻接表转换成邻接矩阵 王道P192.4
typedef struct ENode
{
int adjvex;
struct ENode *next;
}ENode; //边表节点
typedef struct VNode
{
int data;
struct ENode *firstchild;
}VNode,VNodeL[maxSize]; //顶点表节点
typedef struct Graph
{
int VNum,ENum;
VNodeL adjlist;
}Graph;
void Create(Graph &g)
{
cin>>g.VNum>>g.ENum;
for(int i=0;i<g.VNum;i++)
{
cin>>g.adjlist[i].data;
g.adjlist[i].firstchild=NULL;
}
for(int i=0;i<g.ENum;i++)
{
int x,y;
cin>>x>>y;
ENode *p=(ENode*)malloc(sizeof(ENode));
p->adjvex=x;
p->next=g.adjlist[y].firstchild;
g.adjlist[y].firstchild=p;
ENode *q=(ENode*)malloc(sizeof(ENode));
q->adjvex=y;
q->next=g.adjlist[x].firstchild;
g.adjlist[x].firstchild=q;
}
}
void Transfer(Graph g,int matrix[][maxSize])
{
for(int i=0;i<g.VNum;i++)
{
ENode *p=g.adjlist[i].firstchild;
while(p)
{
matrix[i][p->adjvex]=1;
p=p->next;
}
}
}
int main()
{
Graph g;
Create(g);
int matrix[maxSize][maxSize];
for(int i=0;i<g.VNum;i++)
{
for(int j=0;j<g.VNum;j++)
{
matrix[i][j]=0;
}
}
Transfer(g,matrix);
for(int i=0;i<g.VNum;i++)
{
for(int j=0;j<g.VNum;j++)
{
cout<<matrix[i][j]<<" ";
}
cout<<endl;
}
}
// 4
// 4
// 0 1 2 3
// 0 1
// 0 2
// 0 3
// 1 3
// 0 1 1 1
// 1 0 0 1
// 1 0 0 0
// 1 1 0 0
*/
/*
//判断一个无向图是否是一棵树 王道P201.2 若深搜经过的顶点=图的总顶点数,经过的边数等于总顶点数-1则是图
typedef struct ENode
{
int adjvex;
struct ENode *next;
}ENode; //边表节点
typedef struct VNode
{
int data;
struct ENode *firstchild;
}VNode,VNodeL[maxSize]; //顶点表节点
typedef struct Graph
{
int VNum,ENum;
VNodeL adjlist;
}Graph;
void Create(Graph &g)
{
cin>>g.VNum>>g.ENum;
for(int i=0;i<g.VNum;i++)
{
cin>>g.adjlist[i].data;
g.adjlist[i].firstchild=NULL;
}
for(int i=0;i<g.ENum;i++)
{
int x,y;
cin>>x>>y;
ENode *p=(ENode*)malloc(sizeof(ENode));
p->adjvex=x;
p->next=g.adjlist[y].firstchild;
g.adjlist[y].firstchild=p;
ENode *q=(ENode*)malloc(sizeof(ENode));
q->adjvex=y;
q->next=g.adjlist[x].firstchild;
g.adjlist[x].firstchild=q;
}
}
int visited[maxSize];
void DFS(Graph g,int v,int &d,int &e)
{
visited[v]=1;
d++; //统计经过的点的个数!!
ENode *p=g.adjlist[v].firstchild;
while (p) {
e++; //统计经过的边的个数!!
if(visited[p->adjvex]==0)
{
DFS(g,p->adjvex,d,e);
}
p=p->next;
}
}
bool Judge(Graph g,int d,int e)
{
if(d==g.VNum&&e/2==g.VNum-1)
{
return true;
}
return false;
}
int main()
{
Graph g;
Create(g);
int d=0;
int e=0;
DFS(g,0,d,e);
cout<<d<<" "<<e<<endl;
bool x=Judge(g,d,e);
cout<<x<<endl;
}
// 4
// 4
// 0 1 2 3
// 0 1
// 0 2
// 0 3
// 1 3
// 4 8
// 0
*/
/*
//图的深搜的非递归算法 王道P201.3
typedef struct ENode
{
int adjvex;
struct ENode *next;
}ENode; //边表节点
typedef struct VNode
{
int data;
struct ENode *firstchild;
}VNode,VNodeL[maxSize]; //顶点表节点
typedef struct Graph
{
int VNum,ENum;
VNodeL adjlist;
}Graph;
void Create(Graph &g)
{
cin>>g.VNum>>g.ENum;
for(int i=0;i<g.VNum;i++)
{
cin>>g.adjlist[i].data;
g.adjlist[i].firstchild=NULL;
}
for(int i=0;i<g.ENum;i++)
{
int x,y;
cin>>x>>y;
ENode *p=(ENode*)malloc(sizeof(ENode));
p->adjvex=x;
p->next=g.adjlist[y].firstchild;
g.adjlist[y].firstchild=p;
ENode *q=(ENode*)malloc(sizeof(ENode));
q->adjvex=y;
q->next=g.adjlist[x].firstchild;
g.adjlist[x].firstchild=q;
}
}
int visited[maxSize];
void DFS(Graph g,int v)
{
visited[v]=1;
int stack[maxSize];
int top=-1;
stack[++top]=v;
while(top!=-1)
{
v=stack[top--];
cout<<v<<endl;
ENode *p=g.adjlist[v].firstchild;
while(p)
{
if(visited[p->adjvex]==0)
{
visited[p->adjvex]=1;
stack[++top]=p->adjvex;
}
p=p->next;
}
}
}
int main()
{
Graph g;
Create(g);
for(int i=0;i<g.VNum;i++)
visited[i]=0;
DFS(g,2);
}
*/
/*
//判断是否存在由顶点i到顶点j的路径 使用DFS和BFS 王道P201.4
typedef struct ENode
{
int adjvex;
struct ENode *next;
}ENode; //边表节点
typedef struct VNode
{
int data;
struct ENode *firstchild;
}VNode,VNodeL[maxSize]; //顶点表节点
typedef struct Graph
{
int VNum,ENum;
VNodeL adjlist;
}Graph;
void Create(Graph &g)
{
cin>>g.VNum>>g.ENum;
for(int i=0;i<g.VNum;i++)
{
cin>>g.adjlist[i].data;
g.adjlist[i].firstchild=NULL;
}
for(int i=0;i<g.ENum;i++)
{
int x,y;
cin>>x>>y;
ENode *p=(ENode*)malloc(sizeof(ENode));
p->adjvex=x;
p->next=g.adjlist[y].firstchild;
g.adjlist[y].firstchild=p;
ENode *q=(ENode*)malloc(sizeof(ENode));
q->adjvex=y;
q->next=g.adjlist[x].firstchild;
g.adjlist[x].firstchild=q;
}
}
int visited[maxSize];
int BFS(Graph g,int i,int j)
{
int queue[maxSize];
int front=1;
int rear=1;
queue[rear++]=i;
visited[i]=1;
while(front!=rear)
{
i=queue[front++];
if(i==j)
{
return 1;
}
ENode *p=g.adjlist[i].firstchild;
while(p)
{
if(visited[p->adjvex]==0)
{
visited[p->adjvex]=1;
queue[rear++]=p->adjvex;
}
p=p->next;
}
}
return 0;
}
int DFS(Graph g,int i,int j)
{
int stack[maxSize];
int top=-1;
visited[i]=1;
stack[++top]=i;
while(top!=-1)
{
i=stack[top--];
if(i==j)
{
return 1;
}
ENode *p=g.adjlist[i].firstchild;
while(p)
{
if(visited[p->adjvex]==0)
{
visited[p->adjvex]=1;
stack[++top]=p->adjvex;
}
p=p->next;
}
}
return 0;
}
int main()
{
Graph g;
Create(g);
for(int k=0;k<g.VNum;k++)
visited[k]=0;
int i=1,j=2;
int d1=BFS(g,i,j);
cout<<d1<<endl;
for(int k=0;k<g.VNum;k++)
visited[k]=0;
int d2=DFS(g,i,j);
cout<<d2<<endl;
}
*/
/*
//输出从i到j的所有简单路径 王道P201,5 !!重点
typedef struct ENode
{
int adjvex;
struct ENode *next;
}ENode; //边表节点
typedef struct VNode
{
int data;
struct ENode *firstchild;
}VNode,VNodeL[maxSize]; //顶点表节点
typedef struct Graph
{
int VNum,ENum;
VNodeL adjlist;
}Graph;
void Create(Graph &g)
{
cin>>g.VNum>>g.ENum;
for(int i=0;i<g.VNum;i++)
{
cin>>g.adjlist[i].data;
g.adjlist[i].firstchild=NULL;
}
for(int i=0;i<g.ENum;i++)
{
int x,y;
cin>>x>>y;
ENode *q=(ENode*)malloc(sizeof(ENode));
q->adjvex=y;
q->next=g.adjlist[x].firstchild;
g.adjlist[x].firstchild=q;
}
}
int visited[maxSize];
void DFS(Graph g,int i,int j,int d,int path[])
{
visited[i]=1;
int stack[maxSize];
int top=-1;
stack[++top]=i;
d++;
path[d]=i;
while(top!=-1)
{
i=stack[top];
cout<<i<<endl;
// if(i==j)
// {
// for(int k=0;k<=d;k++)
// {
// cout<<path[k];
// }
// }
ENode *p=g.adjlist[i].firstchild;
while (p&&visited[p->adjvex]==1) {
p=p->next;
}
if(p==NULL)
{
// visited[stack[top]]=0;
top--;
}
else
{
d++;
path[d]=p->adjvex;
stack[top++]=p->adjvex;
visited[p->adjvex]=1;
}
}
}
//void DFS(Graph g,int i,int j,int d,int path[])
//{
// visited[i]=1;
// d++;
// path[d]=i;
// if(i==j)
// {
// for(int k=0;k<=d;k++)
// {
// cout<<path[k]<<" ";
// }
// cout<<endl;
// }
// ENode *p=g.adjlist[i].firstchild;
// while(p)
// {
// if(visited[p->adjvex]==0)
// {
// DFS(g,p->adjvex,j,d,path);
// visited[p->adjvex]=0;
// }
// p=p->next;
// }
//}
int main()
{
Graph g;
Create(g);
for(int i=0;i<g.VNum;i++)
{
visited[i]=0;
}
int i=1,j=3;
int path[maxSize];
int d=0;
DFS(g,i,j,d,path);
}
//6
//6
//0 1 2 3 4 5
//0 1
//1 2
//2 3
//1 4
//4 3
//3 5
//0 1 4 3
//0 1 2 3
*/
/*
//对于顺序存储的树,求编号i,j的两个节点的最近公共祖先节点的值 P110 5
//顺序存储的树直接用数组表示就好了
int FindParent(int tree[],int i,int j,int n)
{
while(i!=j)
{
if(i>j)
i=i/2;
else if(i<j)
j=j/2;
}
return i;
}
int main()
{
int i,j;
cin>>i>>j;
int n;
int tree[maxSize];
cin>>n;
for(int i=0;i<n;i++)
cin>>tree[i];
int d=FindParent(tree,i,j,n);
cout<<d<<endl;
}
//5 8
//9
//1 2 3 4 5 6 7 8 9
//2
*/
/*
//后序遍历二叉树的非递归算法 P123.3
typedef struct Tree
{
int data;
Tree *lchild,*rchild;
};
void PostTraval(Tree *t)
{
Tree *stack[maxSize];
int top=-1;
stack[++top]=t;
Tree *r;
while(top!=-1)
{
while(t)
{
stack[++top]=t;
t=t->lchild;
}
t=stack[top];
if(t->lchild==NULL&&t->rchild!=r)
{
t=t->rchild;
}
else
{
top--;
cout<<t->data<<" ";
r=t;
t=NULL;
}
}
}
Tree *Create(Tree *&t,int ch)
{
if(t==NULL)
{
t=(Tree*)malloc(sizeof(Tree));
t->data=ch;
t->lchild=NULL;
t->rchild=NULL;
}
else if(ch<=t->data)
t->lchild=Create(t->lchild,ch);
else if(ch>t->data)
t->rchild=Create(t->rchild,ch);
return t;
}
int main()
{
Tree *t=NULL;
int n,a[maxSize];
cin>>n;
for(int i=0;i<n;i++)
{
cin>>a[i];
t=Create(t,a[i]);
}
PostTraval(t);
}
*/
/*
//给出自下而上从右到左的层次遍历算法 P123 4 先把从上到下从左到右的层次遍历结果存到栈中,再从栈中输出
typedef struct Tree{
int data;
Tree *lchild,*rchild;
}Tree;
int s[maxSize];
int top=-1;
void LayerTraval(Tree *t)
{
Tree *queue[maxSize];
int front=1;
int rear=1;
queue[rear++]=t;
while(front!=rear)
{
Tree *q=queue[front];
s[++top]=q->data;
front++;
if(q->lchild)
{
queue[rear++]=q->lchild;
}
if(q->rchild)
{
queue[rear++]=q->rchild;
}
}
}
Tree *insert(Tree *&t,int ch)
{
if(t==NULL)
{
t=(Tree*)malloc(sizeof(Tree));
t->data=ch;
t->lchild=NULL;
t->rchild=NULL;
}
if(ch<t->data)
{
t->lchild=insert(t->lchild,ch);
}
else if(ch>t->data)
{
t->rchild=insert(t->rchild,ch);
}
return t;
}
int main()
{
Tree *t=NULL;
int n,a[maxSize];
cin>>n;
for(int i=0;i<n;i++)
{
cin>>a[i];
t=insert(t,a[i]);
}
LayerTraval(t);
while(top!=-1)
{
cout<<s[top--]<<" ";
}
}
//6
//5 3 6 1 4 8
//8 4 1 6 3 5
*/
/*
//非递归法求二叉树的高度 P123 5
typedef struct Tree{
int data;
Tree *lchild,*rchild;
}Tree;
Tree *insert(Tree *&t,int ch)
{
if(t==NULL)
{
t=(Tree*)malloc(sizeof(Tree));
t->data=ch;
t->lchild=t->rchild=NULL;
}
else if(ch<t->data)
{
insert(t->lchild,ch);
}
else if(ch>t->data)
{
insert(t->rchild,ch);
}
return t;
}
void DFS(Tree *t,int &max)
{
Tree *stack[maxSize];
int top=-1;
int h=0;
while(t||top!=-1)
{
while(t)
{
stack[++top]=t;
t=t->lchild;
h++;
if(h>max)
max=h;
}
if(top!=-1)
{
t=stack[top--];
h--;
t=t->rchild;
}
}
}
int main()
{
Tree *t=NULL;
int n;
int a[maxSize];
cin>>n;
for(int i=0;i<n;i++)
{
cin>>a[i];
t=insert(t,a[i]);
}
int max=0;
DFS(t,max);
cout<<max<<endl;
}
//
// 6
// 5 3 6 2 1 8
// 4
*/
/*
//由先序遍历和中序遍历建树 P123 6
typedef struct Tree{
int data;
Tree *lchild,*rchild;
}Tree;
Tree *CreatTree(int pre[],int in[],int preL,int preR,int inL,int inR)
{
if(preL<=preR)
{
Tree *p=(Tree*)malloc(sizeof(Tree));
p->lchild=NULL;
p->rchild=NULL;
p->data=pre[preL];
int k;
for(int i=inL;i<=inR;i++)
{
if(in[i]==pre[preL])
{
k=i;
break;
}
}
p->lchild=CreatTree(pre,in,preL+1,preL+(k-inL),inL,k-1);
p->rchild=CreatTree(pre,in,preL+(k-inL)+1,preR,k+1,inR);
return p;
}
return NULL;
}
void postTraval(Tree *t)
{
if(t)
{
postTraval(t->lchild);
postTraval(t->rchild);
cout<<t->data<<" ";
}
}
int main()
{
int pre[maxSize],in[maxSize];
int n;
cin>>n;
for(int i=0;i<n;i++)
{
cin>>pre[i];
}
for(int i=0;i<n;i++)
{
cin>>in[i];
}
Tree *t=NULL;
t=CreatTree(pre,in,0,n-1,0,n-1);
postTraval(t);
}
//6
//5 3 2 1 6 8
//1 2 3 5 6 8
//1 2 3 8 6 5
*/
/*
//链表形式,判断二叉树是否是完全二叉树 P123 7 //检查每一层的节点数
typedef struct Tree{
int data,lev;
Tree *lchild,*rchild;
}Tree;
Tree *insert(Tree *&t,int ch)
{
if(t==NULL)
{
t=(Tree*)malloc(sizeof(Tree));
t->data=ch;
t->lchild=t->rchild=NULL;
}
else if(ch<t->data)
{
insert(t->lchild,ch);
}
else if(ch>t->data)
{
insert(t->rchild,ch);
}
return t;
}
int BFS (Tree *t)
{
Tree *queue[maxSize];
int front=0;
int rear=0;
int l;
queue[rear]=t;
queue[rear]->lev=1;
rear++;
while(front!=rear)
{
Tree *p=queue[front];
l=queue[front]->lev;
front++;
if(p->lchild)
{
queue[rear]=p->lchild;
queue[rear]->lev=l+1;
rear++;
}
if(p->rchild)
{
queue[rear]=p->rchild;
queue[rear]->lev=l+1;
rear++;
}
}
for(int i=1;i<l;i++)
{
int cnt=0;
for(int j=0;j<rear;j++)
{
if(queue[j]->lev==i)
{
cnt++;
}
}
if(cnt!=pow(2,i-1))
return 0;
}
return 1;
}
int main()
{
Tree *t=NULL;
int n;
int a[maxSize];
cin>>n;
for(int i=0;i<n;i++)
{
cin>>a[i];
t=insert(t,a[i]);
}
int d=BFS(t);
cout<<d<<endl;
}
*/
/*
//输出从i到j的所有简单路径 王道P201,5 !!重点
typedef struct ENode
{
int adjvex;
struct ENode *next;
}ENode; //边表节点
typedef struct VNode
{
int data;
struct ENode *firstchild;
}VNode,VNodeL[maxSize]; //顶点表节点
typedef struct Graph
{
int VNum,ENum;
VNodeL adjlist;
}Graph;
void Create(Graph &g)
{
cin>>g.VNum>>g.ENum;
for(int i=0;i<g.VNum;i++)
{
cin>>g.adjlist[i].data;
g.adjlist[i].firstchild=NULL;
}
for(int i=0;i<g.ENum;i++)
{
int x,y;
cin>>x>>y;
ENode *q=(ENode*)malloc(sizeof(ENode));
q->adjvex=y;
q->next=g.adjlist[x].firstchild;
g.adjlist[x].firstchild=q;
}
}
int visited[maxSize];
void DFS(Graph g,int i,int j,int d,int path[])
{
visited[i]=1;
int stack[maxSize];
int top=-1;
stack[++top]=i;
d++;
path[d]=i;
while(top!=-1)
{
i=stack[top];
cout<<i<<endl;
// if(i==j)
// {
// for(int k=0;k<=d;k++)
// {
// cout<<path[k];
// }
// }
ENode *p=g.adjlist[i].firstchild;
while (p&&visited[p->adjvex]==1) {
p=p->next;
}
if(p==NULL)
{
// visited[stack[top]]=0;
top--;
}
else
{
d++;
path[d]=p->adjvex;
stack[top++]=p->adjvex;
visited[p->adjvex]=1;
}
}
}
//void DFS(Graph g,int i,int j,int d,int path[])
//{
// visited[i]=1;
// d++;
// path[d]=i;
// if(i==j)
// {
// for(int k=0;k<=d;k++)
// {
// cout<<path[k]<<" ";
// }
// cout<<endl;
// }
// ENode *p=g.adjlist[i].firstchild;
// while(p)
// {
// if(visited[p->adjvex]==0)
// {
// DFS(g,p->adjvex,j,d,path);
// visited[p->adjvex]=0;
// }
// p=p->next;
// }
//}
int main()
{
Graph g;
Create(g);
for(int i=0;i<g.VNum;i++)
{
visited[i]=0;
}
int i=1,j=3;
int path[maxSize];
int d=0;
DFS(g,i,j,d,path);
}
//6
//6
//0 1 2 3 4 5
//0 1
//1 2
//2 3
//1 4
//4 3
//3 5
//0 1 4 3
//0 1 2 3
*/
/*
//对于顺序存储的树,求编号i,j的两个节点的最近公共祖先节点的值 P110 5
//顺序存储的树直接用数组表示就好了
int FindParent(int tree[],int i,int j,int n)
{
while(i!=j)
{
if(i>j)
i=i/2;
else if(i<j)
j=j/2;
}
return i;
}
int main()
{
int i,j;
cin>>i>>j;
int n;
int tree[maxSize];
cin>>n;
for(int i=0;i<n;i++)
cin>>tree[i];
int d=FindParent(tree,i,j,n);
cout<<d<<endl;
}
//5 8
//9
//1 2 3 4 5 6 7 8 9
//2
*/