hust 1511 Dominos 2

题目描述

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.

输入

The first line of input contains one integer specifying the number of test
cases 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.

输出

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

样例输入

1
3 2 1
1 2
2 3
2

样例输出

2
一开始有点糊涂,感觉dfs可能超时,就写了并查集,结果发现它不符合对称性,然后又想了想dfs,才知道时间复杂度为O(n+n),这才写了,还真是,唉!分析的能力有待提升


#include<map>
#include<set>
#include<stack>
#include<queue>
#include<cmath>
#include<vector>
#include<cstdio>
#include<string>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#define inf 0x0f0f0f0f
using namespace std;
const int maxn=10000+10;

vector<int>tree[maxn];
bool vis[maxn];
int ans;

void init(int n)
{
for (int i=0;i<=n;i++)
{
vis[i]=0;
tree[i].clear();
}
}

void dfs(int u)
{
ans++;
vis[u]=true;
for (int i=0;i<tree[u].size();i++)
{
int v=tree[u][i];
if (!vis[v])
{
dfs(v);
}
}
}

int main()
{
int n,m,l,t,x,y;
scanf("%d",&t);
while(t--)
{
scanf("%d%d%d",&n,&m,&l);
init(n);
while(m--)
{
scanf("%d%d",&x,&y);
tree[x].push_back(y);
}
ans=0;
while(l--)
{
scanf("%d",&x);
if (!vis[x]) dfs(x);
}
printf("%d\n",ans);
}
return 0;
}

作者 chensunrise

hust 1511 Dominos 2,布布扣,bubuko.com

时间: 2024-10-26 18:47:44

hust 1511 Dominos 2的相关文章

hust 1260 Dominos &amp;&amp; hust 1516 Dominos

题目描述 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 knoc

HUST 1588 辗转数对

1588 - 辗转数对 时间限制:1秒 内存限制:128兆 155 次提交 27 次通过 题目描述 假设当前有一个数对(a, b),我们可以通过一步将这个数对变为一个新数对(a + b, b)或者是(a, a + b).初始的数对为(1, 1),你的任务是找到一个数字k,即通过最少的步数使得这个数对中至少一个数字等于n. 输入 输入包括多组数据,每组数据包括一行,每行有一个整数n. 输出 每组数据输出一行,每行一个整数n. 样例输入 5 3 样例输出 3 2 提示 第一个样例的方法是 (1,1)

HUST 1698 - 电影院 组合数学 + 分类思想

http://acm.hust.edu.cn/problem/show/1698 题目就是要把一个数n分成4段,其中中间两段一定要是奇数. 问有多少种情况. 分类, 奇数 + 奇数 + 奇数 + 奇数 奇数 + 奇数 + 奇数 + 偶数 偶数 + 奇数 + 奇数 + 奇数 偶数 + 奇数 + 奇数 + 偶数 然后奇数表达成 2 * a - 1这个样子,就能列出方程. 然后就是类似于解a1 + a2 + a3 + a4 = x的问题了. #include <cstdio> #include &l

HUST 1017 - Exact cover (Dancing Links 模板题)

1017 - Exact cover 时间限制:15秒 内存限制:128兆 自定评测 5584 次提交 2975 次通过 题目描述 There is an N*M matrix with only 0s and 1s, (1 <= N,M <= 1000). An exact cover is a selection of rows such that every column has a 1 in exactly one of the selected rows. Try to find o

勇士出征[HUST 1439]

勇士出征[HUST 1439] 时间1000ms,内存64MB 第十届"北大青鸟"杯浙江师范大学程序设计竞赛 这道题跟UVA-12100是一样的题目.我这里用的是STL的双端队列deque容器配合优先队列priority_queue,写起来会比较轻松:依次将输入压入队列,然后不断扫描队列,符合最大优先级的(优先队列的顶部元素)将其送出,而不再压入队尾.直到找到符合自己的标记的为止. 当然这道题也有用数组使用滚雪球的方式实现的,也就是开一个大的数组,每次将元素后挪时,直接将其放在数组末尾

http://mirror.centos.org/centos/7.2.1511/os/x86_64/Packages/, 开源软件清单list

http://mirror.centos.org/centos/7.2.1511/os/x86_64/Packages/ http://vault.centos.org/7.2.1511/os/Source/SPackages/ Name Last modified Size Description Parent Directory   -   389-ds-base-1.3.4.0-19.el7.x86_64.rpm 2015-11-25 14:10 1.7M   389-ds-base-de

UVALive 2659+HUST 1017+ZOJ 3209 (DLX

UVALive 2659 题目:16*16的数独.试了一发大白模板. /* * @author: Cwind */ //#pragma comment(linker, "/STACK:102400000,102400000") #include <iostream> #include <map> #include <algorithm> #include <cstdio> #include <cstring> #include

HUST 1341 A - A Simple Task(哈理工 亚洲区选拔赛练习赛)

A - A Simple Task Time Limit:1000MS    Memory Limit:131072KB    64bit IO Format:%lld & %llu SubmitStatusPracticeHUST 1341 Description As is known to all, lots of birds are living in HUST. A bird has s units of food on the first day, and eats k units

HDU 1535 &amp;&amp; POJ 1511 Invitation Cards (SPFA 模板 + 反向建图)

Invitation Cards HDU: Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) POJ: Time Limit: 8000 MS     Memory Limit: 262144 K       Problem Description In the age of television, not many people attend theater performa