题意:
说有一些公司之间的收购股票关系,A收购B,B收购C,C收购A,那么第一条和第二条操作是允许的,第三条是被禁止的(因为这可能造成公司操控自己公司的股票)。问需要驳回多少条操作(就是被禁止的操作)。
思路:
那么,其实就可以转化成为一个图中添加一条有向边是否会形成环路的问题。而形成环的问题我们可以简化成两个点之间是否可达的问题。
比如如果a可达b,那么b收购a就是禁止的。
那么对于加入a b这条边来说:
1.a可达b
2.可达a的点也同样可达b
3.b可达到的点,a也可达到
4.所有可以达到b的点,也可以达到b所有可以达到的点。
如下图:
上图是没有加入a b这条边的情况。
下图是加入a b这条边后的情况。
上面标注的1234对应上面所说的1234.
Code:
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cctype>
#include<cmath>
#include<algorithm>
#include<iostream>
#include<string>
#include<vector>
#include<bitset>
#include<queue>
#include<stack>
#include<list>
#include<map>
#include<set>
#define TEST
#define LL long long
#define Mt(f, x) memset(f, x, sizeof(f));
#define rep(i, s, e) for(int i = (s); i <= (e); ++i)
#ifdef TEST
#define See(a) cout << #a << " = " << a << endl;
#define See2(a, b) cout << #a << " = " << a << ' ' << #b << " = " << b << endl;
#define debug(a, s, e) rep(_i, s, e) {cout << a[_i] << ' ';} cout << endl;
#define debug2(a, s, e, ss, ee) rep(i_, s, e) {debug(a[i_], ss, ee)}
#else
#define See(a)
#define See2(a, b)
#define debug(a, s, e)
#define debug2(a, s, e, ss, ee)
#endif // TEST
const int MAX = 2e9;
const int MIN = -2e9;
const double eps = 1e-8;
const double PI = acos(-1.0);
using namespace std;
const int N = 240;
bool to[N][N];
int n, m;
void addEdge(int a, int b)
{
to[a][b] = true;
for(int i = 1; i <= n; ++i)
{
if(to[i][a])
{
to[i][b] = true;
}
}
for(int i = 1; i <= n; ++i)
{
if(to[b][i])
{
to[a][i] = true;
for(int k = 1; k <= n; ++k)
{
if(to[k][b])
{
to[k][i] = true;
}
}
}
}
}
int main()
{
int _ = 1;
while(~scanf("%d%d", &n, &m))
{
if(n == 0 && m == 0)
{
return 0;
}
Mt(to, false);
int ans = 0;
while(m--)
{
int a, b;
scanf("%d%d", &a, &b);
if(to[b][a] || a == b)
{
ans++;
}
else if(!to[a][b])
{
addEdge(a, b);
}
}
printf("%d. %d\n", _++, ans);
}
return 0;
}