题目描述
输入
输出
示例输入
2 1 1 2 2 0
示例输出
12
#include <iostream> #include<cstdio> #include<cstdlib> #include<cstring> using namespace std; struct PTNode { int data;//类的标号; int parent;//当前元素的双亲结点序号(类的直接基类) }p[101]; int main() { int n,m; while(~scanf("%d%d",&n,&m)) { int count=0;//统计有多少类没有直接基类; for(int i=1;i<=n;i++) { p[i].parent=0;//初始化 p[i].data=i; } while(m--) { int x,y; scanf("%d%d",&x,&y); p[y].parent=x; } for(int i=1;i<=n;i++) if(p[i].parent==0)//若父结点为空, 即为根节点 count++; printf("%d\n",count); } return 0; } #include <bits/stdc++.h> using namespace std; int mp[110][110];//表示谁是谁的基类 int main() { int n, m, s; int a, b; while(~scanf("%d %d", &n, &m)) { if(m == 0)//没有直接基类; printf("%d\n",n); else { s=0;//记录有直接基类的类; memset(mp, 0, sizeof(mp));//初始化; while(m--) { scanf("%d %d", &a, &b); mp[a][b] = 1;//表示a是b的直接基类; } for(int i = 1; i <= n; i++) { for(int j = 1; j <= n; j++) { if(i != j&&mp[j][i] == 1) { s++; break; } } } printf("%d\n", n-s); } } return 0; } #include <algorithm> #include <stdio.h> #include <string.h>//memset函数存在于此头文件中 using namespace std; int main() { bool st[500];//标记含有单继承关系的类(逻辑型) int n,m,a,b; while(~scanf("%d %d",&n,&m)) { int cnt=n; memset(st,false,sizeof(st));//初始化; while(m--) { scanf("%d %d",&a,&b); if(!st[b])//b有直接基类; { st[b]=true; cnt--; } } printf("%d\n",cnt); } return 0; }