注:最近这一系列ACM的内容,都是2年多之前的代码,自己回顾一下。
Description
多米诺骨牌大概大家都玩过。当一块骨牌倒下时,它会撞击下一块骨牌,这样一直持续下去。当然,有时候,由于摆放的问题,可能中途会断掉。此时我们不得不用手将它推倒。
Input
输入的第一行包含一个整数c,表示测试数据的组数。
每组数据的第一行包含三个整数n,m,L,其中n表示骨牌数目,m表示有m个关系,L表示用手推倒骨牌的次数。三个整数都不超过10000。
接下来m行,每行包含两个整数x,y,表示如果x倒了,y也会跟着一起倒。反过来不一定。
再接下来L行,每行有一个整数z,表示骨牌z被用手推倒了。骨牌的标号从1开始。
Output
对每组数据,输出最后倒了的骨牌的数量。
Sample Input
1
3 2 1
1 2
2 3
2
Sample Output
2
Source
Mikeymouse@Solaris
多米诺骨牌大概大家都玩过。当一块骨牌倒下时,它会撞击下一块骨牌,这样一直持续下去。当然,有时候,由于摆放的问题,可能中途会断掉。此时我们不得不用手将它推倒。
Input
输入的第一行包含一个整数c,表示测试数据的组数。
每组数据的第一行包含三个整数n,m,L,其中n表示骨牌数目,m表示有m个关系,L表示用手推倒骨牌的次数。三个整数都不超过10000。
接下来m行,每行包含两个整数x,y,表示如果x倒了,y也会跟着一起倒。反过来不一定。
再接下来L行,每行有一个整数z,表示骨牌z被用手推倒了。骨牌的标号从1开始。
Output
对每组数据,输出最后倒了的骨牌的数量。
Sample Input
1
3 2 1
1 2
2 3
2
Sample Output
2
Source
Mikeymouse@Solaris
数据结构为一个有向图,用邻接表的结构记录结构
#include <iostream>
using namespace std;
typedef struct link
{
int pd;
struct link *next;
}L, *LPtr;
int sum=0;
LPtr l[10010];
void insert(LPtr *ptr, int to)//邻接表
{
LPtr p1 = (LPtr)malloc(sizeof(L));
p1->pd = to;
p1->next = NULL;
if ((*ptr) == NULL)
(*ptr) = p1;
else
{
LPtr p2=(*ptr);
while (p2->next != NULL)
{
p2 = p2->next;
}
p2->next = p1;
}
}
void DPS(LPtr link[10010], int gg[10010], int pos)
{
if (gg[pos])
{
gg[pos] = 0;
sum ++;
LPtr p1 = link[pos];
int pos1;
while (p1 != NULL)
{
pos1 = p1->pd;
DPS(link, gg, pos1);
p1 = p1->next;
}
}
}
void linkprint(LPtr ptr, int num)
{
cout << num << ":";
while (ptr!=NULL)
{
cout << ptr->pd << " ";
ptr = ptr->next;
}
cout << endl;
}
int main()
{
int c, n, m, L;
int g[10010] = {0};
int a, b, p;
cin >> c;
while (c--)
{
cin >> n >> m >> L;
for (int i = 1; i <= n; i++)
{
g[i] = 1;
l[i] = NULL;
}
for (int i = 1; i <= m; i++)
{
cin >> a >> b;
insert(&l[a], b);
}
sum = 0;
while (L--)
{
cin >> p;
DPS(l, g, p);
}
cout << sum << endl;
}
// system("pause");
return 0;
}