(beginer)DFS (强连通)UVA 11504 Dominos

Problem D: 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 knock the next one down. In that case, we have to knock it down by hand to get the dominos falling again.

Your task is to determine, given the layout of some domino tiles, the minimum number of dominos that must be knocked down by hand in order for all of the dominos to fall.

Input Specification

The first line of input contains one integer specifying the number of test cases to follow. Each test case begins with a line containing two integers, each no larger than 100 000. The first integer n is the number of domino tiles and the second integer m is the number of lines to follow in the test case. The domino tiles are numbered from 1 to n. Each of the following lines contains two integers x and y indicating that if domino number x falls, it will cause domino number y to fall as well.

Sample Input

1
3 2
1 2
2 3

Output Specification

For each test case, output a line containing one integer, the minimum number of dominos that must be knocked over by hand in order for all the dominos to fall.

Output for Sample Input

1

题意:给出多米诺骨牌之间的倒下的关系,如果u倒下,那么v会跟着倒下。然后问最少需要多少次手动让一个牌倒下,能让所有的牌都倒下。

思路:我们先找出所有的强连通分量,找出来之后就是一个DAG,把每个分量当做是一个点,因为里面任意一个牌倒下的话,里面所有的牌都会跟着倒下。那么这个时候有多少个点的入度等于0,就需要手动推倒多少张牌。

代码:
#include<iostream>
#include<cstring>
#include<string.h>
#include<cstdio>
#include<stack>
#include<vector>
using namespace std;
const int maxn = 100000+5;
int n , m , sccno[maxn] , pre[maxn] , low[maxn] , ind[maxn];
int scc_cnt , dfs_clock;
vector<int> G[maxn];
stack<int> S;

void init()
{
while (S.size()) S.pop();
for (int i = 1 ; i <= n ; ++i) 
{
G[i].clear();
sccno[i] = 0;
pre[i] = 0;
ind[i] = 0;
low[i] = 0;
}
scc_cnt = dfs_clock = 0;
}

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

void dfs(int u)
{
pre[u] = low[u] = ++dfs_clock;
S.push(u);
for (int i = 0 ; i < G[u].size() ; ++i)
{
int v = G[u][i];
if (!pre[v])
{
dfs(v);
low[u] = min(low[u],low[v]);
} else if (!sccno[v])
low[u] = min(low[u],pre[v]);
}
if (low[u]==pre[u])
{
++scc_cnt;
while (true)
{
int x = S.top(); S.pop();
sccno[x] = scc_cnt;
if (x==u) break;
}
}
}

void solve()
{
for (int i = 1 ; i <= n ; ++i) if (!pre[i]) 
dfs(i);
for (int i = 1 ; i <= n ; ++i)
{
int u = i;
for (int j = 0 ; j < G[u].size() ; ++j)
{
int v = G[u][j];
if (sccno[u]==sccno[v]) continue;
++ind[sccno[v]];
}
}
int ans = 0;
for (int i = 1 ; i <= scc_cnt ; ++i) if (ind[i]==0)
++ans;
cout << ans << endl;
}

int main()
{
int T; cin>>T;
while (T--)
{
scanf("%d%d",&n,&m);
init();
input();
solve();
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值