UVA11518 HDU2754 Dominos 2DFS

Posted 海岛Blog

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了UVA11518 HDU2754 Dominos 2DFS相关的知识,希望对你有一定的参考价值。

Dominos 2
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 365 Accepted Submission(s): 105

Problem Description
Dominos are lots of fun. Children like to stand the tiles on their side in long lines. When one domino falls, it knocks down the next one, which knocks down the one after that, all the way down the line. However, sometimes a domino fails to knock the next one down. In that case, we have to knock it down by hand to get the dominos falling again.

Given a set of dominos that are knocked down by hand, your task is to determine the total number of dominos that fall.

Input
The first line of each test chunk contains an integer specifying the number of test cases in this chunk to follow. Each test case begins with a line containing three integers n, m, l no larger than 10 000, followed by m+l additional lines. The first integer n is the number of domino tiles. The domino tiles are numbered from 1 to n. Each of the m lines after the first line contains two integers x and y indicating that if domino number x falls, it will cause domino number y to fall as well. Each of the following l lines contains a single integer z indicating that the domino numbered z is knocked over by hand.
Please process to the end of the data file.

Output
For each test case, output a line containing one integer, the total number of dominos that fall over.

Sample Input
1
3 2 1
1 2
2 3
2
1
3 2 1
1 2
2 3
2

Sample Output
2
2

Source
University of Waterloo Local Contest 2008.10.04

问题链接UVA11518 HDU2754 Dominos 2
问题简述:多米诺骨牌,从指定的一张牌开始,每次推倒另外一张牌,问最后总共推倒几张牌?
问题分析:图的搜索问题,用DFS来解决。
程序说明:图用邻接表表示。HDU的输入数据跟UVA略有不同,需要多加一个循环加以控制。
参考链接:(略)
题记:(略)

AC的C++语言程序如下:

/* UVA11518 HDU2754 Dominos 2 */

#include <bits/stdc++.h>

using namespace std;

const int N = 10000 + 1;
vector<int> g[N];
int vis[N];

void dfs(int n)
{
    vis[n] = 1;
    for (int i = g[n].size() - 1; i >= 0; i--)
        if (vis[g[n][i]] == 0) dfs(g[n][i]);
}

int main()
{
    int t, n, m, l, u, v;
    while (~scanf("%d", &t))
    while (t--) {
        scanf("%d%d%d", &n, &m, &l);

        for(int i = 1; i <= n; i++) g[i].clear();
        memset(vis, 0, sizeof vis);

        while (m--) {
            scanf("%d%d", &u, &v);
            g[u].push_back(v);
        }

        while (l--) {
            scanf("%d", &u);
            dfs(u);
        }

        int cnt = 0;
        for (int i = 1; i <= n; i++)
            if (vis[i]) cnt++;

        printf("%d\\n", cnt);
    }

    return 0;
}

以上是关于UVA11518 HDU2754 Dominos 2DFS的主要内容,如果未能解决你的问题,请参考以下文章

UVA11504- Dominos(Tarjan+缩点)

HDU3173 Dominos

UVA116 HDU1619 Unidirectional TSPDP

2019年7月做题记录

POJ1420 HDU1659 UVA196 UVALive5606 SpreadsheetDFS

POJ3370 UVA11237 HDU1808 Halloween treats鸽笼原理