浙大PAT甲级-1004
2017-06-03 00:05
417 查看
代码有点乱,自己都懒得再看了。
关键点:
-用二维数组存储树的节点
广度遍历,思路应该比较简单#include <iostream>
using namespace std;
int tree[100][100];
int visit[100];
void init(int n)
{
for(int i=1; i<=n; i++)
{
visit[i]=0;
for(int j=1; j<=n; j++)
tree[i][j]=0;
}
}
int main()
{
int n,m,x;
cin>>n>>m;
init(n);
x=m;
while(x--)
{
int a,b;
cin>>a>>b;
while(b--)
{
int c;
cin>>c;
tree[a][c]=1;
}
}
visit[1]=1;
int leaf=0;
bool judge=0;
bool judge2=0;
for(int i=2; i<=n; i++)
if(tree[1][i]==1)
{
visit[i]=1;
judge=1;
}
if(judge==1)
{
cout<<0;
for(int k=1; k<=n; k++)
{
leaf=0;
judge2=0;
for(int i=2; i<=n; i++)
{
if(visit[i]==k){
judge=0;
judge2=1;
for(int j=i; j<=n; j++)
{
if(tree[i][j]==1)
{
visit[j]=k+1;
judge=1;
}
}
if(judge==0)
leaf++;
}
}
if(judge2==0)
break;
cout<<" "<<leaf;
}
}
else
{
cout<<1;
}
cout<<endl;
return 0;
}
关键点:
-用二维数组存储树的节点
广度遍历,思路应该比较简单#include <iostream>
using namespace std;
int tree[100][100];
int visit[100];
void init(int n)
{
for(int i=1; i<=n; i++)
{
visit[i]=0;
for(int j=1; j<=n; j++)
tree[i][j]=0;
}
}
int main()
{
int n,m,x;
cin>>n>>m;
init(n);
x=m;
while(x--)
{
int a,b;
cin>>a>>b;
while(b--)
{
int c;
cin>>c;
tree[a][c]=1;
}
}
visit[1]=1;
int leaf=0;
bool judge=0;
bool judge2=0;
for(int i=2; i<=n; i++)
if(tree[1][i]==1)
{
visit[i]=1;
judge=1;
}
if(judge==1)
{
cout<<0;
for(int k=1; k<=n; k++)
{
leaf=0;
judge2=0;
for(int i=2; i<=n; i++)
{
if(visit[i]==k){
judge=0;
judge2=1;
for(int j=i; j<=n; j++)
{
if(tree[i][j]==1)
{
visit[j]=k+1;
judge=1;
}
}
if(judge==0)
leaf++;
}
}
if(judge2==0)
break;
cout<<" "<<leaf;
}
}
else
{
cout<<1;
}
cout<<endl;
return 0;
}
相关文章推荐
- 浙大Pat | 浙大pat 牛客网甲级 1004 Acute Stroke (30) BFS
- 浙大pat | 牛客网甲级 1025 Sort with Swap(0) (25)排序
- 浙大PAT甲级 1029
- 浙大PAT甲级 1037
- *浙大PAT甲级 1045
- 浙大PAT甲级 1061
- 浙大PAT甲级 1076
- **浙大PAT甲级 1086 二叉树的先中根遍历求后根遍历
- PAT甲级1004
- 浙大PAT甲级-1030
- 浙大pat | 牛客网甲级 1037Find Coins (25) 数组循环遍历
- 浙大PAT甲级 1077
- 浙大PAT甲级-1005
- 浙大PAT乙级1004. 成绩排名 (20)
- 浙大PAT 1004. 成绩排名 (解题思路)
- PAT甲级C语言.1004. 成绩排名
- 浙大Pat | 浙大pat 牛客网甲级 1003 Highest Price in Supply Chain (25)DFS
- 浙大pat | 牛客网甲级 1045 Hello World for U (20) 字符串
- 浙大PAT甲级 1030
- 浙大PAT甲级 1046