题目描述
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.
输入
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.
输出
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.
样例输入
1 3 2 1 2 2 3
样例输出
1
这个题怎么看都觉得是一个水题,只要找入度为0的点不就是吗?当然是了,入度为0的点肯定对的,但是考虑一下,若1-〉2,2-〉1,那么入度为0 的点为0个,但是答案肯定为1,为什么呢,因为出现了环,所以我们要不环去掉,就是要缩点,当然是强连通分量来做,
#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; int indegree[100000+10]; struct SCC { static const int maxn=100000+10; vector<int>group[maxn],scc[maxn]; int pre[maxn],lowlink[maxn],sccno[maxn],dfs_clock,scc_cnt,n,m; stack<int>S; void init() { for (int i=0;i<=n;i++) group[i].clear(); } void addedge(int from,int to) { group[from].push_back(to); } void dfs(int u) { pre[u]=lowlink[u]=++dfs_clock; S.push(u); for (int i=0;i<group[u].size();i++) { int v=group[u][i]; if (!pre[v]) { dfs(v); lowlink[u]=min(lowlink[u],lowlink[v]); } else if (!sccno[v]) { lowlink[u]=min(lowlink[u],pre[v]); } } if (lowlink[u]==pre[u]) { scc_cnt++; scc[scc_cnt].clear(); while (1) { int x=S.top(); S.pop(); scc[scc_cnt].push_back(x); sccno[x]=scc_cnt; if (x==u) break; } } } void find_scc() { dfs_clock=scc_cnt=0; memset(pre,0,sizeof(pre)); memset(sccno,0,sizeof(sccno)); for (int i=1;i<=n;i++) if (!pre[i]) dfs(i); } }; SCC Dominos; int main() { int x,y,M,T; scanf("%d",&T); while(T--) { scanf("%d%d",&Dominos.n,&Dominos.m); Dominos.init(); M=Dominos.m; while(M--) { scanf("%d%d",&x,&y); Dominos.addedge(x,y); } Dominos.find_scc(); for (int i=1;i<=Dominos.scc_cnt;i++) indegree[i]=0; for (int u=1;u<=Dominos.n;u++) for (int i=0;i<Dominos.group[u].size();i++) { int v=Dominos.group[u][i]; if (Dominos.sccno[u]!=Dominos.sccno[v]) indegree[Dominos.sccno[v]]++; } int ans=0; for (int i=1;i<=Dominos.scc_cnt;i++) if (indegree[i]==0) ans++; printf("%d\n",ans); } return 0; }
作者 chensunrise
hust 1260 Dominos && hust 1516 Dominos,布布扣,bubuko.com
hust 1260 Dominos && hust 1516 Dominos
原文:http://www.cnblogs.com/chensunrise/p/3778870.html