暑假集训 8.16 sdutoj3642 判断给定森林中有多少棵树特别版 (简单考查邻接表)

来源:互联网 发布:mac wine 安装软件 编辑:程序博客网 时间:2024/04/29 14:21

判断给定森林中有多少棵树特别版

Time Limit: 1000ms   Memory limit: 65536K 

题目描述

 众人皆知,在编程领域中,C++是一门非常重要的语言,不仅仅因为其强大的功能,还因为它是很多其他面向对象语言的祖先和典范。不过这世上几乎没什么东 西是完美的,C++也不例外,多继承结构在带来强大功能的同时也给软件设计和维护带来了很多困难。为此,在java语言中,只允许单继承结构,并采用接口 来模拟多继承。KK最近获得了一份java编写的迷你游戏的源代码,他对这份代码非常感兴趣。这份java代码是由n个类组成的(本题不考虑接口),n个类分别用数字1..n表示。现在给你n个类之间的关系,有q次询问,每次询问某一个有多少个直接继承的子类。输入子类的个数和标号(标号按照字典序大小输出)。

输入

首先输入一个整数T,表示数据的组数。每组数据格式如下。
第一行包含两个整数n,m,表示该份代码中的n个类和m个单继承关系(1<=m<n<=10^5)

输出

 对于每组输入。输出询问类的子类的数量和编号。

示例输入

110 92 13 24 35 36 37 38 39 310 5107637128125

示例输出

0064 5 6 7 8 90121301213110

邻接表存储图,然后询问的时候依次输出某个head[]中的元素就行了.....

用矩阵的话会爆内存;

不过题目描述和题目....

///ACcode

#include <bits/stdc++.h>using namespace std;const int maxn=100010;typedef struct node{    int data;    node *next;} node,*Node;///有序的邻接表插入函数...头指针的数据域代表"后面"一共有多少个元素void Insert(Node &head,int x){    Node q,a,tail; ///a是q的前驱节点 tail是要插入的节点    tail=new node;    tail->data=x;    if (head==NULL)    {        head=new node;        head->data=1;  ///初始为 1        head->next=tail;        tail->next=NULL;    }    else if (head!=NULL)    {        head->data++; ///链的 数据个数++        a=head; ///前驱        q=head->next;        while (q)        {            if (q->data > x)            {                a->next=tail;                tail->next=q;                break; ///从小到大排序 遇到大的就插入 然后一定要跳出while            }            a=a->next;            q=q->next;        }        if (q==NULL)  ///没找到比x大的 所以把x放在最后        {            a->next=tail;            tail->next=NULL;        }    }}int main(){    int n,m,i;    int u,v;    int key;    int t;    Node head[maxn],tail;    cin>>t;    while (t--)    {        cin>>n>>m;        for (i=1; i<=n; i++) ///初始化        {            head[i]=NULL;        }        for (i=1; i<=m; i++)        {            cin>>u>>v;            Insert(head[v],u); ///将u的数据插入到v的节点中        }        int q;        cin>>q;        for (i=1; i<=q; i++)        {            cin>>key;            if (head[key]==NULL)            {                cout<<"0"<<endl;            }            else            {                cout<<head[key]->data<<endl;                tail=head[key]->next;                while (tail)                {                    cout<<tail->data;                    if (tail->next!=NULL)                    {                        cout<<" ";                    }                    tail=tail->next;                }                cout<<endl;            }        }    }    return 0;}

有关的邻接表传送门  http://blog.csdn.net/gentle_guan/article/details/52214869


0 0
原创粉丝点击